Skip to content

Commit 9d6ef87

Browse files
committed
Introduce EntryNotFoundException.
1 parent 827ab31 commit 9d6ef87

6 files changed

Lines changed: 58 additions & 24 deletions

File tree

lib/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ String getLibzimFiles() {
342342
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionIterator.java " +
343343
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " +
344344
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " +
345-
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java"
345+
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java " +
346+
"${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java"
346347
}
347348

348349
task buildLinuxBinding(type: Exec) {

lib/src/main/cpp/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ catch(const zim::ZimFileFormatError& e) { \
5151
throwException(env, "java/lang/Exception", e.what()); \
5252
return RET; \
5353
} catch(const zim::EntryNotFound& e) { \
54-
throwException(env, "java/lang/Exception", e.what()); \
54+
throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \
5555
return RET; \
5656
} catch (const std::ios_base::failure& e) { \
5757
throwException(env, "java/io/IOException", e.what()); \

lib/src/main/java/org/kiwix/libzim/Archive.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ public Archive(FileDescriptor fd, long offset, long size)
6060
public native int getArticleCount();
6161
public native int getMediaCount();
6262
public native String getUuid();
63-
public native String getMetadata(String name);
64-
public native Item getMetadataItem(String name);
63+
public native String getMetadata(String name) throws EntryNotFoundException;
64+
public native Item getMetadataItem(String name) throws EntryNotFoundException;
6565
public native String[] getMetadataKeys();
6666

6767
public native Item getIllustrationItem(int size);
6868
public native boolean hasIllustration(int size);
6969
public native long[] getIllustrationSizes();
7070

71-
public native Entry getEntryByPath(String path);
72-
public native Entry getEntryByPath(int index);
71+
public native Entry getEntryByPath(String path) throws EntryNotFoundException;
72+
public native Entry getEntryByPath(int index) throws EntryNotFoundException;
7373
public native boolean hasEntryByPath(String path);
7474

75-
public native Entry getEntryByTitle(String title);
76-
public native Entry getEntryByTitle(int index);
75+
public native Entry getEntryByTitle(String title) throws EntryNotFoundException;
76+
public native Entry getEntryByTitle(int index) throws EntryNotFoundException;
7777
public native boolean hasEntryByTitle(String title);
7878

79-
public native Entry getEntryByClusterOrder(int index);
79+
public native Entry getEntryByClusterOrder(int index) throws EntryNotFoundException;
8080

81-
public native Entry getMainEntry();
81+
public native Entry getMainEntry() throws EntryNotFoundException;
8282
public native boolean hasMainEntry();
8383

8484
public native Entry getRandomEntry();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.org>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
package org.kiwix.libzim;
21+
22+
public class EntryNotFoundException extends Exception
23+
{
24+
public EntryNotFoundException(String message) {
25+
super(message);
26+
}
27+
}

lib/src/test/org/kiwix/test/libzim/TestArchive.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@ public TestArchive(FileDescriptor fd, long offset, long size)
5454
public int getArticleCount() { return inner.getArticleCount(); }
5555
public int getMediaCount() { return inner.getMediaCount(); }
5656
public String getUuid() { return inner.getUuid(); }
57-
public String getMetadata(String name) { return inner.getMetadata(name); }
58-
public TestItem getMetadataItem(String name) { return new TestItem(inner.getMetadataItem(name)); }
57+
public String getMetadata(String name) throws EntryNotFoundException { return inner.getMetadata(name); }
58+
public TestItem getMetadataItem(String name) throws EntryNotFoundException { return new TestItem(inner.getMetadataItem(name)); }
5959
public String[] getMetadataKeys() { return inner.getMetadataKeys(); }
6060

6161
public TestItem getIllustrationItem(int size) { return new TestItem(inner.getIllustrationItem(size)); }
6262
public boolean hasIllustration(int size) { return inner.hasIllustration(size); }
6363
public long[] getIllustrationSizes() { return inner.getIllustrationSizes(); }
6464

65-
public TestEntry getEntryByPath(String path) { return new TestEntry(inner.getEntryByPath(path)); }
66-
public TestEntry getEntryByPath(int index) { return new TestEntry(inner.getEntryByPath(index)); }
65+
public TestEntry getEntryByPath(String path) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(path)); }
66+
public TestEntry getEntryByPath(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(index)); }
6767
public boolean hasEntryByPath(String path) { return inner.hasEntryByPath(path); }
6868

69-
public TestEntry getEntryByTitle(String title) { return new TestEntry(inner.getEntryByTitle(title)); }
70-
public TestEntry getEntryByTitle(int index) { return new TestEntry(inner.getEntryByTitle(index)); }
69+
public TestEntry getEntryByTitle(String title) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(title)); }
70+
public TestEntry getEntryByTitle(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(index)); }
7171
public boolean hasEntryByTitle(String title) { return inner.hasEntryByTitle(title); }
7272

73-
public TestEntry getEntryByClusterOrder(int index) { return new TestEntry(inner.getEntryByClusterOrder(index)); }
73+
public TestEntry getEntryByClusterOrder(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByClusterOrder(index)); }
7474

75-
public TestEntry getMainEntry() { return new TestEntry(inner.getMainEntry()); }
75+
public TestEntry getMainEntry() throws EntryNotFoundException { return new TestEntry(inner.getMainEntry()); }
7676
public boolean hasMainEntry() { return inner.hasMainEntry(); }
7777

7878
public TestEntry getRandomEntry() { return new TestEntry(inner.getRandomEntry()); }

lib/src/test/test.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static String getTextFileContent(String path)
4747
}
4848

4949
private void testArchive(TestArchive archive)
50-
throws IOException {
50+
throws IOException, EntryNotFoundException {
5151
// test the zim file main page title
5252
TestEntry mainPage = archive.getMainEntry();
5353
assertTrue(mainPage.isRedirect());
@@ -158,20 +158,24 @@ private void testArchive(TestArchive archive)
158158
// Test invalid path
159159
try {
160160
archive.getEntryByTitle("Wrong title");
161-
} catch(Exception e) {
161+
} catch(EntryNotFoundException e) {
162162
assertEquals("Cannot find entry", e.getMessage());
163+
} catch(Exception e) {
164+
fail("ERROR: Must be a EntryNotFoundException.");
163165
}
164166

165167
try {
166168
archive.getEntryByPath("wrong_path.html");
167-
} catch(Exception e) {
169+
} catch(EntryNotFoundException e) {
168170
assertEquals("Cannot find entry", e.getMessage());
171+
} catch(Exception e) {
172+
fail("ERROR: Must be a EntryNotFoundException.");
169173
}
170174
}
171175

172176
@Test
173177
public void testArchiveDirect()
174-
throws JNIKiwixException, IOException, ZimFileFormatException {
178+
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
175179
TestArchive archive = new TestArchive("small.zim");
176180
testArchive(archive);
177181
assertTrue(archive.check());
@@ -200,12 +204,14 @@ public void testNotValid() {
200204
fail("ERROR: Archive created with invalid Zim file!");
201205
} catch (ZimFileFormatException e) {
202206
assertEquals("Invalid magic number", e.getMessage());
207+
} catch(Exception e) {
208+
fail("ERROR: Must be a ZimFileFormatException.");
203209
}
204210
}
205211

206212
@Test
207213
public void testArchiveByFd()
208-
throws JNIKiwixException, IOException, ZimFileFormatException {
214+
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
209215
FileInputStream fis = new FileInputStream("small.zim");
210216
TestArchive archive = new TestArchive(fis.getFD());
211217
testArchive(archive);
@@ -216,7 +222,7 @@ public void testArchiveByFd()
216222

217223
@Test
218224
public void testArchiveWithAnEmbeddedArchive()
219-
throws JNIKiwixException, IOException, ZimFileFormatException {
225+
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
220226
File plainArchive = new File("small.zim");
221227
FileInputStream fis = new FileInputStream("small.zim.embedded");
222228
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());

0 commit comments

Comments
 (0)