Try to trigger gc to call finalize methods.

This commit is contained in:
Matthieu Gautier 2023-06-30 11:54:26 +02:00
parent 33152da9b4
commit ab7bbc1043

View File

@ -171,16 +171,21 @@ public class test {
} catch(Exception e) { } catch(Exception e) {
fail("ERROR: Must be a EntryNotFoundException."); fail("ERROR: Must be a EntryNotFoundException.");
} }
System.gc();
System.runFinalization();
} }
@Test @Test
public void testArchiveDirect() public void testArchiveDirect()
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
TestArchive archive = new TestArchive("small.zim"); {
testArchive(archive); TestArchive archive = new TestArchive("small.zim");
assertTrue(archive.check()); testArchive(archive);
assertEquals("small.zim", archive.getFilename()); assertTrue(archive.check());
archive.dispose(); assertEquals("small.zim", archive.getFilename());
}
System.gc();
System.runFinalization();
} }
@Test @Test
@ -212,25 +217,31 @@ public class test {
@Test @Test
public void testArchiveByFd() public void testArchiveByFd()
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
FileInputStream fis = new FileInputStream("small.zim"); {
TestArchive archive = new TestArchive(fis.getFD()); FileInputStream fis = new FileInputStream("small.zim");
testArchive(archive); TestArchive archive = new TestArchive(fis.getFD());
assertTrue(archive.check()); testArchive(archive);
assertEquals("", archive.getFilename()); assertTrue(archive.check());
archive.dispose(); assertEquals("", archive.getFilename());
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testArchiveWithAnEmbeddedArchive() public void testArchiveWithAnEmbeddedArchive()
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
File plainArchive = new File("small.zim"); {
FileInputStream fis = new FileInputStream("small.zim.embedded"); File plainArchive = new File("small.zim");
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length()); FileInputStream fis = new FileInputStream("small.zim.embedded");
// This fails. See https://github.com/openzim/libzim/issues/812 TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
//assertTrue(archive.check()); // This fails. See https://github.com/openzim/libzim/issues/812
testArchive(archive); //assertTrue(archive.check());
assertEquals("", archive.getFilename()); testArchive(archive);
archive.dispose(); assertEquals("", archive.getFilename());
}
System.gc();
System.runFinalization();
} }
private void testLibrary(TestLibrary lib) private void testLibrary(TestLibrary lib)
@ -270,165 +281,191 @@ public class test {
@Test @Test
public void testLibrarySimple() throws IOException { public void testLibrarySimple() throws IOException {
TestLibrary lib = new TestLibrary(); {
TestManager manager = new TestManager(lib); TestLibrary lib = new TestLibrary();
manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true); TestManager manager = new TestManager(lib);
testLibrary(lib); manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true);
String[] bookIds = lib.getBooksIds(); testLibrary(lib);
TestBook book = lib.getBookById(bookIds[0]); String[] bookIds = lib.getBooksIds();
assertEquals(book.getIllustration(48).url(), ""); TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath()); assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getHumanReadableIdFromPath(), "small"); assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertTrue(book.isPathValid()); assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
// remove book from library by id // remove book from library by id
lib.removeBookById(bookIds[0]); lib.removeBookById(bookIds[0]);
bookIds = lib.getBooksIds(); bookIds = lib.getBooksIds();
assertEquals(bookIds.length, 0); assertEquals(bookIds.length, 0);
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testLibraryXml() throws IOException { public void testLibraryXml() throws IOException {
TestLibrary lib = new TestLibrary(); {
TestManager manager = new TestManager(lib); TestLibrary lib = new TestLibrary();
manager.readFile("library.xml"); TestManager manager = new TestManager(lib);
testLibrary(lib); manager.readFile("library.xml");
String[] bookIds = lib.getBooksIds(); testLibrary(lib);
TestBook book = lib.getBookById(bookIds[0]); String[] bookIds = lib.getBooksIds();
assertEquals(book.getIllustration(48).url(), ""); TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath()); assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getHumanReadableIdFromPath(), "small"); assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertTrue(book.isPathValid()); assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testLibraryXmlContent() throws IOException { public void testLibraryXmlContent() throws IOException {
TestLibrary lib = new TestLibrary(); {
TestManager manager = new TestManager(lib); TestLibrary lib = new TestLibrary();
String content = getTextFileContent("library.xml"); TestManager manager = new TestManager(lib);
manager.readXml(content, "library.xml"); String content = getTextFileContent("library.xml");
testLibrary(lib); manager.readXml(content, "library.xml");
String[] bookIds = lib.getBooksIds(); testLibrary(lib);
TestBook book = lib.getBookById(bookIds[0]); String[] bookIds = lib.getBooksIds();
assertEquals(book.getIllustration(48).url(), ""); TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath()); assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getHumanReadableIdFromPath(), "small"); assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertTrue(book.isPathValid()); assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testLibraryOPDS() throws IOException { public void testLibraryOPDS() throws IOException {
TestLibrary lib = new TestLibrary(); {
TestManager manager = new TestManager(lib); TestLibrary lib = new TestLibrary();
String content = getTextFileContent("catalog.xml"); TestManager manager = new TestManager(lib);
manager.readOpds(content, "http://localhost"); String content = getTextFileContent("catalog.xml");
testLibrary(lib); manager.readOpds(content, "http://localhost");
String[] bookIds = lib.getBooksIds(); testLibrary(lib);
TestBook book = lib.getBookById(bookIds[0]); String[] bookIds = lib.getBooksIds();
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small"); TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getPath(), ""); assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");
assertEquals(book.getHumanReadableIdFromPath(), ""); assertEquals(book.getPath(), "");
assertFalse(book.isPathValid()); assertEquals(book.getHumanReadableIdFromPath(), "");
assertFalse(book.isPathValid());
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testServer() throws ZimFileFormatException, JNIKiwixException { public void testServer() throws ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim"); {
TestLibrary lib = new TestLibrary(); TestArchive archive = new TestArchive("small.zim");
TestBook book = new TestBook(); TestLibrary lib = new TestLibrary();
book.update(archive); TestBook book = new TestBook();
lib.addBook(book); book.update(archive);
assertEquals(1, lib.getBookCount(true, true)); lib.addBook(book);
TestServer server = new TestServer(lib); assertEquals(1, lib.getBookCount(true, true));
server.setPort(8080); TestServer server = new TestServer(lib);
server.setRoot("FOO"); server.setPort(8080);
server.setAddress("127.0.0.1"); server.setRoot("FOO");
server.setNbThreads(1); server.setAddress("127.0.0.1");
server.setBlockExternalLinks(true); server.setNbThreads(1);
server.setTaskbar(true, true); server.setBlockExternalLinks(true);
assertTrue(server.start()); server.setTaskbar(true, true);
server.stop(); assertTrue(server.start());
server.stop();
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testBookMark() throws ZimFileFormatException, JNIKiwixException { public void testBookMark() throws ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim"); {
TestLibrary lib = new TestLibrary(); TestArchive archive = new TestArchive("small.zim");
TestBook book = new TestBook(); TestLibrary lib = new TestLibrary();
book.update(archive); TestBook book = new TestBook();
lib.addBook(book); book.update(archive);
TestBookmark bookmark = new TestBookmark(); lib.addBook(book);
bookmark.setBookId(book.getId()); TestBookmark bookmark = new TestBookmark();
bookmark.setTitle(book.getTitle()); bookmark.setBookId(book.getId());
bookmark.setUrl(book.getUrl()); bookmark.setTitle(book.getTitle());
bookmark.setLanguage(book.getLanguage()); bookmark.setUrl(book.getUrl());
bookmark.setDate(book.getDate()); bookmark.setLanguage(book.getLanguage());
bookmark.setBookTitle(book.getName()); bookmark.setDate(book.getDate());
// add bookmark to library bookmark.setBookTitle(book.getName());
lib.addBookmark(bookmark); // add bookmark to library
TestBookmark[] bookmarkArray = lib.getBookmarks(true); lib.addBookmark(bookmark);
assertEquals(1, bookmarkArray.length); TestBookmark[] bookmarkArray = lib.getBookmarks(true);
bookmark = bookmarkArray[0]; assertEquals(1, bookmarkArray.length);
// test saved bookmark bookmark = bookmarkArray[0];
assertEquals(bookmark.getBookId(), book.getId()); // test saved bookmark
assertEquals(bookmark.getTitle(), book.getTitle()); assertEquals(bookmark.getBookId(), book.getId());
assertEquals(bookmark.getUrl(), book.getUrl()); assertEquals(bookmark.getTitle(), book.getTitle());
assertEquals(bookmark.getLanguage(), book.getLanguage()); assertEquals(bookmark.getUrl(), book.getUrl());
assertEquals(bookmark.getDate(), book.getDate()); assertEquals(bookmark.getLanguage(), book.getLanguage());
assertEquals(bookmark.getBookTitle(), book.getName()); assertEquals(bookmark.getDate(), book.getDate());
// remove bookmark from library assertEquals(bookmark.getBookTitle(), book.getName());
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl()); // remove bookmark from library
bookmarkArray = lib.getBookmarks(true); lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
assertEquals(0, bookmarkArray.length); bookmarkArray = lib.getBookmarks(true);
assertEquals(0, bookmarkArray.length);
}
System.gc();
System.runFinalization();
} }
@Test @Test
public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException { public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim"); {
TestArchive archive = new TestArchive("small.zim");
TestSearcher searcher = new TestSearcher(archive); TestSearcher searcher = new TestSearcher(archive);
searcher.setVerbose(true); searcher.setVerbose(true);
TestQuery query = new TestQuery("test__"); TestQuery query = new TestQuery("test__");
query.setQuery("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()); assertTrue(iterator.hasNext());
assertEquals("Test ZIM file", iterator.getTitle()); assertEquals("Test ZIM file", iterator.getTitle());
assertEquals("main.html", iterator.getPath()); assertEquals("main.html", iterator.getPath());
assertEquals(100, iterator.getScore()); assertEquals(100, iterator.getScore());
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet()); assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
assertEquals(3, iterator.getWordCount()); assertEquals(3, iterator.getWordCount());
assertEquals(0, iterator.getFileIndex()); assertEquals(0, iterator.getFileIndex());
assertEquals(-1, iterator.getSize()); assertEquals(-1, iterator.getSize());
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId()); assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId());
TestEntry entry = iterator.next(); TestEntry entry = iterator.next();
assertEquals("main.html", entry.getPath()); assertEquals("main.html", entry.getPath());
query.setGeorange(50,70,50); query.setGeorange(50,70,50);
assertEquals(0, searcher.search(query).getEstimatedMatches()); assertEquals(0, searcher.search(query).getEstimatedMatches());
searcher.dispose();
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]); TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
searcher2.addArchive(archive); searcher2.addArchive(archive);
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches()); assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive); TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
suggestionSearcher.setVerbose(true); 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()); assertTrue(results.hasNext());
TestSuggestionItem suggestionItem = results.next(); TestSuggestionItem suggestionItem = results.next();
assertFalse(results.hasNext()); assertFalse(results.hasNext());
assertEquals("Test ZIM file", suggestionItem.getTitle()); assertEquals("Test ZIM file", suggestionItem.getTitle());
assertEquals("main.html", suggestionItem.getPath()); assertEquals("main.html", suggestionItem.getPath());
assertTrue(suggestionItem.hasSnippet()); assertTrue(suggestionItem.hasSnippet());
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet()); assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
suggestionSearcher.dispose(); }
System.gc();
System.runFinalization();
} }
static static