Skip to content

Commit

Permalink
[Issue comixed#27] Add the PageCacheService class and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Dec 8, 2019
1 parent 895b108 commit 783cda6
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
@@ -0,0 +1,8 @@
package org.comixed;

import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;

@Configuration
@TestPropertySource("classpath:application.properties")
public class ComiXedTestContext {}
@@ -0,0 +1,79 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2019, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.service.library;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PageCacheService {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

@Value("${comixed.images.cache.location}")
private String cacheDirectory;

public byte[] findByHash(final String hash) {
this.logger.debug("Searching for cached image: hash={}", hash);

final File file = this.getFileForHash(hash);
if (file.exists() && !file.isDirectory()) {
this.logger.debug("Loading cached image content: {} bytes", file.length());
try {
return IOUtils.readFully(new FileInputStream(file), (int) file.length());
} catch (Exception error) {
this.logger.error("Failed to load cached image", error);
}
}

this.logger.debug("No image in cache");
return null;
}

File getFileForHash(final String hash) {
if (hash.length() != 32) {
return null;
}

final String path =
this.cacheDirectory
+ File.separator
+ hash.substring(0, 8)
+ File.separator
+ hash.substring(8, 16)
+ File.separator
+ hash.substring(16, 24)
+ File.separator
+ hash.substring(24, 32);
return new File(path);
}

public void saveByHash(final String hash, final byte[] content) throws IOException {
this.logger.debug("Saving image to cache: hash={}", hash);
final File file = this.getFileForHash(hash);
file.getParentFile().mkdirs();
IOUtils.write(content, new FileOutputStream(file, false));
}
}
@@ -0,0 +1,71 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2019, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.service.library;

import static junit.framework.TestCase.*;

import java.io.File;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {PageCacheService.class})
@TestPropertySource("classpath:application.properties")
public class PageCacheServiceTest {
private static final String TEST_MISSING_PAGE_HASH = "4C6DD238138491B89A3DB9BC6E3A3E2D";
private static final String TEST_PAGE_HASH = "D5397C1B6053B093CB133CA3B7081C9E";

@Autowired private PageCacheService pageCacheService;

@Before
public void setUp() {
// clean up any remnant
final File file = pageCacheService.getFileForHash(TEST_MISSING_PAGE_HASH);
if (file.exists()) {
file.delete();
}
}

@Test
public void testFindByHashNotCached() throws IOException {
final byte[] result = pageCacheService.findByHash(TEST_MISSING_PAGE_HASH);

assertNull(result);
}

@Test
public void testFindByHash() throws IOException {
final byte[] result = pageCacheService.findByHash(TEST_PAGE_HASH);

assertNotNull(result);
}

@Test
public void testSaveByHash() throws IOException {
pageCacheService.saveByHash(TEST_MISSING_PAGE_HASH, TEST_MISSING_PAGE_HASH.getBytes());

assertTrue(pageCacheService.getFileForHash(TEST_MISSING_PAGE_HASH).exists());
}
}
3 changes: 3 additions & 0 deletions comixed-services/src/test/resources/application.properties
Expand Up @@ -33,3 +33,6 @@ comic.entry.loaders[2].bean=filenameEntryLoader
# Filename entry loaders
comic.filename-entry.loaders[0].mask=ComicInfo.xml
comic.filename-entry.loaders[0].bean=comicInfoEntryLoader

# test image caching directory
comixed.images.cache.location=target/test-classes/image-cache
Binary file not shown.

0 comments on commit 783cda6

Please sign in to comment.