feat(cbx-reader): enhance ZIP archive handling with Unicode support and fallback mechanisms (#2593)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package org.booklore.service.reader;
|
||||
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.util.FileUtils;
|
||||
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.ApiError;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.util.FileUtils;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -89,6 +89,53 @@ class CbxReaderServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailablePages_CBZ_Fallback_Success() throws Exception {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
|
||||
// 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.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
Reference in New Issue
Block a user