Bidirectional progress sync between koreader and booklore reader (#2312)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+331
@@ -0,0 +1,331 @@
|
||||
package com.adityachandel.booklore.util.epub;
|
||||
|
||||
import io.documentnode.epub4j.domain.Book;
|
||||
import io.documentnode.epub4j.domain.Resource;
|
||||
import io.documentnode.epub4j.epub.EpubWriter;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EpubContentReaderTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private File validEpubFile;
|
||||
private File emptySpineEpubFile;
|
||||
|
||||
private static final String CHAPTER1_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 1</title></head>
|
||||
<body>
|
||||
<h1>Chapter 1</h1>
|
||||
<p>This is the first paragraph of chapter one.</p>
|
||||
<p>This is the second paragraph.</p>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
private static final String CHAPTER2_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 2</title></head>
|
||||
<body>
|
||||
<h1>Chapter 2</h1>
|
||||
<p>Content of chapter two.</p>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
private static final String CHAPTER3_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 3</title></head>
|
||||
<body>
|
||||
<h1>Chapter 3</h1>
|
||||
<p>Final chapter content.</p>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
validEpubFile = createTestEpub("valid.epub", true);
|
||||
emptySpineEpubFile = createTestEpub("empty_spine.epub", false);
|
||||
}
|
||||
|
||||
private File createTestEpub(String filename, boolean withSpine) throws IOException {
|
||||
Book book = new Book();
|
||||
book.getMetadata().addTitle("Test Book");
|
||||
book.getMetadata().addAuthor(new io.documentnode.epub4j.domain.Author("Test Author"));
|
||||
|
||||
if (withSpine) {
|
||||
Resource chapter1 = new Resource(CHAPTER1_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter1.xhtml");
|
||||
Resource chapter2 = new Resource(CHAPTER2_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter2.xhtml");
|
||||
Resource chapter3 = new Resource(CHAPTER3_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter3.xhtml");
|
||||
|
||||
book.addSection("Chapter 1", chapter1);
|
||||
book.addSection("Chapter 2", chapter2);
|
||||
book.addSection("Chapter 3", chapter3);
|
||||
}
|
||||
|
||||
File epubFile = tempDir.resolve(filename).toFile();
|
||||
try (FileOutputStream out = new FileOutputStream(epubFile)) {
|
||||
new EpubWriter().write(book, out);
|
||||
}
|
||||
return epubFile;
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetSpineItemContentTests {
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_firstChapter_returnsContent() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile, 0);
|
||||
|
||||
assertNotNull(content);
|
||||
assertTrue(content.contains("Chapter 1"));
|
||||
assertTrue(content.contains("first paragraph"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_secondChapter_returnsContent() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile, 1);
|
||||
|
||||
assertNotNull(content);
|
||||
assertTrue(content.contains("Chapter 2"));
|
||||
assertTrue(content.contains("chapter two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_lastChapter_returnsContent() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile, 2);
|
||||
|
||||
assertNotNull(content);
|
||||
assertTrue(content.contains("Chapter 3"));
|
||||
assertTrue(content.contains("Final chapter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_withPath_returnsContent() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile.toPath(), 0);
|
||||
|
||||
assertNotNull(content);
|
||||
assertTrue(content.contains("Chapter 1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_negativeIndex_throwsException() {
|
||||
EpubContentReader.EpubReadException exception = assertThrows(
|
||||
EpubContentReader.EpubReadException.class,
|
||||
() -> EpubContentReader.getSpineItemContent(validEpubFile, -1)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("out of bounds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_indexTooLarge_throwsException() {
|
||||
EpubContentReader.EpubReadException exception = assertThrows(
|
||||
EpubContentReader.EpubReadException.class,
|
||||
() -> EpubContentReader.getSpineItemContent(validEpubFile, 999)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("out of bounds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_emptySpine_throwsException() {
|
||||
EpubContentReader.EpubReadException exception = assertThrows(
|
||||
EpubContentReader.EpubReadException.class,
|
||||
() -> EpubContentReader.getSpineItemContent(emptySpineEpubFile, 0)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("no spine"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_nonExistentFile_throwsException() {
|
||||
File nonExistent = new File(tempDir.toFile(), "nonexistent.epub");
|
||||
|
||||
assertThrows(
|
||||
EpubContentReader.EpubReadException.class,
|
||||
() -> EpubContentReader.getSpineItemContent(nonExistent, 0)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_invalidEpubFile_throwsException() throws IOException {
|
||||
File invalidFile = tempDir.resolve("invalid.epub").toFile();
|
||||
java.nio.file.Files.writeString(invalidFile.toPath(), "not an epub");
|
||||
|
||||
assertThrows(
|
||||
EpubContentReader.EpubReadException.class,
|
||||
() -> EpubContentReader.getSpineItemContent(invalidFile, 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetSpineSizeTests {
|
||||
|
||||
@Test
|
||||
void getSpineSize_validEpub_returnsCorrectSize() {
|
||||
int size = EpubContentReader.getSpineSize(validEpubFile);
|
||||
|
||||
assertEquals(3, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineSize_emptySpine_returnsZero() {
|
||||
int size = EpubContentReader.getSpineSize(emptySpineEpubFile);
|
||||
|
||||
assertEquals(0, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineSize_nonExistentFile_returnsZero() {
|
||||
File nonExistent = new File(tempDir.toFile(), "nonexistent.epub");
|
||||
|
||||
int size = EpubContentReader.getSpineSize(nonExistent);
|
||||
|
||||
assertEquals(0, size);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetSpineItemHrefTests {
|
||||
|
||||
@Test
|
||||
void getSpineItemHref_validIndex_returnsHref() {
|
||||
String href = EpubContentReader.getSpineItemHref(validEpubFile, 0);
|
||||
|
||||
assertNotNull(href);
|
||||
assertTrue(href.contains("chapter1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemHref_secondItem_returnsCorrectHref() {
|
||||
String href = EpubContentReader.getSpineItemHref(validEpubFile, 1);
|
||||
|
||||
assertNotNull(href);
|
||||
assertTrue(href.contains("chapter2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemHref_negativeIndex_returnsNull() {
|
||||
String href = EpubContentReader.getSpineItemHref(validEpubFile, -1);
|
||||
|
||||
assertNull(href);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemHref_indexTooLarge_returnsNull() {
|
||||
String href = EpubContentReader.getSpineItemHref(validEpubFile, 999);
|
||||
|
||||
assertNull(href);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemHref_emptySpine_returnsNull() {
|
||||
String href = EpubContentReader.getSpineItemHref(emptySpineEpubFile, 0);
|
||||
|
||||
assertNull(href);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetAllSpineItemHrefsTests {
|
||||
|
||||
@Test
|
||||
void getAllSpineItemHrefs_validEpub_returnsAllHrefs() {
|
||||
List<String> hrefs = EpubContentReader.getAllSpineItemHrefs(validEpubFile);
|
||||
|
||||
assertEquals(3, hrefs.size());
|
||||
assertTrue(hrefs.get(0).contains("chapter1"));
|
||||
assertTrue(hrefs.get(1).contains("chapter2"));
|
||||
assertTrue(hrefs.get(2).contains("chapter3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllSpineItemHrefs_emptySpine_returnsEmptyList() {
|
||||
List<String> hrefs = EpubContentReader.getAllSpineItemHrefs(emptySpineEpubFile);
|
||||
|
||||
assertTrue(hrefs.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllSpineItemHrefs_nonExistentFile_returnsEmptyList() {
|
||||
File nonExistent = new File(tempDir.toFile(), "nonexistent.epub");
|
||||
|
||||
List<String> hrefs = EpubContentReader.getAllSpineItemHrefs(nonExistent);
|
||||
|
||||
assertTrue(hrefs.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class EpubReadExceptionTests {
|
||||
|
||||
@Test
|
||||
void epubReadException_withMessage_containsMessage() {
|
||||
EpubContentReader.EpubReadException exception =
|
||||
new EpubContentReader.EpubReadException("Test message");
|
||||
|
||||
assertEquals("Test message", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void epubReadException_withCause_containsCause() {
|
||||
IOException cause = new IOException("IO error");
|
||||
EpubContentReader.EpubReadException exception =
|
||||
new EpubContentReader.EpubReadException("Test message", cause);
|
||||
|
||||
assertEquals("Test message", exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ContentValidationTests {
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_returnsUtf8Content() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile, 0);
|
||||
|
||||
assertNotNull(content);
|
||||
assertFalse(content.isEmpty());
|
||||
assertTrue(content.contains("<body>") || content.contains("<BODY>") || content.toLowerCase().contains("<body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_preservesHtmlStructure() {
|
||||
String content = EpubContentReader.getSpineItemContent(validEpubFile, 0);
|
||||
|
||||
assertTrue(content.contains("<h1>") || content.toLowerCase().contains("<h1"));
|
||||
assertTrue(content.contains("<p>") || content.toLowerCase().contains("<p"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSpineItemContent_multipleReadsReturnSameContent() {
|
||||
String content1 = EpubContentReader.getSpineItemContent(validEpubFile, 0);
|
||||
String content2 = EpubContentReader.getSpineItemContent(validEpubFile, 0);
|
||||
|
||||
assertEquals(content1, content2);
|
||||
}
|
||||
}
|
||||
}
|
||||
+474
@@ -0,0 +1,474 @@
|
||||
package com.adityachandel.booklore.util.koreader;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CfiConvertorTest {
|
||||
|
||||
private Document simpleDocument;
|
||||
private Document complexDocument;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Simple HTML document for basic tests
|
||||
simpleDocument = Jsoup.parse("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<div id="chapter1">
|
||||
<p>First paragraph with some text.</p>
|
||||
<p>Second paragraph with more text.</p>
|
||||
</div>
|
||||
<div id="chapter2">
|
||||
<p>Third paragraph.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""");
|
||||
|
||||
// Complex HTML document with nested elements
|
||||
complexDocument = Jsoup.parse("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<section>
|
||||
<div class="content">
|
||||
<h1>Chapter Title</h1>
|
||||
<p>This is <em>emphasized</em> and <strong>bold</strong> text.</p>
|
||||
<p>Another paragraph with <span class="highlight">highlighted</span> content.</p>
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<p>Sidebar content here.</p>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<div>
|
||||
<p>Second section paragraph.</p>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
||||
""");
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ExtractSpineIndexTests {
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromCfi_spineIndex0() {
|
||||
String cfi = "epubcfi(/6/2!/4/2)";
|
||||
int result = CfiConvertor.extractSpineIndex(cfi);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromCfi_spineIndex1() {
|
||||
String cfi = "epubcfi(/6/4!/4/2)";
|
||||
int result = CfiConvertor.extractSpineIndex(cfi);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromCfi_spineIndex5() {
|
||||
String cfi = "epubcfi(/6/12!/4/2:10)";
|
||||
int result = CfiConvertor.extractSpineIndex(cfi);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromXPointer_spineIndex0() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p";
|
||||
int result = CfiConvertor.extractSpineIndex(xpointer);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromXPointer_spineIndex2() {
|
||||
String xpointer = "/body/DocFragment[3]/body/section/div/p";
|
||||
int result = CfiConvertor.extractSpineIndex(xpointer);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromXPointer_withTextOffset() {
|
||||
String xpointer = "/body/DocFragment[5]/body/p/text().42";
|
||||
int result = CfiConvertor.extractSpineIndex(xpointer);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
@ValueSource(strings = {" ", "\t", "\n"})
|
||||
void extractSpineIndex_nullOrEmpty_throwsException(String input) {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> CfiConvertor.extractSpineIndex(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_invalidFormat_throwsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> CfiConvertor.extractSpineIndex("invalid/path/here"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_malformedCfi_throwsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> CfiConvertor.extractSpineIndex("epubcfi(malformed)"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class XPointerToCfiTests {
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_simpleElement() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
assertTrue(cfi.endsWith(")"));
|
||||
assertTrue(cfi.contains("/6/2!")); // Spine index 0 = step 2
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_withTextOffset() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().5";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
assertTrue(cfi.contains(":5")); // Text offset
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_differentSpineIndex() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 2);
|
||||
String xpointer = "/body/DocFragment[3]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.contains("/6/6!")); // Spine index 2 = step 6
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_bodyOnly() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_rangeWithStartAndEnd() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String startXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().0";
|
||||
String endXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().10";
|
||||
|
||||
String cfi = converter.xPointerToCfi(startXPointer, endXPointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_invalidPath_throwsException() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> converter.xPointerToCfi("/invalid/path"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class CfiToXPointerTests {
|
||||
|
||||
@Test
|
||||
void cfiToXPointer_simpleElement() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult result = converter.cfiToXPointer(cfi);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getXpointer());
|
||||
assertTrue(result.getXpointer().startsWith("/body/DocFragment[1]/body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cfiToXPointer_withTextOffset() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/2!/4/2/2:5)";
|
||||
|
||||
CfiConvertor.XPointerResult result = converter.cfiToXPointer(cfi);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.getXpointer().contains("/text()."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cfiToXPointer_spineIndexMismatch_throwsException() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/4!/4/2)"; // Spine index 1, but converter is 0
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> converter.cfiToXPointer(cfi));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cfiToXPointer_invalidCfiFormat_throwsException() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> converter.cfiToXPointer("not-a-valid-cfi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cfiToXPointer_malformedCfi_throwsException() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> converter.cfiToXPointer("epubcfi(invalid)"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ValidationTests {
|
||||
|
||||
@Test
|
||||
void validateCfi_validCfi_returnsTrue() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/2!/4/2)";
|
||||
|
||||
assertTrue(converter.validateCfi(cfi));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCfi_invalidCfi_returnsFalse() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
|
||||
assertFalse(converter.validateCfi("invalid"));
|
||||
assertFalse(converter.validateCfi("epubcfi(malformed)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCfi_wrongSpineIndex_returnsFalse() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/10!/4/2)"; // Spine index 4, converter is 0
|
||||
|
||||
assertFalse(converter.validateCfi(cfi));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateXPointer_validXPointer_returnsTrue() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
assertTrue(converter.validateXPointer(xpointer));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateXPointer_invalidXPointer_returnsFalse() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
|
||||
assertFalse(converter.validateXPointer("/invalid/path"));
|
||||
assertFalse(converter.validateXPointer("not-an-xpointer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateXPointer_withPos1_returnsTrue() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String pos0 = "/body/DocFragment[1]/body/div[1]/p[1]/text().0";
|
||||
String pos1 = "/body/DocFragment[1]/body/div[1]/p[1]/text().10";
|
||||
|
||||
assertTrue(converter.validateXPointer(pos0, pos1));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NormalizeProgressXPointerTests {
|
||||
|
||||
@Test
|
||||
void normalizeProgressXPointer_removesTextOffset() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p/text().42";
|
||||
|
||||
String result = CfiConvertor.normalizeProgressXPointer(xpointer);
|
||||
|
||||
assertEquals("/body/DocFragment[1]/body/div/p", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeProgressXPointer_removesNodeSuffix() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p.5";
|
||||
|
||||
String result = CfiConvertor.normalizeProgressXPointer(xpointer);
|
||||
|
||||
assertEquals("/body/DocFragment[1]/body/div/p", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeProgressXPointer_removesBothTextOffsetAndNodeSuffix() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p/text().10.5";
|
||||
|
||||
String result = CfiConvertor.normalizeProgressXPointer(xpointer);
|
||||
|
||||
assertEquals("/body/DocFragment[1]/body/div/p", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeProgressXPointer_noChangesNeeded() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p";
|
||||
|
||||
String result = CfiConvertor.normalizeProgressXPointer(xpointer);
|
||||
|
||||
assertEquals("/body/DocFragment[1]/body/div/p", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeProgressXPointer_nullInput_returnsNull() {
|
||||
assertNull(CfiConvertor.normalizeProgressXPointer(null));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"/body/DocFragment[1]/body/text().0, /body/DocFragment[1]/body",
|
||||
"/body/DocFragment[3]/body/section/div/p/text().100, /body/DocFragment[3]/body/section/div/p",
|
||||
"/body/DocFragment[1]/body/div[2]/p[3]/text().5, /body/DocFragment[1]/body/div[2]/p[3]"
|
||||
})
|
||||
void normalizeProgressXPointer_variousInputs(String input, String expected) {
|
||||
assertEquals(expected, CfiConvertor.normalizeProgressXPointer(input));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ComplexDocumentTests {
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_nestedElements() {
|
||||
CfiConvertor converter = new CfiConvertor(complexDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/section[1]/div[1]/p[1]";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_deeplyNestedElement() {
|
||||
CfiConvertor converter = new CfiConvertor(complexDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/section[1]/div[1]/h1";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_secondSection() {
|
||||
CfiConvertor converter = new CfiConvertor(complexDocument, 0);
|
||||
String xpointer = "/body/DocFragment[1]/body/section[2]/div/p";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class RoundTripTests {
|
||||
|
||||
@Test
|
||||
void roundTrip_xPointerToCfiAndBack() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String originalXPointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = converter.xPointerToCfi(originalXPointer);
|
||||
CfiConvertor.XPointerResult result = converter.cfiToXPointer(cfi);
|
||||
|
||||
assertNotNull(result.getXpointer());
|
||||
// The path should resolve to the same element, though format may differ
|
||||
assertTrue(result.getXpointer().contains("/body/DocFragment[1]/body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTrip_cfiToXPointerAndBack() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String originalCfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult xpointerResult = converter.cfiToXPointer(originalCfi);
|
||||
String cfi = converter.xPointerToCfi(xpointerResult.getXpointer());
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
assertTrue(cfi.contains("/6/2!")); // Same spine index
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class EdgeCaseTests {
|
||||
|
||||
@Test
|
||||
void constructor_withDefaultSpineIndex() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument);
|
||||
|
||||
// Should work with spine index 0
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.contains("/6/2!")); // Default spine index 0
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_elementWithoutIndex() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
// Using tag without index (implicit [1])
|
||||
String xpointer = "/body/DocFragment[1]/body/div/p";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerToCfi_largeSpineIndex() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 99);
|
||||
String xpointer = "/body/DocFragment[100]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = converter.xPointerToCfi(xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.contains("/6/200!")); // Spine index 99 = step 200
|
||||
}
|
||||
|
||||
@Test
|
||||
void xPointerResult_allFieldsPopulated() {
|
||||
CfiConvertor converter = new CfiConvertor(simpleDocument, 0);
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult result = converter.cfiToXPointer(cfi);
|
||||
|
||||
assertNotNull(result.getXpointer());
|
||||
assertNotNull(result.getPos0());
|
||||
assertNotNull(result.getPos1());
|
||||
}
|
||||
}
|
||||
}
|
||||
+411
@@ -0,0 +1,411 @@
|
||||
package com.adityachandel.booklore.util.koreader;
|
||||
|
||||
import io.documentnode.epub4j.domain.Author;
|
||||
import io.documentnode.epub4j.domain.Book;
|
||||
import io.documentnode.epub4j.domain.Resource;
|
||||
import io.documentnode.epub4j.epub.EpubWriter;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EpubCfiServiceTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private EpubCfiService service;
|
||||
private File testEpubFile;
|
||||
|
||||
private static final String CHAPTER1_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 1</title></head>
|
||||
<body>
|
||||
<div id="chapter1">
|
||||
<h1>Chapter 1</h1>
|
||||
<p>First paragraph with some text content.</p>
|
||||
<p>Second paragraph with more text.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
private static final String CHAPTER2_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 2</title></head>
|
||||
<body>
|
||||
<div id="chapter2">
|
||||
<h1>Chapter 2</h1>
|
||||
<p>Content of chapter two.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
private static final String CHAPTER3_CONTENT = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Chapter 3</title></head>
|
||||
<body>
|
||||
<div id="chapter3">
|
||||
<h1>Chapter 3</h1>
|
||||
<p>Final chapter content here.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
service = new EpubCfiService();
|
||||
testEpubFile = createTestEpub("test.epub");
|
||||
}
|
||||
|
||||
private File createTestEpub(String filename) throws IOException {
|
||||
Book book = new Book();
|
||||
book.getMetadata().addTitle("Test Book");
|
||||
book.getMetadata().addAuthor(new Author("Test Author"));
|
||||
|
||||
Resource chapter1 = new Resource(CHAPTER1_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter1.xhtml");
|
||||
Resource chapter2 = new Resource(CHAPTER2_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter2.xhtml");
|
||||
Resource chapter3 = new Resource(CHAPTER3_CONTENT.getBytes(StandardCharsets.UTF_8), "chapter3.xhtml");
|
||||
|
||||
book.addSection("Chapter 1", chapter1);
|
||||
book.addSection("Chapter 2", chapter2);
|
||||
book.addSection("Chapter 3", chapter3);
|
||||
|
||||
File epubFile = tempDir.resolve(filename).toFile();
|
||||
try (FileOutputStream out = new FileOutputStream(epubFile)) {
|
||||
new EpubWriter().write(book, out);
|
||||
}
|
||||
return epubFile;
|
||||
}
|
||||
|
||||
@Nested
|
||||
class CreateConverterTests {
|
||||
|
||||
@Test
|
||||
void createConverter_validFile_returnsConverter() {
|
||||
CfiConvertor converter = service.createConverter(testEpubFile, 0);
|
||||
|
||||
assertNotNull(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createConverter_withPath_returnsConverter() {
|
||||
CfiConvertor converter = service.createConverter(testEpubFile.toPath(), 0);
|
||||
|
||||
assertNotNull(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createConverter_differentSpineIndices_returnsDifferentConverters() {
|
||||
CfiConvertor converter0 = service.createConverter(testEpubFile, 0);
|
||||
CfiConvertor converter1 = service.createConverter(testEpubFile, 1);
|
||||
|
||||
assertNotNull(converter0);
|
||||
assertNotNull(converter1);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ConvertXPointerToCfiTests {
|
||||
|
||||
@Test
|
||||
void convertXPointerToCfi_validXPointer_returnsCfi() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile, xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
assertTrue(cfi.endsWith(")"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertXPointerToCfi_withPath_returnsCfi() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile.toPath(), xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertXPointerToCfi_secondSpine_returnsCfiWithCorrectSpineIndex() {
|
||||
String xpointer = "/body/DocFragment[2]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile, xpointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.contains("/6/4!"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ConvertCfiToXPointerTests {
|
||||
|
||||
@Test
|
||||
void convertCfiToXPointer_validCfi_returnsXPointerResult() {
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult result = service.convertCfiToXPointer(testEpubFile, cfi);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getXpointer());
|
||||
assertTrue(result.getXpointer().startsWith("/body/DocFragment[1]/body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCfiToXPointer_withPath_returnsXPointerResult() {
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult result = service.convertCfiToXPointer(testEpubFile.toPath(), cfi);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getXpointer());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ConvertXPointerRangeToCfiTests {
|
||||
|
||||
@Test
|
||||
void convertXPointerRangeToCfi_validRange_returnsCfi() {
|
||||
String startXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().0";
|
||||
String endXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().10";
|
||||
|
||||
String cfi = service.convertXPointerRangeToCfi(testEpubFile, startXPointer, endXPointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertXPointerRangeToCfi_withPath_returnsCfi() {
|
||||
String startXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().0";
|
||||
String endXPointer = "/body/DocFragment[1]/body/div[1]/p[1]/text().10";
|
||||
|
||||
String cfi = service.convertXPointerRangeToCfi(testEpubFile.toPath(), startXPointer, endXPointer);
|
||||
|
||||
assertNotNull(cfi);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ConvertCfiToProgressXPointerTests {
|
||||
|
||||
@Test
|
||||
void convertCfiToProgressXPointer_validCfi_returnsNormalizedXPointer() {
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
String progressXPointer = service.convertCfiToProgressXPointer(testEpubFile, cfi);
|
||||
|
||||
assertNotNull(progressXPointer);
|
||||
assertFalse(progressXPointer.contains("/text()."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCfiToProgressXPointer_withPath_returnsNormalizedXPointer() {
|
||||
String cfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
String progressXPointer = service.convertCfiToProgressXPointer(testEpubFile.toPath(), cfi);
|
||||
|
||||
assertNotNull(progressXPointer);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ValidationTests {
|
||||
|
||||
@Test
|
||||
void validateCfi_validCfi_returnsTrue() {
|
||||
String cfi = "epubcfi(/6/2!/4/2)";
|
||||
|
||||
boolean result = service.validateCfi(testEpubFile, cfi);
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCfi_invalidCfi_returnsFalse() {
|
||||
boolean result = service.validateCfi(testEpubFile, "invalid-cfi");
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCfi_malformedCfi_returnsFalse() {
|
||||
boolean result = service.validateCfi(testEpubFile, "epubcfi(malformed)");
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateXPointer_validXPointer_returnsTrue() {
|
||||
String xpointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
boolean result = service.validateXPointer(testEpubFile, xpointer);
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateXPointer_invalidXPointer_returnsFalse() {
|
||||
boolean result = service.validateXPointer(testEpubFile, "/invalid/path");
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ExtractSpineIndexTests {
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromCfi_returnsCorrectIndex() {
|
||||
int index = service.extractSpineIndex("epubcfi(/6/4!/4/2)");
|
||||
|
||||
assertEquals(1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSpineIndex_fromXPointer_returnsCorrectIndex() {
|
||||
int index = service.extractSpineIndex("/body/DocFragment[3]/body/div/p");
|
||||
|
||||
assertEquals(2, index);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetSpineSizeTests {
|
||||
|
||||
@Test
|
||||
void getSpineSize_validEpub_returnsCorrectSize() {
|
||||
int size = service.getSpineSize(testEpubFile);
|
||||
|
||||
assertEquals(3, size);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class CacheTests {
|
||||
|
||||
@Test
|
||||
void createConverter_calledTwice_usesCachedDocument() {
|
||||
CfiConvertor converter1 = service.createConverter(testEpubFile, 0);
|
||||
CfiConvertor converter2 = service.createConverter(testEpubFile, 0);
|
||||
|
||||
assertNotNull(converter1);
|
||||
assertNotNull(converter2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void evictCache_removesEntriesForFile() {
|
||||
service.createConverter(testEpubFile, 0);
|
||||
service.createConverter(testEpubFile, 1);
|
||||
|
||||
service.evictCache(testEpubFile);
|
||||
|
||||
CfiConvertor converter = service.createConverter(testEpubFile, 0);
|
||||
assertNotNull(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearCache_removesAllEntries() {
|
||||
service.createConverter(testEpubFile, 0);
|
||||
service.createConverter(testEpubFile, 1);
|
||||
|
||||
service.clearCache();
|
||||
|
||||
CfiConvertor converter = service.createConverter(testEpubFile, 0);
|
||||
assertNotNull(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cache_differentSpineIndices_cachedSeparately() {
|
||||
service.createConverter(testEpubFile, 0);
|
||||
service.createConverter(testEpubFile, 1);
|
||||
service.createConverter(testEpubFile, 2);
|
||||
|
||||
CfiConvertor converter0 = service.createConverter(testEpubFile, 0);
|
||||
CfiConvertor converter1 = service.createConverter(testEpubFile, 1);
|
||||
CfiConvertor converter2 = service.createConverter(testEpubFile, 2);
|
||||
|
||||
assertNotNull(converter0);
|
||||
assertNotNull(converter1);
|
||||
assertNotNull(converter2);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class RoundTripTests {
|
||||
|
||||
@Test
|
||||
void roundTrip_xPointerToCfiAndBack() {
|
||||
String originalXPointer = "/body/DocFragment[1]/body/div[1]/p[1]";
|
||||
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile, originalXPointer);
|
||||
CfiConvertor.XPointerResult result = service.convertCfiToXPointer(testEpubFile, cfi);
|
||||
|
||||
assertNotNull(result.getXpointer());
|
||||
assertTrue(result.getXpointer().contains("/body/DocFragment[1]/body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTrip_cfiToXPointerAndBack() {
|
||||
String originalCfi = "epubcfi(/6/2!/4/2/2)";
|
||||
|
||||
CfiConvertor.XPointerResult xpointerResult = service.convertCfiToXPointer(testEpubFile, originalCfi);
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile, xpointerResult.getXpointer());
|
||||
|
||||
assertNotNull(cfi);
|
||||
assertTrue(cfi.startsWith("epubcfi("));
|
||||
assertTrue(cfi.contains("/6/2!"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class MultipleFilesTests {
|
||||
|
||||
@Test
|
||||
void cache_differentFiles_cachedSeparately() throws IOException {
|
||||
File secondEpub = createTestEpub("second.epub");
|
||||
|
||||
service.createConverter(testEpubFile, 0);
|
||||
service.createConverter(secondEpub, 0);
|
||||
|
||||
CfiConvertor converter1 = service.createConverter(testEpubFile, 0);
|
||||
CfiConvertor converter2 = service.createConverter(secondEpub, 0);
|
||||
|
||||
assertNotNull(converter1);
|
||||
assertNotNull(converter2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void evictCache_onlyAffectsSpecificFile() throws IOException {
|
||||
File secondEpub = createTestEpub("second.epub");
|
||||
|
||||
service.createConverter(testEpubFile, 0);
|
||||
service.createConverter(secondEpub, 0);
|
||||
|
||||
service.evictCache(testEpubFile);
|
||||
|
||||
CfiConvertor converter = service.createConverter(secondEpub, 0);
|
||||
assertNotNull(converter);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user