From 9d6ef87b57098ac37ad884a6e2aaeadeff7adcec Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 30 Jun 2023 10:59:43 +0200 Subject: [PATCH] Introduce EntryNotFoundException. --- lib/build.gradle | 3 ++- lib/src/main/cpp/macros.h | 2 +- .../main/java/org/kiwix/libzim/Archive.java | 16 +++++------ .../kiwix/libzim/EntryNotFoundException.java | 27 +++++++++++++++++++ .../org/kiwix/test/libzim/TestArchive.java | 16 +++++------ lib/src/test/test.java | 18 ++++++++----- 6 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 lib/src/main/java/org/kiwix/libzim/EntryNotFoundException.java diff --git a/lib/build.gradle b/lib/build.gradle index 0c5dc79..6d25317 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -342,7 +342,8 @@ String getLibzimFiles() { "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionIterator.java " + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " + - "${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java" + "${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java" } task buildLinuxBinding(type: Exec) { diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h index ec276ff..46201d5 100644 --- a/lib/src/main/cpp/macros.h +++ b/lib/src/main/cpp/macros.h @@ -51,7 +51,7 @@ catch(const zim::ZimFileFormatError& e) { \ throwException(env, "java/lang/Exception", e.what()); \ return RET; \ } catch(const zim::EntryNotFound& e) { \ - throwException(env, "java/lang/Exception", e.what()); \ + throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \ return RET; \ } catch (const std::ios_base::failure& e) { \ throwException(env, "java/io/IOException", e.what()); \ diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java index 735cc79..58883dd 100644 --- a/lib/src/main/java/org/kiwix/libzim/Archive.java +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -60,25 +60,25 @@ public class Archive public native int getArticleCount(); public native int getMediaCount(); public native String getUuid(); - public native String getMetadata(String name); - public native Item getMetadataItem(String name); + public native String getMetadata(String name) throws EntryNotFoundException; + public native Item getMetadataItem(String name) throws EntryNotFoundException; public native String[] getMetadataKeys(); public native Item getIllustrationItem(int size); public native boolean hasIllustration(int size); public native long[] getIllustrationSizes(); - public native Entry getEntryByPath(String path); - public native Entry getEntryByPath(int index); + public native Entry getEntryByPath(String path) throws EntryNotFoundException; + public native Entry getEntryByPath(int index) throws EntryNotFoundException; public native boolean hasEntryByPath(String path); - public native Entry getEntryByTitle(String title); - public native Entry getEntryByTitle(int index); + public native Entry getEntryByTitle(String title) throws EntryNotFoundException; + public native Entry getEntryByTitle(int index) throws EntryNotFoundException; public native boolean hasEntryByTitle(String title); - public native Entry getEntryByClusterOrder(int index); + public native Entry getEntryByClusterOrder(int index) throws EntryNotFoundException; - public native Entry getMainEntry(); + public native Entry getMainEntry() throws EntryNotFoundException; public native boolean hasMainEntry(); public native Entry getRandomEntry(); diff --git a/lib/src/main/java/org/kiwix/libzim/EntryNotFoundException.java b/lib/src/main/java/org/kiwix/libzim/EntryNotFoundException.java new file mode 100644 index 0000000..9148ee0 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/EntryNotFoundException.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class EntryNotFoundException extends Exception +{ + public EntryNotFoundException(String message) { + super(message); + } +} diff --git a/lib/src/test/org/kiwix/test/libzim/TestArchive.java b/lib/src/test/org/kiwix/test/libzim/TestArchive.java index 1b12839..90ba533 100644 --- a/lib/src/test/org/kiwix/test/libzim/TestArchive.java +++ b/lib/src/test/org/kiwix/test/libzim/TestArchive.java @@ -54,25 +54,25 @@ public class TestArchive public int getArticleCount() { return inner.getArticleCount(); } public int getMediaCount() { return inner.getMediaCount(); } public String getUuid() { return inner.getUuid(); } - public String getMetadata(String name) { return inner.getMetadata(name); } - public TestItem getMetadataItem(String name) { return new TestItem(inner.getMetadataItem(name)); } + public String getMetadata(String name) throws EntryNotFoundException { return inner.getMetadata(name); } + public TestItem getMetadataItem(String name) throws EntryNotFoundException { return new TestItem(inner.getMetadataItem(name)); } public String[] getMetadataKeys() { return inner.getMetadataKeys(); } public TestItem getIllustrationItem(int size) { return new TestItem(inner.getIllustrationItem(size)); } public boolean hasIllustration(int size) { return inner.hasIllustration(size); } public long[] getIllustrationSizes() { return inner.getIllustrationSizes(); } - public TestEntry getEntryByPath(String path) { return new TestEntry(inner.getEntryByPath(path)); } - public TestEntry getEntryByPath(int index) { return new TestEntry(inner.getEntryByPath(index)); } + public TestEntry getEntryByPath(String path) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(path)); } + public TestEntry getEntryByPath(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(index)); } public boolean hasEntryByPath(String path) { return inner.hasEntryByPath(path); } - public TestEntry getEntryByTitle(String title) { return new TestEntry(inner.getEntryByTitle(title)); } - public TestEntry getEntryByTitle(int index) { return new TestEntry(inner.getEntryByTitle(index)); } + public TestEntry getEntryByTitle(String title) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(title)); } + public TestEntry getEntryByTitle(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(index)); } public boolean hasEntryByTitle(String title) { return inner.hasEntryByTitle(title); } - public TestEntry getEntryByClusterOrder(int index) { return new TestEntry(inner.getEntryByClusterOrder(index)); } + public TestEntry getEntryByClusterOrder(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByClusterOrder(index)); } - public TestEntry getMainEntry() { return new TestEntry(inner.getMainEntry()); } + public TestEntry getMainEntry() throws EntryNotFoundException { return new TestEntry(inner.getMainEntry()); } public boolean hasMainEntry() { return inner.hasMainEntry(); } public TestEntry getRandomEntry() { return new TestEntry(inner.getRandomEntry()); } diff --git a/lib/src/test/test.java b/lib/src/test/test.java index bfffb25..61d1342 100644 --- a/lib/src/test/test.java +++ b/lib/src/test/test.java @@ -47,7 +47,7 @@ public class test { } private void testArchive(TestArchive archive) - throws IOException { + throws IOException, EntryNotFoundException { // test the zim file main page title TestEntry mainPage = archive.getMainEntry(); assertTrue(mainPage.isRedirect()); @@ -158,20 +158,24 @@ public class test { // Test invalid path try { archive.getEntryByTitle("Wrong title"); - } catch(Exception e) { + } catch(EntryNotFoundException e) { assertEquals("Cannot find entry", e.getMessage()); + } catch(Exception e) { + fail("ERROR: Must be a EntryNotFoundException."); } try { archive.getEntryByPath("wrong_path.html"); - } catch(Exception e) { + } catch(EntryNotFoundException e) { assertEquals("Cannot find entry", e.getMessage()); + } catch(Exception e) { + fail("ERROR: Must be a EntryNotFoundException."); } } @Test public void testArchiveDirect() - throws JNIKiwixException, IOException, ZimFileFormatException { + throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { TestArchive archive = new TestArchive("small.zim"); testArchive(archive); assertTrue(archive.check()); @@ -200,12 +204,14 @@ public class test { fail("ERROR: Archive created with invalid Zim file!"); } catch (ZimFileFormatException e) { assertEquals("Invalid magic number", e.getMessage()); + } catch(Exception e) { + fail("ERROR: Must be a ZimFileFormatException."); } } @Test public void testArchiveByFd() - throws JNIKiwixException, IOException, ZimFileFormatException { + throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { FileInputStream fis = new FileInputStream("small.zim"); TestArchive archive = new TestArchive(fis.getFD()); testArchive(archive); @@ -216,7 +222,7 @@ public class test { @Test public void testArchiveWithAnEmbeddedArchive() - throws JNIKiwixException, IOException, ZimFileFormatException { + throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException { File plainArchive = new File("small.zim"); FileInputStream fis = new FileInputStream("small.zim.embedded"); TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());