Skip to content

Commit 16ee150

Browse files
committed
Add a full coverage of libzim part.
1 parent 8c3a358 commit 16ee150

1 file changed

Lines changed: 117 additions & 6 deletions

File tree

lib/src/test/test.java

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ private void testArchive(TestArchive archive)
5050
throws IOException {
5151
// test the zim file main page title
5252
TestEntry mainPage = archive.getMainEntry();
53+
assertTrue(mainPage.isRedirect());
5354
assertEquals("mainPage", mainPage.getTitle());
5455
assertEquals("Test ZIM file", mainPage.getItem(true).getTitle());
56+
assertEquals("Test ZIM file", mainPage.getRedirectEntry().getTitle());
57+
assertEquals("Test ZIM file", mainPage.getRedirect().getTitle());
5558
// test zim file main url
5659
assertEquals("mainPage", mainPage.getPath());
5760
assertEquals("main.html", mainPage.getItem(true).getPath());
@@ -61,27 +64,104 @@ private void testArchive(TestArchive archive)
6164
// test zim file content
6265
byte[] mainData = getFileContent("small_zimfile_data/main.html");
6366
byte[] inZimMainData = archive.getEntryByPath("main.html").getItem(true).getData().getData();
64-
assert(Arrays.equals(mainData, inZimMainData));
67+
assertTrue(Arrays.equals(mainData, inZimMainData));
6568

6669
// test zim file icon
67-
assertEquals(true, archive.hasIllustration(48));
70+
assertTrue(archive.hasIllustration(48));
6871
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
6972
TestItem item = archive.getIllustrationItem(48);
7073
assertEquals(faviconData.length, item.getSize());
71-
assert(Arrays.equals(faviconData, item.getData().getData()));
74+
assertEquals("image/png", item.getMimetype());
75+
TestBlob illustrationData = item.getData();
76+
assertEquals(faviconData.length, illustrationData.size());
77+
assertTrue(Arrays.equals(faviconData, illustrationData.getData()));
7278

7379
// Checking direct access information
7480
DirectAccessInfo dai = item.getDirectAccessInformation();
7581
assertNotEquals("", dai.filename);
7682
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
77-
assert(Arrays.equals(faviconData, readData));
83+
assertTrue(Arrays.equals(faviconData, readData));
84+
85+
// Checking all metadata
86+
assertFalse(archive.isMultiPart());
87+
assertTrue(archive.hasNewNamespaceScheme());
88+
assertTrue(archive.hasChecksum());
89+
assertEquals("f4373bda1fdce141ba8e5c80baaf905d", archive.getChecksum());
90+
assertTrue(archive.hasTitleIndex());
91+
assertTrue(archive.hasFulltextIndex());
92+
assertTrue(archive.hasMainEntry());
93+
long[] illuSizes = {48};
94+
assertTrue(Arrays.equals(illuSizes, archive.getIllustrationSizes()));
95+
String[] metaKeys = {"Counter", "Creator", "Date", "Description", "Illustration_48x48@1", "Language", "LongDescription", "Name", "Publisher", "Scraper", "Tags", "Title"};
96+
assertTrue(Arrays.equals(
97+
metaKeys,
98+
archive.getMetadataKeys()
99+
));
100+
assertEquals("c23a31c1-c357-9e82-3b43-f87aaf706d04", archive.getUuid());
101+
assertEquals(1, archive.getMediaCount());
102+
assertEquals(1, archive.getArticleCount());
103+
assertEquals(2, archive.getEntryCount());
104+
assertEquals(19, archive.getAllEntryCount());
105+
assertTrue(archive.hasEntryByTitle("Test ZIM file"));
106+
assertTrue(archive.hasEntryByPath("main.html"));
107+
assertEquals("Test ZIM file", archive.getEntryByTitle("Test ZIM file").getTitle());
108+
assertEquals("main.html", archive.getEntryByPath("main.html").getPath());
109+
assertEquals("Test ZIM file", archive.getEntryByTitle(0).getTitle());
110+
assertEquals("main.html", archive.getEntryByPath(1).getPath());
111+
assertEquals("main.html", archive.getEntryByClusterOrder(0).getPath());
112+
113+
114+
assertEquals("Test ZIM file", archive.getMetadata("Title"));
115+
assertEquals("Title", archive.getMetadataItem("Title").getTitle());
116+
117+
assertFalse(archive.getRandomEntry().getTitle().isEmpty());
118+
119+
{
120+
TestEntryIterator iter = archive.iterByPath();
121+
assertTrue(iter.hasNext());
122+
assertEquals("favicon.png", iter.next().getPath());
123+
assertEquals("main.html", iter.next().getPath());
124+
assertFalse(iter.hasNext());
125+
}
126+
127+
{
128+
TestEntryIterator iter = archive.iterByTitle();
129+
assertTrue(iter.hasNext());
130+
assertEquals("main.html", iter.next().getPath());
131+
// No favicon, because favicon is not a main article (no title)
132+
assertFalse(iter.hasNext());
133+
}
134+
135+
{
136+
TestEntryIterator iter = archive.iterEfficient();
137+
assertTrue(iter.hasNext());
138+
assertEquals("main.html", iter.next().getPath());
139+
assertEquals("favicon.png", iter.next().getPath());
140+
assertFalse(iter.hasNext());
141+
}
142+
143+
{
144+
TestEntryIterator iter = archive.findByPath("ma");
145+
assertTrue(iter.hasNext());
146+
assertEquals("main.html", iter.next().getPath());
147+
assertFalse(iter.hasNext());
148+
}
149+
150+
{
151+
TestEntryIterator iter = archive.findByTitle("Test");
152+
assertTrue(iter.hasNext());
153+
assertEquals("main.html", iter.next().getPath());
154+
assertFalse(iter.hasNext());
155+
}
78156
}
79157

80158
@Test
81159
public void testArchiveDirect()
82160
throws JNIKiwixException, IOException, ZimFileFormatException {
83161
TestArchive archive = new TestArchive("small.zim");
84162
testArchive(archive);
163+
assertTrue(archive.check());
164+
assertEquals("small.zim", archive.getFilename());
85165
archive.dispose();
86166

87167
// test reader with invalid zim file
@@ -100,6 +180,8 @@ public void testArchiveByFd()
100180
FileInputStream fis = new FileInputStream("small.zim");
101181
TestArchive archive = new TestArchive(fis.getFD());
102182
testArchive(archive);
183+
assertTrue(archive.check());
184+
assertEquals("", archive.getFilename());
103185
archive.dispose();
104186
}
105187

@@ -109,7 +191,10 @@ public void testArchiveWithAnEmbeddedArchive()
109191
File plainArchive = new File("small.zim");
110192
FileInputStream fis = new FileInputStream("small.zim.embedded");
111193
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
194+
// This fails. See https://github.com/openzim/libzim/issues/812
195+
//assertTrue(archive.check());
112196
testArchive(archive);
197+
assertEquals("", archive.getFilename());
113198
archive.dispose();
114199
}
115200

@@ -148,7 +233,7 @@ public void testServer() throws ZimFileFormatException, JNIKiwixException {
148233
assertEquals(1, lib.getBookCount(true, true));
149234
TestServer server = new TestServer(lib);
150235
server.setPort(8080);
151-
assertEquals(true, server.start());
236+
assertTrue(server.start());
152237
}
153238

154239
@Test
@@ -187,21 +272,47 @@ public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixExc
187272
TestArchive archive = new TestArchive("small.zim");
188273

189274
TestSearcher searcher = new TestSearcher(archive);
190-
TestQuery query = new TestQuery("test");
275+
searcher.setVerbose(true);
276+
TestQuery query = new TestQuery("test__");
277+
query.setQuery("test");
278+
191279
TestSearch search = searcher.search(query);
192280
int estimatedMatches = (int) search.getEstimatedMatches();
193281
assertEquals(1, estimatedMatches);
194282
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
283+
assertTrue(iterator.hasNext());
195284
assertEquals("Test ZIM file", iterator.getTitle());
285+
assertEquals("main.html", iterator.getPath());
286+
assertEquals(100, iterator.getScore());
287+
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
288+
assertEquals(3, iterator.getWordCount());
289+
assertEquals(0, iterator.getFileIndex());
290+
assertEquals(-1, iterator.getSize());
291+
assertEquals("c23a31c1-c357-9e82-3b43-f87aaf706d04", iterator.getZimId());
292+
TestEntry entry = iterator.next();
293+
assertEquals("main.html", entry.getPath());
294+
295+
query.setGeorange(50,70,50);
296+
assertEquals(0, searcher.search(query).getEstimatedMatches());
196297
searcher.dispose();
197298

299+
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
300+
searcher2.addArchive(archive);
301+
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
302+
198303
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
304+
suggestionSearcher.setVerbose(true);
199305
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
200306
int matches = (int) suggestionSearch.getEstimatedMatches();
201307
assertEquals(1, matches);
202308
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
309+
assertTrue(results.hasNext());
203310
TestSuggestionItem suggestionItem = results.next();
311+
assertFalse(results.hasNext());
204312
assertEquals("Test ZIM file", suggestionItem.getTitle());
313+
assertEquals("main.html", suggestionItem.getPath());
314+
assertTrue(suggestionItem.hasSnippet());
315+
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
205316
suggestionSearcher.dispose();
206317
}
207318

0 commit comments

Comments
 (0)