refactor(api): use NightCompress library for reading CBX (#113)
* build: drop unrar from dockerfile * build: add libarchive to dockerfile * ci: add libarchive for tests * build: try adding to the gradle build * refactor: use nightcompress for reading CBX * test: disable tests using nightcompress when unavailable * ci: drop showing java information * build: add libarchive to dev compose * build: fix libarchive link in alpine seems alpine doesn't create libarchive.so and this seems to be the easiest approach to fix that * build: use expected order for allowing java to use native libs * fix: ensure we initialize NightCompress before using it * fix: properly find input stream * fix: return amount of data copied * test: add helper to turn off tests in ArchiveService * test: rewrite CbxMetadataExtractorTest to mock archiveService * refactor: expose `Path` to make `CbxMetadataExtractor` easier to test * test: drop now useless test * test: add archiveservice to rar5 integration * test: mock out archiveservice in cbxreader test * test: use real archiveservice in cbxconverstionintegration * file: the cbxconversionservicetest also seems to be integration * test: remove unused imports from cbxmetadataextractortest * test: cbxcomicinfocompliance is also an integration * test: cbxmetadatawriter is also integration * chore: drop unused properties * test: make cbxmetadataextractortest do something for covers before it was basically useless because of fallback * fix: invert check * chore: better comment * test: fix more cbxmetadataextractor tests that never worked * test: we only ever raise ioexception * fix: handle `null` comicinfo when parsing * test: properly test for no files comicinfo * build: manual link of dev docker compose * test: if you use any matchers, all must be * build: drop unrar from dev compose * test: if you use any matchers, all must be * test: always get a fresh stream * test: drop more toFile
This commit is contained in:
+14
-22
@@ -9,9 +9,8 @@ import org.booklore.service.metadata.writer.CbxMetadataWriter;
|
||||
import org.booklore.service.reader.CbxReaderService;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.util.UnrarHelper;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
@@ -20,30 +19,21 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assumptions.assumeThat;
|
||||
|
||||
/**
|
||||
* Integration tests that feed a real RAR5 archive into the service layer
|
||||
* and verify junrar fails then the unrar CLI fallback kicks in.
|
||||
*/
|
||||
@EnabledIf("org.booklore.service.ArchiveService#isAvailable")
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class Rar5FallbackIntegrationTest {
|
||||
class Rar5IntegrationTest {
|
||||
|
||||
private static final Path RAR5_CBR = Path.of("src/test/resources/cbx/test-rar5.cbr");
|
||||
|
||||
@BeforeAll
|
||||
static void checkUnrarAvailable() {
|
||||
assumeThat(UnrarHelper.isAvailable())
|
||||
.as("unrar binary must be on PATH to run these tests")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
// -- CbxMetadataExtractor: extractMetadata fallback --
|
||||
|
||||
@Test
|
||||
@@ -51,7 +41,7 @@ class Rar5FallbackIntegrationTest {
|
||||
Path cbrCopy = tempDir.resolve("test.cbr");
|
||||
Files.copy(RAR5_CBR, cbrCopy);
|
||||
|
||||
CbxMetadataExtractor extractor = new CbxMetadataExtractor();
|
||||
CbxMetadataExtractor extractor = new CbxMetadataExtractor(new ArchiveService());
|
||||
BookMetadata metadata = extractor.extractMetadata(cbrCopy.toFile());
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Test RAR5 Comic");
|
||||
@@ -70,13 +60,13 @@ class Rar5FallbackIntegrationTest {
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(99L);
|
||||
BookRepository mockRepo = org.mockito.Mockito.mock(BookRepository.class);
|
||||
org.mockito.Mockito.when(mockRepo.findById(99L)).thenReturn(java.util.Optional.of(book));
|
||||
org.mockito.Mockito.when(mockRepo.findByIdWithBookFiles(99L)).thenReturn(java.util.Optional.of(book));
|
||||
|
||||
try (var fileUtilsStatic = org.mockito.Mockito.mockStatic(org.booklore.util.FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> org.booklore.util.FileUtils.getBookFullPath(book))
|
||||
.thenReturn(cbrCopy);
|
||||
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo);
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo, new ArchiveService());
|
||||
List<Integer> pages = readerService.getAvailablePages(99L);
|
||||
|
||||
assertThat(pages).hasSize(3);
|
||||
@@ -92,13 +82,15 @@ class Rar5FallbackIntegrationTest {
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(99L);
|
||||
BookRepository mockRepo = org.mockito.Mockito.mock(BookRepository.class);
|
||||
org.mockito.Mockito.when(mockRepo.findById(99L)).thenReturn(java.util.Optional.of(book));
|
||||
org.mockito.Mockito.when(mockRepo.findByIdWithBookFiles(99L)).thenReturn(java.util.Optional.of(book));
|
||||
|
||||
try (var fileUtilsStatic = org.mockito.Mockito.mockStatic(org.booklore.util.FileUtils.class)) {
|
||||
try (
|
||||
var fileUtilsStatic = org.mockito.Mockito.mockStatic(org.booklore.util.FileUtils.class)
|
||||
) {
|
||||
fileUtilsStatic.when(() -> org.booklore.util.FileUtils.getBookFullPath(book))
|
||||
.thenReturn(cbrCopy);
|
||||
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo);
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo, new ArchiveService());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
readerService.streamPageImage(99L, 1, out);
|
||||
|
||||
@@ -122,7 +114,7 @@ class Rar5FallbackIntegrationTest {
|
||||
meta.setTitle("Test RAR5 Comic");
|
||||
book.setMetadata(meta);
|
||||
|
||||
CbxConversionService conversionService = new CbxConversionService();
|
||||
CbxConversionService conversionService = new CbxConversionService(new ArchiveService());
|
||||
File epub = conversionService.convertCbxToEpub(cbrCopy.toFile(), tempDir.toFile(), book, 85);
|
||||
|
||||
assertThat(epub).exists();
|
||||
@@ -143,7 +135,7 @@ class Rar5FallbackIntegrationTest {
|
||||
// -- CbxMetadataWriter: loadFromRar + convertRarToZipArchive fallback --
|
||||
|
||||
@Test
|
||||
void metadataWriter_convertsRar5ToCbzViaFallback(@TempDir Path tempDir) throws Exception {
|
||||
void metadataWriter_convertsRar5ToCbz(@TempDir Path tempDir) throws Exception {
|
||||
Path cbrCopy = tempDir.resolve("test.cbr");
|
||||
Files.copy(RAR5_CBR, cbrCopy);
|
||||
|
||||
@@ -159,7 +151,7 @@ class Rar5FallbackIntegrationTest {
|
||||
appSettings.setMetadataPersistenceSettings(persistenceSettings);
|
||||
org.mockito.Mockito.when(mockSettings.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
CbxMetadataWriter writer = new CbxMetadataWriter(mockSettings);
|
||||
CbxMetadataWriter writer = new CbxMetadataWriter(mockSettings, new ArchiveService());
|
||||
|
||||
BookMetadataEntity metadata = new BookMetadataEntity();
|
||||
metadata.setTitle("Updated RAR5 Title");
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.booklore.service.fileprocessor;
|
||||
|
||||
import org.booklore.mapper.BookMapper;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.LibraryPathEntity;
|
||||
import org.booklore.repository.BookAdditionalFileRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.service.book.BookCreatorService;
|
||||
import org.booklore.service.metadata.MetadataMatchService;
|
||||
import org.booklore.service.metadata.extractor.CbxMetadataExtractor;
|
||||
import org.booklore.service.metadata.sidecar.SidecarMetadataWriter;
|
||||
import org.booklore.util.FileService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CbxProcessorTest {
|
||||
|
||||
@Mock private BookRepository bookRepository;
|
||||
@Mock private BookAdditionalFileRepository bookAdditionalFileRepository;
|
||||
@Mock private BookCreatorService bookCreatorService;
|
||||
@Mock private BookMapper bookMapper;
|
||||
@Mock private FileService fileService;
|
||||
@Mock private MetadataMatchService metadataMatchService;
|
||||
@Mock private SidecarMetadataWriter sidecarMetadataWriter;
|
||||
@Mock private CbxMetadataExtractor cbxMetadataExtractor;
|
||||
|
||||
private CbxProcessor cbxProcessor;
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
cbxProcessor = new CbxProcessor(
|
||||
bookRepository,
|
||||
bookAdditionalFileRepository,
|
||||
bookCreatorService,
|
||||
bookMapper,
|
||||
fileService,
|
||||
metadataMatchService,
|
||||
sidecarMetadataWriter,
|
||||
cbxMetadataExtractor
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCover_ZipNamedAsCbr_ShouldExtractImage() throws IOException {
|
||||
// Create a ZIP file named .cbr
|
||||
File zipAsCbr = tempDir.resolve("mismatched.cbr").toFile();
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipAsCbr))) {
|
||||
ZipEntry entry = new ZipEntry("cover.jpg");
|
||||
zos.putNextEntry(entry);
|
||||
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||
ImageIO.write(image, "jpg", zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
BookEntity bookEntity = new BookEntity();
|
||||
bookEntity.setId(1L);
|
||||
|
||||
// Create and configure the primary book file
|
||||
BookFileEntity bookFile = new BookFileEntity();
|
||||
bookFile.setFileName(zipAsCbr.getName());
|
||||
bookFile.setFileSubPath("");
|
||||
bookFile.setBook(bookEntity);
|
||||
bookEntity.getBookFiles().add(bookFile);
|
||||
LibraryPathEntity libPath = new LibraryPathEntity();
|
||||
libPath.setPath(tempDir.toString());
|
||||
bookEntity.setLibraryPath(libPath);
|
||||
bookEntity.setMetadata(new BookMetadataEntity());
|
||||
|
||||
when(fileService.saveCoverImages(any(BufferedImage.class), eq(1L))).thenReturn(true);
|
||||
|
||||
cbxProcessor.generateCover(bookEntity);
|
||||
|
||||
verify(fileService).saveCoverImages(any(BufferedImage.class), eq(1L));
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -3,13 +3,14 @@ package org.booklore.service.kobo;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import freemarker.template.TemplateException;
|
||||
import com.github.junrar.exception.RarException;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -24,6 +25,7 @@ import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@DisplayName("CBX Conversion Integration Test")
|
||||
@EnabledIf("org.booklore.service.ArchiveService#isAvailable")
|
||||
class CbxConversionIntegrationTest {
|
||||
|
||||
@TempDir
|
||||
@@ -33,12 +35,12 @@ class CbxConversionIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
conversionService = new CbxConversionService();
|
||||
conversionService = new CbxConversionService(new ArchiveService());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should successfully convert CBZ to EPUB with valid structure")
|
||||
void convertCbzToEpub_MainConversionTest() throws IOException, TemplateException, RarException {
|
||||
void convertCbzToEpub_MainConversionTest() throws IOException, TemplateException {
|
||||
File testCbzFile = createTestComicCbzFile();
|
||||
BookEntity bookMetadata = createTestBookMetadata();
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@ package org.booklore.service.kobo;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import freemarker.template.TemplateException;
|
||||
import com.github.junrar.exception.RarException;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -25,6 +26,7 @@ import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@DisplayName("CBX Conversion Service Tests")
|
||||
@EnabledIf("org.booklore.service.ArchiveService#isAvailable")
|
||||
class CbxConversionServiceTest {
|
||||
|
||||
@TempDir
|
||||
@@ -36,13 +38,13 @@ class CbxConversionServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
cbxConversionService = new CbxConversionService();
|
||||
cbxConversionService = new CbxConversionService(new ArchiveService());
|
||||
testCbzFile = createTestCbzFile();
|
||||
testBookEntity = createTestBookEntity();
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCbxToEpub_WithValidCbzFile_ShouldGenerateValidEpub() throws IOException, TemplateException, RarException {
|
||||
void convertCbxToEpub_WithValidCbzFile_ShouldGenerateValidEpub() throws IOException, TemplateException {
|
||||
File epubFile = cbxConversionService.convertCbxToEpub(testCbzFile, tempDir.toFile(), testBookEntity,85);
|
||||
|
||||
assertThat(epubFile).exists();
|
||||
@@ -94,7 +96,7 @@ class CbxConversionServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCbxToEpub_WithNullBookEntity_ShouldUseDefaultMetadata() throws IOException, TemplateException, RarException {
|
||||
void convertCbxToEpub_WithNullBookEntity_ShouldUseDefaultMetadata() throws IOException, TemplateException {
|
||||
File epubFile = cbxConversionService.convertCbxToEpub(testCbzFile, tempDir.toFile(), null,85);
|
||||
|
||||
assertThat(epubFile).exists();
|
||||
@@ -102,7 +104,7 @@ class CbxConversionServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCbxToEpub_WithMultipleImages_ShouldPreservePageOrder() throws IOException, TemplateException, RarException {
|
||||
void convertCbxToEpub_WithMultipleImages_ShouldPreservePageOrder() throws IOException, TemplateException {
|
||||
File multiPageCbzFile = createMultiPageCbzFile();
|
||||
|
||||
File epubFile = cbxConversionService.convertCbxToEpub(multiPageCbzFile, tempDir.toFile(), testBookEntity,85);
|
||||
@@ -112,7 +114,7 @@ class CbxConversionServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCbxToEpub_WithZipNamedAsCbr_ShouldGenerateValidEpub() throws IOException, TemplateException, RarException {
|
||||
void convertCbxToEpub_WithZipNamedAsCbr_ShouldGenerateValidEpub() throws IOException, TemplateException {
|
||||
File zipAsCbr = new File(tempDir.toFile(), "fake.cbr");
|
||||
try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(new FileOutputStream(zipAsCbr))) {
|
||||
BufferedImage testImage = createTestImage("Page 1", Color.RED);
|
||||
|
||||
+231
-356
File diff suppressed because it is too large
Load Diff
+4
-1
@@ -6,10 +6,12 @@ import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.MoodEntity;
|
||||
import org.booklore.model.entity.TagEntity;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
@@ -28,6 +30,7 @@ import java.util.zip.ZipOutputStream;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@EnabledIf("org.booklore.service.ArchiveService#isAvailable")
|
||||
class CbxComicInfoComplianceTest {
|
||||
|
||||
private CbxMetadataWriter writer;
|
||||
@@ -47,7 +50,7 @@ class CbxComicInfoComplianceTest {
|
||||
settings.setMetadataPersistenceSettings(persistence);
|
||||
Mockito.when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
|
||||
writer = new CbxMetadataWriter(appSettingService);
|
||||
writer = new CbxMetadataWriter(appSettingService, new ArchiveService());
|
||||
tempDir = Files.createTempDirectory("compliance_test_");
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -6,10 +6,12 @@ import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ComicCreatorRole;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.mockito.Mockito;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
@@ -34,6 +36,7 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@EnabledIf("org.booklore.service.ArchiveService#isAvailable")
|
||||
class CbxMetadataWriterTest {
|
||||
|
||||
private CbxMetadataWriter writer;
|
||||
@@ -53,7 +56,7 @@ class CbxMetadataWriterTest {
|
||||
settings.setMetadataPersistenceSettings(persistence);
|
||||
Mockito.when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
|
||||
writer = new CbxMetadataWriter(appSettingService);
|
||||
writer = new CbxMetadataWriter(appSettingService, new ArchiveService());
|
||||
tempDir = Files.createTempDirectory("cbx_writer_test_");
|
||||
}
|
||||
|
||||
@@ -260,13 +263,14 @@ class CbxMetadataWriterTest {
|
||||
@Test
|
||||
void saveMetadataToFile_ZipNamedAsCbr_ShouldUpdateMetadata() throws Exception {
|
||||
File zipAsCbr = createCbz(tempDir.resolve("mismatched.cbr"), new String[]{"page1.jpg"});
|
||||
Path zipAsCbz = tempDir.resolve("mismatched.cbz");
|
||||
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("Mismatched Title");
|
||||
|
||||
writer.saveMetadataToFile(zipAsCbr, meta, null, new MetadataClearFlags());
|
||||
|
||||
try (ZipFile zip = new ZipFile(zipAsCbr)) {
|
||||
try (ZipFile zip = new ZipFile(zipAsCbz.toFile())) {
|
||||
ZipEntry ci = zip.getEntry("ComicInfo.xml");
|
||||
assertNotNull(ci, "ComicInfo.xml should be present");
|
||||
Document doc = parseXml(zip.getInputStream(ci));
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
package org.booklore.service.reader;
|
||||
|
||||
import com.github.junrar.Archive;
|
||||
import com.github.junrar.rarfile.FileHeader;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.util.FileUtils;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -18,14 +12,12 @@ import org.mockito.*;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -36,6 +28,9 @@ class CbxReaderServiceTest {
|
||||
@Mock
|
||||
BookRepository bookRepository;
|
||||
|
||||
@Mock
|
||||
ArchiveService archiveService;
|
||||
|
||||
@InjectMocks
|
||||
CbxReaderService cbxReaderService;
|
||||
|
||||
@@ -44,289 +39,90 @@ class CbxReaderServiceTest {
|
||||
|
||||
BookEntity bookEntity;
|
||||
Path cbzPath;
|
||||
Path cb7Path;
|
||||
Path cbrPath;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
bookEntity = new BookEntity();
|
||||
bookEntity.setId(1L);
|
||||
cbzPath = Path.of("/tmp/test.cbz");
|
||||
cb7Path = Path.of("/tmp/test.cb7");
|
||||
cbrPath = Path.of("/tmp/test.cbr");
|
||||
Files.deleteIfExists(cbzPath);
|
||||
Files.deleteIfExists(cb7Path);
|
||||
Files.deleteIfExists(cbrPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CBZ_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
ZipArchiveEntry entry2 = new ZipArchiveEntry("2.png");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1, entry2));
|
||||
ZipFile zipFile = mock(ZipFile.class);
|
||||
when(zipFile.getEntries()).thenReturn(entries);
|
||||
|
||||
ZipFile.Builder builder = mock(ZipFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(cbzPath)).thenReturn(builder);
|
||||
when(builder.setCharset(any(Charset.class))).thenReturn(builder);
|
||||
when(builder.setUseUnicodeExtraFields(anyBoolean())).thenReturn(builder);
|
||||
when(builder.setIgnoreLocalFileHeader(anyBoolean())).thenReturn(builder);
|
||||
when(builder.get()).thenReturn(zipFile);
|
||||
|
||||
try (MockedStatic<ZipFile> zipFileStatic = mockStatic(ZipFile.class)) {
|
||||
zipFileStatic.when(ZipFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cbzPath);
|
||||
Files.setLastModifiedTime(cbzPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
List<Integer> pages = cbxReaderService.getAvailablePages(1L);
|
||||
assertEquals(List.of(1, 2), pages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CBZ_Fallback_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
// Unicode enabled -> throws Exception
|
||||
ZipFile zipFileFail = mock(ZipFile.class);
|
||||
when(zipFileFail.getEntries()).thenThrow(new IllegalArgumentException("Corrupt extra fields"));
|
||||
|
||||
// Unicode disabled -> returns valid entries
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
ZipFile zipFileSuccess = mock(ZipFile.class);
|
||||
when(zipFileSuccess.getEntries()).thenReturn(entries);
|
||||
|
||||
ZipFile.Builder builder = mock(ZipFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(any(Path.class))).thenReturn(builder);
|
||||
when(builder.setCharset(any(Charset.class))).thenReturn(builder);
|
||||
when(builder.setIgnoreLocalFileHeader(anyBoolean())).thenReturn(builder);
|
||||
|
||||
// Mock builder behavior based on UseUnicodeExtraFields call
|
||||
when(builder.setUseUnicodeExtraFields(anyBoolean())).thenAnswer(invocation -> {
|
||||
return builder;
|
||||
});
|
||||
|
||||
|
||||
when(builder.get())
|
||||
.thenReturn(zipFileFail) // 1. Fast, Unicode=True
|
||||
.thenReturn(zipFileFail) // 2. Slow, Unicode=True
|
||||
.thenReturn(zipFileSuccess); // 3. Fast, Unicode=False
|
||||
|
||||
try (MockedStatic<ZipFile> zipFileStatic = mockStatic(ZipFile.class)) {
|
||||
zipFileStatic.when(ZipFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cbzPath);
|
||||
Files.setLastModifiedTime(cbzPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
List<Integer> pages = cbxReaderService.getAvailablePages(1L);
|
||||
assertEquals(List.of(1), pages);
|
||||
|
||||
// Verify that we eventually called with useUnicode=false
|
||||
verify(builder).setUseUnicodeExtraFields(eq(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStreamPageImage_CBZ_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
ZipFile zipFile = mock(ZipFile.class);
|
||||
when(zipFile.getEntries()).thenReturn(entries);
|
||||
when(zipFile.getEntry("1.jpg")).thenReturn(entry1);
|
||||
when(zipFile.getInputStream(entry1)).thenReturn(new ByteArrayInputStream(new byte[]{1, 2, 3}));
|
||||
|
||||
ZipFile.Builder builder = mock(ZipFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(cbzPath)).thenReturn(builder);
|
||||
when(builder.setCharset(any(Charset.class))).thenReturn(builder);
|
||||
when(builder.setUseUnicodeExtraFields(anyBoolean())).thenReturn(builder);
|
||||
when(builder.setIgnoreLocalFileHeader(anyBoolean())).thenReturn(builder);
|
||||
when(builder.get()).thenReturn(zipFile);
|
||||
|
||||
try (MockedStatic<ZipFile> zipFileStatic = mockStatic(ZipFile.class)) {
|
||||
zipFileStatic.when(ZipFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cbzPath);
|
||||
Files.setLastModifiedTime(cbzPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
cbxReaderService.streamPageImage(1L, 1, out);
|
||||
assertArrayEquals(new byte[]{1, 2, 3}, out.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CBZ_ThrowsOnMissingBook() {
|
||||
void testGetAvailablePages_ThrowsOnMissingBook() {
|
||||
when(bookRepository.findByIdWithBookFiles(2L)).thenReturn(Optional.empty());
|
||||
assertThrows(ApiError.BOOK_NOT_FOUND.createException().getClass(), () -> cbxReaderService.getAvailablePages(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CB7_Success() throws Exception {
|
||||
void testGetAvailablePages_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cb7Path);
|
||||
when(archiveService.streamEntryNames(cbzPath)).then((i) -> Stream.of("1.jpg"));
|
||||
|
||||
SevenZArchiveEntry entry1 = mock(SevenZArchiveEntry.class);
|
||||
when(entry1.getName()).thenReturn("1.jpg");
|
||||
when(entry1.isDirectory()).thenReturn(false);
|
||||
try (
|
||||
MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class);
|
||||
MockedStatic<Files> filesStatic = mockStatic(Files.class);
|
||||
) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
filesStatic.when(() -> Files.getLastModifiedTime(cbzPath)).thenReturn(FileTime.from(Instant.now()));
|
||||
|
||||
SevenZFile sevenZFile = mock(SevenZFile.class);
|
||||
when(sevenZFile.getNextEntry()).thenReturn(entry1, (SevenZArchiveEntry) null);
|
||||
|
||||
SevenZFile.Builder builder = mock(SevenZFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(cb7Path)).thenReturn(builder);
|
||||
when(builder.get()).thenReturn(sevenZFile);
|
||||
|
||||
try (MockedStatic<SevenZFile> sevenZFileStatic = mockStatic(SevenZFile.class)) {
|
||||
sevenZFileStatic.when(SevenZFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cb7Path);
|
||||
Files.setLastModifiedTime(cb7Path, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
List<Integer> pages = cbxReaderService.getAvailablePages(1L);
|
||||
assertEquals(List.of(1), pages);
|
||||
}
|
||||
List<Integer> pages = cbxReaderService.getAvailablePages(1L);
|
||||
assertEquals(List.of(1), pages);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CBR_Success() throws Exception {
|
||||
void testStreamPageImage_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath);
|
||||
when(archiveService.streamEntryNames(cbzPath)).then((i) -> Stream.of("1.jpg"));
|
||||
when(
|
||||
archiveService.transferEntryTo(eq(cbzPath), eq("1.jpg"), any())
|
||||
).then((i) -> {i.getArgument(2, OutputStream.class).write(new byte[]{1, 2, 3}); return null; });
|
||||
try (
|
||||
MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class);
|
||||
MockedStatic<Files> filesStatic = mockStatic(Files.class);
|
||||
) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
filesStatic.when(() -> Files.getLastModifiedTime(cbzPath)).thenReturn(FileTime.from(Instant.now()));
|
||||
|
||||
FileHeader header = mock(FileHeader.class);
|
||||
when(header.isDirectory()).thenReturn(false);
|
||||
when(header.getFileName()).thenReturn("1.jpg");
|
||||
|
||||
try (MockedConstruction<Archive> ignored = mockConstruction(Archive.class, (mock, context) -> {
|
||||
when(mock.getFileHeaders()).thenReturn(List.of(header));
|
||||
})) {
|
||||
Files.deleteIfExists(cbrPath);
|
||||
Files.createFile(cbrPath);
|
||||
Files.setLastModifiedTime(cbrPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
List<Integer> pages = cbxReaderService.getAvailablePages(1L);
|
||||
assertEquals(List.of(1), pages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStreamPageImage_CBR_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath);
|
||||
|
||||
FileHeader header = mock(FileHeader.class);
|
||||
when(header.isDirectory()).thenReturn(false);
|
||||
when(header.getFileName()).thenReturn("1.jpg");
|
||||
|
||||
try (MockedConstruction<Archive> ignored = mockConstruction(Archive.class, (mock, context) -> {
|
||||
when(mock.getFileHeaders()).thenReturn(List.of(header));
|
||||
doAnswer(invocation -> {
|
||||
OutputStream out = invocation.getArgument(1);
|
||||
out.write(new byte[]{1, 2, 3});
|
||||
return null;
|
||||
}).when(mock).extractFile(eq(header), any(OutputStream.class));
|
||||
})) {
|
||||
Files.deleteIfExists(cbrPath);
|
||||
Files.createFile(cbrPath);
|
||||
Files.setLastModifiedTime(cbrPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
cbxReaderService.streamPageImage(1L, 1, out);
|
||||
assertArrayEquals(new byte[]{1, 2, 3}, out.toByteArray());
|
||||
}
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
cbxReaderService.streamPageImage(1L, 1, out);
|
||||
assertArrayEquals(new byte[]{1, 2, 3}, out.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStreamPageImage_PageOutOfRange_Throws() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
when(archiveService.streamEntryNames(cbzPath)).then((i) -> Stream.of("1.jpg"));
|
||||
try (
|
||||
MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class);
|
||||
MockedStatic<Files> filesStatic = mockStatic(Files.class);
|
||||
) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
filesStatic.when(() -> Files.getLastModifiedTime(cbzPath)).thenReturn(FileTime.from(Instant.now()));
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
ZipFile zipFile = mock(ZipFile.class);
|
||||
when(zipFile.getEntries()).thenReturn(entries);
|
||||
|
||||
ZipFile.Builder builder = mock(ZipFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(cbzPath)).thenReturn(builder);
|
||||
when(builder.setCharset(any(Charset.class))).thenReturn(builder);
|
||||
when(builder.setUseUnicodeExtraFields(anyBoolean())).thenReturn(builder);
|
||||
when(builder.setIgnoreLocalFileHeader(anyBoolean())).thenReturn(builder);
|
||||
when(builder.get()).thenReturn(zipFile);
|
||||
|
||||
try (MockedStatic<ZipFile> zipFileStatic = mockStatic(ZipFile.class)) {
|
||||
zipFileStatic.when(ZipFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cbzPath);
|
||||
Files.setLastModifiedTime(cbzPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
assertThrows(FileNotFoundException.class, () -> cbxReaderService.streamPageImage(1L, 2, new ByteArrayOutputStream()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_UnsupportedArchive_Throws() throws Exception {
|
||||
Path unknownPath = Path.of("/tmp/test.unknown");
|
||||
Files.deleteIfExists(unknownPath);
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(unknownPath);
|
||||
|
||||
Files.createFile(unknownPath);
|
||||
Files.setLastModifiedTime(unknownPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
assertThrows(APIException.class, () -> cbxReaderService.getAvailablePages(1L));
|
||||
assertThrows(
|
||||
FileNotFoundException.class,
|
||||
() -> cbxReaderService.streamPageImage(1L, 2, new ByteArrayOutputStream())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStreamPageImage_EntryNotFound_Throws() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
when(archiveService.streamEntryNames(cbzPath)).then((i) -> Stream.of("1.jpg"));
|
||||
try (
|
||||
MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class);
|
||||
MockedStatic<Files> filesStatic = mockStatic(Files.class);
|
||||
) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
filesStatic.when(() -> Files.getLastModifiedTime(cbzPath)).thenReturn(FileTime.from(Instant.now()));
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
ZipFile zipFile = mock(ZipFile.class);
|
||||
when(zipFile.getEntries()).thenReturn(entries);
|
||||
|
||||
ZipFile.Builder builder = mock(ZipFile.Builder.class, RETURNS_DEEP_STUBS);
|
||||
when(builder.setPath(cbzPath)).thenReturn(builder);
|
||||
when(builder.setCharset(any(Charset.class))).thenReturn(builder);
|
||||
when(builder.setUseUnicodeExtraFields(anyBoolean())).thenReturn(builder);
|
||||
when(builder.setIgnoreLocalFileHeader(anyBoolean())).thenReturn(builder);
|
||||
when(builder.get()).thenReturn(zipFile);
|
||||
|
||||
try (MockedStatic<ZipFile> zipFileStatic = mockStatic(ZipFile.class)) {
|
||||
zipFileStatic.when(ZipFile::builder).thenReturn(builder);
|
||||
|
||||
Files.createFile(cbzPath);
|
||||
Files.setLastModifiedTime(cbzPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
|
||||
assertThrows(IOException.class, () -> cbxReaderService.streamPageImage(1L, 2, new ByteArrayOutputStream()));
|
||||
}
|
||||
assertThrows(
|
||||
FileNotFoundException.class,
|
||||
() -> cbxReaderService.streamPageImage(1L, 2, new ByteArrayOutputStream())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
package org.booklore.util;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assumptions.assumeThat;
|
||||
|
||||
class UnrarHelperIntegrationTest {
|
||||
|
||||
private static final Path RAR5_CBR = Path.of("src/test/resources/cbx/test-rar5.cbr");
|
||||
|
||||
@BeforeAll
|
||||
static void checkUnrarAvailable() {
|
||||
assumeThat(UnrarHelper.isAvailable())
|
||||
.as("unrar binary must be on PATH to run these tests")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEntries_returnsAllEntriesFromRar5Archive() throws IOException {
|
||||
List<String> entries = UnrarHelper.listEntries(RAR5_CBR);
|
||||
|
||||
assertThat(entries).containsExactly(
|
||||
"ComicInfo.xml",
|
||||
"page_001.jpg",
|
||||
"page_002.jpg",
|
||||
"page_003.jpg"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractEntryBytes_extractsComicInfoXml() throws IOException {
|
||||
byte[] bytes = UnrarHelper.extractEntryBytes(RAR5_CBR, "ComicInfo.xml");
|
||||
String xml = new String(bytes);
|
||||
|
||||
assertThat(xml).contains("<Title>Test RAR5 Comic</Title>");
|
||||
assertThat(xml).contains("<Series>RAR5 Test Series</Series>");
|
||||
assertThat(xml).contains("<Writer>Test Author</Writer>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractEntry_streamsImageToOutputStream() throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
UnrarHelper.extractEntry(RAR5_CBR, "page_001.jpg", out);
|
||||
|
||||
byte[] imageBytes = out.toByteArray();
|
||||
assertThat(imageBytes).hasSizeGreaterThan(0);
|
||||
assertThat(imageBytes[0]).isEqualTo((byte) 0xFF);
|
||||
assertThat(imageBytes[1]).isEqualTo((byte) 0xD8);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractAll_extractsAllFilesToDirectory(@TempDir Path tempDir) throws IOException {
|
||||
UnrarHelper.extractAll(RAR5_CBR, tempDir);
|
||||
|
||||
assertThat(tempDir.resolve("ComicInfo.xml")).exists();
|
||||
assertThat(tempDir.resolve("page_001.jpg")).exists();
|
||||
assertThat(tempDir.resolve("page_002.jpg")).exists();
|
||||
assertThat(tempDir.resolve("page_003.jpg")).exists();
|
||||
|
||||
String xml = Files.readString(tempDir.resolve("ComicInfo.xml"));
|
||||
assertThat(xml).contains("<Title>Test RAR5 Comic</Title>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEntries_throwsForNonExistentFile() {
|
||||
Path bogus = Path.of("/tmp/does-not-exist.cbr");
|
||||
|
||||
assertThatThrownBy(() -> UnrarHelper.listEntries(bogus))
|
||||
.isInstanceOf(IOException.class)
|
||||
.hasMessageContaining("unrar list failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractEntryBytes_throwsForNonExistentEntry() {
|
||||
assertThatThrownBy(() -> UnrarHelper.extractEntryBytes(RAR5_CBR, "no-such-file.jpg"))
|
||||
.isInstanceOf(IOException.class)
|
||||
.hasMessageContaining("unrar extract failed");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user