mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-14 17:47:12 -04:00
Add a full coverage of libzim part.
This commit is contained in:
parent
8c3a358486
commit
16ee15021a
@ -50,8 +50,11 @@ public class test {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
// test the zim file main page title
|
// test the zim file main page title
|
||||||
TestEntry mainPage = archive.getMainEntry();
|
TestEntry mainPage = archive.getMainEntry();
|
||||||
|
assertTrue(mainPage.isRedirect());
|
||||||
assertEquals("mainPage", mainPage.getTitle());
|
assertEquals("mainPage", mainPage.getTitle());
|
||||||
assertEquals("Test ZIM file", mainPage.getItem(true).getTitle());
|
assertEquals("Test ZIM file", mainPage.getItem(true).getTitle());
|
||||||
|
assertEquals("Test ZIM file", mainPage.getRedirectEntry().getTitle());
|
||||||
|
assertEquals("Test ZIM file", mainPage.getRedirect().getTitle());
|
||||||
// test zim file main url
|
// test zim file main url
|
||||||
assertEquals("mainPage", mainPage.getPath());
|
assertEquals("mainPage", mainPage.getPath());
|
||||||
assertEquals("main.html", mainPage.getItem(true).getPath());
|
assertEquals("main.html", mainPage.getItem(true).getPath());
|
||||||
@ -61,20 +64,95 @@ public class test {
|
|||||||
// test zim file content
|
// test zim file content
|
||||||
byte[] mainData = getFileContent("small_zimfile_data/main.html");
|
byte[] mainData = getFileContent("small_zimfile_data/main.html");
|
||||||
byte[] inZimMainData = archive.getEntryByPath("main.html").getItem(true).getData().getData();
|
byte[] inZimMainData = archive.getEntryByPath("main.html").getItem(true).getData().getData();
|
||||||
assert(Arrays.equals(mainData, inZimMainData));
|
assertTrue(Arrays.equals(mainData, inZimMainData));
|
||||||
|
|
||||||
// test zim file icon
|
// test zim file icon
|
||||||
assertEquals(true, archive.hasIllustration(48));
|
assertTrue(archive.hasIllustration(48));
|
||||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||||
TestItem item = archive.getIllustrationItem(48);
|
TestItem item = archive.getIllustrationItem(48);
|
||||||
assertEquals(faviconData.length, item.getSize());
|
assertEquals(faviconData.length, item.getSize());
|
||||||
assert(Arrays.equals(faviconData, item.getData().getData()));
|
assertEquals("image/png", item.getMimetype());
|
||||||
|
TestBlob illustrationData = item.getData();
|
||||||
|
assertEquals(faviconData.length, illustrationData.size());
|
||||||
|
assertTrue(Arrays.equals(faviconData, illustrationData.getData()));
|
||||||
|
|
||||||
// Checking direct access information
|
// Checking direct access information
|
||||||
DirectAccessInfo dai = item.getDirectAccessInformation();
|
DirectAccessInfo dai = item.getDirectAccessInformation();
|
||||||
assertNotEquals("", dai.filename);
|
assertNotEquals("", dai.filename);
|
||||||
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
|
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
|
||||||
assert(Arrays.equals(faviconData, readData));
|
assertTrue(Arrays.equals(faviconData, readData));
|
||||||
|
|
||||||
|
// Checking all metadata
|
||||||
|
assertFalse(archive.isMultiPart());
|
||||||
|
assertTrue(archive.hasNewNamespaceScheme());
|
||||||
|
assertTrue(archive.hasChecksum());
|
||||||
|
assertEquals("f4373bda1fdce141ba8e5c80baaf905d", archive.getChecksum());
|
||||||
|
assertTrue(archive.hasTitleIndex());
|
||||||
|
assertTrue(archive.hasFulltextIndex());
|
||||||
|
assertTrue(archive.hasMainEntry());
|
||||||
|
long[] illuSizes = {48};
|
||||||
|
assertTrue(Arrays.equals(illuSizes, archive.getIllustrationSizes()));
|
||||||
|
String[] metaKeys = {"Counter", "Creator", "Date", "Description", "Illustration_48x48@1", "Language", "LongDescription", "Name", "Publisher", "Scraper", "Tags", "Title"};
|
||||||
|
assertTrue(Arrays.equals(
|
||||||
|
metaKeys,
|
||||||
|
archive.getMetadataKeys()
|
||||||
|
));
|
||||||
|
assertEquals("c23a31c1-c357-9e82-3b43-f87aaf706d04", archive.getUuid());
|
||||||
|
assertEquals(1, archive.getMediaCount());
|
||||||
|
assertEquals(1, archive.getArticleCount());
|
||||||
|
assertEquals(2, archive.getEntryCount());
|
||||||
|
assertEquals(19, archive.getAllEntryCount());
|
||||||
|
assertTrue(archive.hasEntryByTitle("Test ZIM file"));
|
||||||
|
assertTrue(archive.hasEntryByPath("main.html"));
|
||||||
|
assertEquals("Test ZIM file", archive.getEntryByTitle("Test ZIM file").getTitle());
|
||||||
|
assertEquals("main.html", archive.getEntryByPath("main.html").getPath());
|
||||||
|
assertEquals("Test ZIM file", archive.getEntryByTitle(0).getTitle());
|
||||||
|
assertEquals("main.html", archive.getEntryByPath(1).getPath());
|
||||||
|
assertEquals("main.html", archive.getEntryByClusterOrder(0).getPath());
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals("Test ZIM file", archive.getMetadata("Title"));
|
||||||
|
assertEquals("Title", archive.getMetadataItem("Title").getTitle());
|
||||||
|
|
||||||
|
assertFalse(archive.getRandomEntry().getTitle().isEmpty());
|
||||||
|
|
||||||
|
{
|
||||||
|
TestEntryIterator iter = archive.iterByPath();
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertEquals("favicon.png", iter.next().getPath());
|
||||||
|
assertEquals("main.html", iter.next().getPath());
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
TestEntryIterator iter = archive.iterByTitle();
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertEquals("main.html", iter.next().getPath());
|
||||||
|
// No favicon, because favicon is not a main article (no title)
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
TestEntryIterator iter = archive.iterEfficient();
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertEquals("main.html", iter.next().getPath());
|
||||||
|
assertEquals("favicon.png", iter.next().getPath());
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
TestEntryIterator iter = archive.findByPath("ma");
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertEquals("main.html", iter.next().getPath());
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
TestEntryIterator iter = archive.findByTitle("Test");
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertEquals("main.html", iter.next().getPath());
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -82,6 +160,8 @@ public class test {
|
|||||||
throws JNIKiwixException, IOException, ZimFileFormatException {
|
throws JNIKiwixException, IOException, ZimFileFormatException {
|
||||||
TestArchive archive = new TestArchive("small.zim");
|
TestArchive archive = new TestArchive("small.zim");
|
||||||
testArchive(archive);
|
testArchive(archive);
|
||||||
|
assertTrue(archive.check());
|
||||||
|
assertEquals("small.zim", archive.getFilename());
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
|
|
||||||
// test reader with invalid zim file
|
// test reader with invalid zim file
|
||||||
@ -100,6 +180,8 @@ public class test {
|
|||||||
FileInputStream fis = new FileInputStream("small.zim");
|
FileInputStream fis = new FileInputStream("small.zim");
|
||||||
TestArchive archive = new TestArchive(fis.getFD());
|
TestArchive archive = new TestArchive(fis.getFD());
|
||||||
testArchive(archive);
|
testArchive(archive);
|
||||||
|
assertTrue(archive.check());
|
||||||
|
assertEquals("", archive.getFilename());
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +191,10 @@ public class test {
|
|||||||
File plainArchive = new File("small.zim");
|
File plainArchive = new File("small.zim");
|
||||||
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
||||||
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
|
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
|
||||||
|
// This fails. See https://github.com/openzim/libzim/issues/812
|
||||||
|
//assertTrue(archive.check());
|
||||||
testArchive(archive);
|
testArchive(archive);
|
||||||
|
assertEquals("", archive.getFilename());
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +233,7 @@ public class test {
|
|||||||
assertEquals(1, lib.getBookCount(true, true));
|
assertEquals(1, lib.getBookCount(true, true));
|
||||||
TestServer server = new TestServer(lib);
|
TestServer server = new TestServer(lib);
|
||||||
server.setPort(8080);
|
server.setPort(8080);
|
||||||
assertEquals(true, server.start());
|
assertTrue(server.start());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -187,21 +272,47 @@ public class test {
|
|||||||
TestArchive archive = new TestArchive("small.zim");
|
TestArchive archive = new TestArchive("small.zim");
|
||||||
|
|
||||||
TestSearcher searcher = new TestSearcher(archive);
|
TestSearcher searcher = new TestSearcher(archive);
|
||||||
TestQuery query = new TestQuery("test");
|
searcher.setVerbose(true);
|
||||||
|
TestQuery query = new TestQuery("test__");
|
||||||
|
query.setQuery("test");
|
||||||
|
|
||||||
TestSearch search = searcher.search(query);
|
TestSearch search = searcher.search(query);
|
||||||
int estimatedMatches = (int) search.getEstimatedMatches();
|
int estimatedMatches = (int) search.getEstimatedMatches();
|
||||||
assertEquals(1, estimatedMatches);
|
assertEquals(1, estimatedMatches);
|
||||||
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
|
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
|
||||||
|
assertTrue(iterator.hasNext());
|
||||||
assertEquals("Test ZIM file", iterator.getTitle());
|
assertEquals("Test ZIM file", iterator.getTitle());
|
||||||
|
assertEquals("main.html", iterator.getPath());
|
||||||
|
assertEquals(100, iterator.getScore());
|
||||||
|
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
|
||||||
|
assertEquals(3, iterator.getWordCount());
|
||||||
|
assertEquals(0, iterator.getFileIndex());
|
||||||
|
assertEquals(-1, iterator.getSize());
|
||||||
|
assertEquals("c23a31c1-c357-9e82-3b43-f87aaf706d04", iterator.getZimId());
|
||||||
|
TestEntry entry = iterator.next();
|
||||||
|
assertEquals("main.html", entry.getPath());
|
||||||
|
|
||||||
|
query.setGeorange(50,70,50);
|
||||||
|
assertEquals(0, searcher.search(query).getEstimatedMatches());
|
||||||
searcher.dispose();
|
searcher.dispose();
|
||||||
|
|
||||||
|
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
|
||||||
|
searcher2.addArchive(archive);
|
||||||
|
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
|
||||||
|
|
||||||
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
|
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
|
||||||
|
suggestionSearcher.setVerbose(true);
|
||||||
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
|
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
|
||||||
int matches = (int) suggestionSearch.getEstimatedMatches();
|
int matches = (int) suggestionSearch.getEstimatedMatches();
|
||||||
assertEquals(1, matches);
|
assertEquals(1, matches);
|
||||||
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
|
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
|
||||||
|
assertTrue(results.hasNext());
|
||||||
TestSuggestionItem suggestionItem = results.next();
|
TestSuggestionItem suggestionItem = results.next();
|
||||||
|
assertFalse(results.hasNext());
|
||||||
assertEquals("Test ZIM file", suggestionItem.getTitle());
|
assertEquals("Test ZIM file", suggestionItem.getTitle());
|
||||||
|
assertEquals("main.html", suggestionItem.getPath());
|
||||||
|
assertTrue(suggestionItem.hasSnippet());
|
||||||
|
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
|
||||||
suggestionSearcher.dispose();
|
suggestionSearcher.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user