Skip to content

Commit ab7bbc1

Browse files
committed
Try to trigger gc to call finalize methods.
1 parent 33152da commit ab7bbc1

1 file changed

Lines changed: 191 additions & 154 deletions

File tree

lib/src/test/test.java

Lines changed: 191 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,21 @@ private void testArchive(TestArchive archive)
171171
} catch(Exception e) {
172172
fail("ERROR: Must be a EntryNotFoundException.");
173173
}
174+
System.gc();
175+
System.runFinalization();
174176
}
175177

176178
@Test
177179
public void testArchiveDirect()
178180
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
179-
TestArchive archive = new TestArchive("small.zim");
180-
testArchive(archive);
181-
assertTrue(archive.check());
182-
assertEquals("small.zim", archive.getFilename());
183-
archive.dispose();
181+
{
182+
TestArchive archive = new TestArchive("small.zim");
183+
testArchive(archive);
184+
assertTrue(archive.check());
185+
assertEquals("small.zim", archive.getFilename());
186+
}
187+
System.gc();
188+
System.runFinalization();
184189
}
185190

186191
@Test
@@ -212,25 +217,31 @@ public void testNotValid() {
212217
@Test
213218
public void testArchiveByFd()
214219
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
215-
FileInputStream fis = new FileInputStream("small.zim");
216-
TestArchive archive = new TestArchive(fis.getFD());
217-
testArchive(archive);
218-
assertTrue(archive.check());
219-
assertEquals("", archive.getFilename());
220-
archive.dispose();
220+
{
221+
FileInputStream fis = new FileInputStream("small.zim");
222+
TestArchive archive = new TestArchive(fis.getFD());
223+
testArchive(archive);
224+
assertTrue(archive.check());
225+
assertEquals("", archive.getFilename());
226+
}
227+
System.gc();
228+
System.runFinalization();
221229
}
222230

223231
@Test
224232
public void testArchiveWithAnEmbeddedArchive()
225233
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
226-
File plainArchive = new File("small.zim");
227-
FileInputStream fis = new FileInputStream("small.zim.embedded");
228-
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
229-
// This fails. See https://github.com/openzim/libzim/issues/812
230-
//assertTrue(archive.check());
231-
testArchive(archive);
232-
assertEquals("", archive.getFilename());
233-
archive.dispose();
234+
{
235+
File plainArchive = new File("small.zim");
236+
FileInputStream fis = new FileInputStream("small.zim.embedded");
237+
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
238+
// This fails. See https://github.com/openzim/libzim/issues/812
239+
//assertTrue(archive.check());
240+
testArchive(archive);
241+
assertEquals("", archive.getFilename());
242+
}
243+
System.gc();
244+
System.runFinalization();
234245
}
235246

236247
private void testLibrary(TestLibrary lib)
@@ -270,165 +281,191 @@ private void testLibrary(TestLibrary lib)
270281

271282
@Test
272283
public void testLibrarySimple() throws IOException {
273-
TestLibrary lib = new TestLibrary();
274-
TestManager manager = new TestManager(lib);
275-
manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true);
276-
testLibrary(lib);
277-
String[] bookIds = lib.getBooksIds();
278-
TestBook book = lib.getBookById(bookIds[0]);
279-
assertEquals(book.getIllustration(48).url(), "");
280-
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
281-
assertEquals(book.getHumanReadableIdFromPath(), "small");
282-
assertTrue(book.isPathValid());
283-
284-
// remove book from library by id
285-
lib.removeBookById(bookIds[0]);
286-
bookIds = lib.getBooksIds();
287-
assertEquals(bookIds.length, 0);
284+
{
285+
TestLibrary lib = new TestLibrary();
286+
TestManager manager = new TestManager(lib);
287+
manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true);
288+
testLibrary(lib);
289+
String[] bookIds = lib.getBooksIds();
290+
TestBook book = lib.getBookById(bookIds[0]);
291+
assertEquals(book.getIllustration(48).url(), "");
292+
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
293+
assertEquals(book.getHumanReadableIdFromPath(), "small");
294+
assertTrue(book.isPathValid());
295+
296+
// remove book from library by id
297+
lib.removeBookById(bookIds[0]);
298+
bookIds = lib.getBooksIds();
299+
assertEquals(bookIds.length, 0);
300+
}
301+
System.gc();
302+
System.runFinalization();
288303
}
289304

290305
@Test
291306
public void testLibraryXml() throws IOException {
292-
TestLibrary lib = new TestLibrary();
293-
TestManager manager = new TestManager(lib);
294-
manager.readFile("library.xml");
295-
testLibrary(lib);
296-
String[] bookIds = lib.getBooksIds();
297-
TestBook book = lib.getBookById(bookIds[0]);
298-
assertEquals(book.getIllustration(48).url(), "");
299-
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
300-
assertEquals(book.getHumanReadableIdFromPath(), "small");
301-
assertTrue(book.isPathValid());
307+
{
308+
TestLibrary lib = new TestLibrary();
309+
TestManager manager = new TestManager(lib);
310+
manager.readFile("library.xml");
311+
testLibrary(lib);
312+
String[] bookIds = lib.getBooksIds();
313+
TestBook book = lib.getBookById(bookIds[0]);
314+
assertEquals(book.getIllustration(48).url(), "");
315+
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
316+
assertEquals(book.getHumanReadableIdFromPath(), "small");
317+
assertTrue(book.isPathValid());
318+
}
319+
System.gc();
320+
System.runFinalization();
302321
}
303322

304323
@Test
305324
public void testLibraryXmlContent() throws IOException {
306-
TestLibrary lib = new TestLibrary();
307-
TestManager manager = new TestManager(lib);
308-
String content = getTextFileContent("library.xml");
309-
manager.readXml(content, "library.xml");
310-
testLibrary(lib);
311-
String[] bookIds = lib.getBooksIds();
312-
TestBook book = lib.getBookById(bookIds[0]);
313-
assertEquals(book.getIllustration(48).url(), "");
314-
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
315-
assertEquals(book.getHumanReadableIdFromPath(), "small");
316-
assertTrue(book.isPathValid());
325+
{
326+
TestLibrary lib = new TestLibrary();
327+
TestManager manager = new TestManager(lib);
328+
String content = getTextFileContent("library.xml");
329+
manager.readXml(content, "library.xml");
330+
testLibrary(lib);
331+
String[] bookIds = lib.getBooksIds();
332+
TestBook book = lib.getBookById(bookIds[0]);
333+
assertEquals(book.getIllustration(48).url(), "");
334+
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
335+
assertEquals(book.getHumanReadableIdFromPath(), "small");
336+
assertTrue(book.isPathValid());
337+
}
338+
System.gc();
339+
System.runFinalization();
317340
}
318341

319342
@Test
320343
public void testLibraryOPDS() throws IOException {
321-
TestLibrary lib = new TestLibrary();
322-
TestManager manager = new TestManager(lib);
323-
String content = getTextFileContent("catalog.xml");
324-
manager.readOpds(content, "http://localhost");
325-
testLibrary(lib);
326-
String[] bookIds = lib.getBooksIds();
327-
TestBook book = lib.getBookById(bookIds[0]);
328-
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");
329-
assertEquals(book.getPath(), "");
330-
assertEquals(book.getHumanReadableIdFromPath(), "");
331-
assertFalse(book.isPathValid());
344+
{
345+
TestLibrary lib = new TestLibrary();
346+
TestManager manager = new TestManager(lib);
347+
String content = getTextFileContent("catalog.xml");
348+
manager.readOpds(content, "http://localhost");
349+
testLibrary(lib);
350+
String[] bookIds = lib.getBooksIds();
351+
TestBook book = lib.getBookById(bookIds[0]);
352+
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");
353+
assertEquals(book.getPath(), "");
354+
assertEquals(book.getHumanReadableIdFromPath(), "");
355+
assertFalse(book.isPathValid());
356+
}
357+
System.gc();
358+
System.runFinalization();
332359
}
333360

334361
@Test
335362
public void testServer() throws ZimFileFormatException, JNIKiwixException {
336-
TestArchive archive = new TestArchive("small.zim");
337-
TestLibrary lib = new TestLibrary();
338-
TestBook book = new TestBook();
339-
book.update(archive);
340-
lib.addBook(book);
341-
assertEquals(1, lib.getBookCount(true, true));
342-
TestServer server = new TestServer(lib);
343-
server.setPort(8080);
344-
server.setRoot("FOO");
345-
server.setAddress("127.0.0.1");
346-
server.setNbThreads(1);
347-
server.setBlockExternalLinks(true);
348-
server.setTaskbar(true, true);
349-
assertTrue(server.start());
350-
server.stop();
363+
{
364+
TestArchive archive = new TestArchive("small.zim");
365+
TestLibrary lib = new TestLibrary();
366+
TestBook book = new TestBook();
367+
book.update(archive);
368+
lib.addBook(book);
369+
assertEquals(1, lib.getBookCount(true, true));
370+
TestServer server = new TestServer(lib);
371+
server.setPort(8080);
372+
server.setRoot("FOO");
373+
server.setAddress("127.0.0.1");
374+
server.setNbThreads(1);
375+
server.setBlockExternalLinks(true);
376+
server.setTaskbar(true, true);
377+
assertTrue(server.start());
378+
server.stop();
379+
}
380+
System.gc();
381+
System.runFinalization();
351382
}
352383

353384
@Test
354385
public void testBookMark() throws ZimFileFormatException, JNIKiwixException {
355-
TestArchive archive = new TestArchive("small.zim");
356-
TestLibrary lib = new TestLibrary();
357-
TestBook book = new TestBook();
358-
book.update(archive);
359-
lib.addBook(book);
360-
TestBookmark bookmark = new TestBookmark();
361-
bookmark.setBookId(book.getId());
362-
bookmark.setTitle(book.getTitle());
363-
bookmark.setUrl(book.getUrl());
364-
bookmark.setLanguage(book.getLanguage());
365-
bookmark.setDate(book.getDate());
366-
bookmark.setBookTitle(book.getName());
367-
// add bookmark to library
368-
lib.addBookmark(bookmark);
369-
TestBookmark[] bookmarkArray = lib.getBookmarks(true);
370-
assertEquals(1, bookmarkArray.length);
371-
bookmark = bookmarkArray[0];
372-
// test saved bookmark
373-
assertEquals(bookmark.getBookId(), book.getId());
374-
assertEquals(bookmark.getTitle(), book.getTitle());
375-
assertEquals(bookmark.getUrl(), book.getUrl());
376-
assertEquals(bookmark.getLanguage(), book.getLanguage());
377-
assertEquals(bookmark.getDate(), book.getDate());
378-
assertEquals(bookmark.getBookTitle(), book.getName());
379-
// remove bookmark from library
380-
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
381-
bookmarkArray = lib.getBookmarks(true);
382-
assertEquals(0, bookmarkArray.length);
386+
{
387+
TestArchive archive = new TestArchive("small.zim");
388+
TestLibrary lib = new TestLibrary();
389+
TestBook book = new TestBook();
390+
book.update(archive);
391+
lib.addBook(book);
392+
TestBookmark bookmark = new TestBookmark();
393+
bookmark.setBookId(book.getId());
394+
bookmark.setTitle(book.getTitle());
395+
bookmark.setUrl(book.getUrl());
396+
bookmark.setLanguage(book.getLanguage());
397+
bookmark.setDate(book.getDate());
398+
bookmark.setBookTitle(book.getName());
399+
// add bookmark to library
400+
lib.addBookmark(bookmark);
401+
TestBookmark[] bookmarkArray = lib.getBookmarks(true);
402+
assertEquals(1, bookmarkArray.length);
403+
bookmark = bookmarkArray[0];
404+
// test saved bookmark
405+
assertEquals(bookmark.getBookId(), book.getId());
406+
assertEquals(bookmark.getTitle(), book.getTitle());
407+
assertEquals(bookmark.getUrl(), book.getUrl());
408+
assertEquals(bookmark.getLanguage(), book.getLanguage());
409+
assertEquals(bookmark.getDate(), book.getDate());
410+
assertEquals(bookmark.getBookTitle(), book.getName());
411+
// remove bookmark from library
412+
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
413+
bookmarkArray = lib.getBookmarks(true);
414+
assertEquals(0, bookmarkArray.length);
415+
}
416+
System.gc();
417+
System.runFinalization();
383418
}
384419

385420
@Test
386421
public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException {
387-
TestArchive archive = new TestArchive("small.zim");
388-
389-
TestSearcher searcher = new TestSearcher(archive);
390-
searcher.setVerbose(true);
391-
TestQuery query = new TestQuery("test__");
392-
query.setQuery("test");
393-
394-
TestSearch search = searcher.search(query);
395-
int estimatedMatches = (int) search.getEstimatedMatches();
396-
assertEquals(1, estimatedMatches);
397-
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
398-
assertTrue(iterator.hasNext());
399-
assertEquals("Test ZIM file", iterator.getTitle());
400-
assertEquals("main.html", iterator.getPath());
401-
assertEquals(100, iterator.getScore());
402-
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
403-
assertEquals(3, iterator.getWordCount());
404-
assertEquals(0, iterator.getFileIndex());
405-
assertEquals(-1, iterator.getSize());
406-
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId());
407-
TestEntry entry = iterator.next();
408-
assertEquals("main.html", entry.getPath());
409-
410-
query.setGeorange(50,70,50);
411-
assertEquals(0, searcher.search(query).getEstimatedMatches());
412-
searcher.dispose();
413-
414-
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
415-
searcher2.addArchive(archive);
416-
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
417-
418-
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
419-
suggestionSearcher.setVerbose(true);
420-
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
421-
int matches = (int) suggestionSearch.getEstimatedMatches();
422-
assertEquals(1, matches);
423-
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
424-
assertTrue(results.hasNext());
425-
TestSuggestionItem suggestionItem = results.next();
426-
assertFalse(results.hasNext());
427-
assertEquals("Test ZIM file", suggestionItem.getTitle());
428-
assertEquals("main.html", suggestionItem.getPath());
429-
assertTrue(suggestionItem.hasSnippet());
430-
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
431-
suggestionSearcher.dispose();
422+
{
423+
TestArchive archive = new TestArchive("small.zim");
424+
425+
TestSearcher searcher = new TestSearcher(archive);
426+
searcher.setVerbose(true);
427+
TestQuery query = new TestQuery("test__");
428+
query.setQuery("test");
429+
430+
TestSearch search = searcher.search(query);
431+
int estimatedMatches = (int) search.getEstimatedMatches();
432+
assertEquals(1, estimatedMatches);
433+
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
434+
assertTrue(iterator.hasNext());
435+
assertEquals("Test ZIM file", iterator.getTitle());
436+
assertEquals("main.html", iterator.getPath());
437+
assertEquals(100, iterator.getScore());
438+
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
439+
assertEquals(3, iterator.getWordCount());
440+
assertEquals(0, iterator.getFileIndex());
441+
assertEquals(-1, iterator.getSize());
442+
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId());
443+
TestEntry entry = iterator.next();
444+
assertEquals("main.html", entry.getPath());
445+
446+
query.setGeorange(50,70,50);
447+
assertEquals(0, searcher.search(query).getEstimatedMatches());
448+
449+
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
450+
searcher2.addArchive(archive);
451+
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
452+
453+
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
454+
suggestionSearcher.setVerbose(true);
455+
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
456+
int matches = (int) suggestionSearch.getEstimatedMatches();
457+
assertEquals(1, matches);
458+
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
459+
assertTrue(results.hasNext());
460+
TestSuggestionItem suggestionItem = results.next();
461+
assertFalse(results.hasNext());
462+
assertEquals("Test ZIM file", suggestionItem.getTitle());
463+
assertEquals("main.html", suggestionItem.getPath());
464+
assertTrue(suggestionItem.hasSnippet());
465+
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
466+
}
467+
System.gc();
468+
System.runFinalization();
432469
}
433470

434471
static

0 commit comments

Comments
 (0)