Skip to content

Commit 3e02f17

Browse files
committed
Copy test from branch Issue#19
zim files has been changed but I don't know why. Change came from commits 3cd8ed9 and 8bff08c but it is not explain what are the changes. @MohitMaliFtechiz ?
1 parent a0d95cb commit 3e02f17

7 files changed

Lines changed: 203 additions & 154 deletions

File tree

lib/src/test/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required (VERSION 3.16)
2+
project (buildkiwix)
3+
4+
add_library(buildkiwix
5+
SHARED
6+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/base64.cpp
7+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/book.cpp
8+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/filter.cpp
9+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixicu.cpp
10+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixreader.cpp
11+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixsearcher.cpp
12+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixserver.cpp
13+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/library.cpp
14+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/manager.cpp
15+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/utils.h
16+
)
17+
18+
find_package(JNI)
19+
20+
if (JNI_FOUND)
21+
message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
22+
message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
23+
endif()
24+
25+
include_directories(
26+
/opt/hostedtoolcache/jdk/11.0.17/x64/include
27+
/opt/hostedtoolcache/jdk/11.0.17/x64/include/linux
28+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp
29+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/libkiwix
30+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/libzim
31+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/javah_generated
32+
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/utils
33+
)
34+
35+
target_link_libraries(buildkiwix
36+
LINK_PUBLIC
37+
${CMAKE_SOURCE_DIR}/../../../../../../build/libkiwix.so
38+
${CMAKE_SOURCE_DIR}/../../../../../../build/libzim.so
39+
)

lib/src/test/compile_and_run_test.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ die()
99
exit 1
1010
}
1111

12+
# This is for building wrapper so file
13+
cmake .
14+
make
15+
16+
# Copy generated .so file to build directory to run test cases
17+
cd ../../../../../../../
18+
./gradlew copyBuildKiwixSoFile
19+
cd app/src/androidTests/java/org/kiwix/kiwixlib/
1220

1321
KIWIX_LIB_JAR=$1
1422
if [ -z $KIWIX_LIB_JAR ]
@@ -32,6 +40,10 @@ javac -g -d . -s . -cp "junit-4.13.jar:$KIWIX_LIB_JAR" test.java \
3240
|| die "Compilation failed"
3341

3442
java -Djava.library.path="$KIWIX_LIB_DIR" \
35-
-cp "junit-4.13.jar:hamcrest-core-1.3.jar:$KIWIX_LIB_JAR:." \
43+
-javaagent:jacoco-0.8.7/lib/jacocoagent.jar -cp "junit-4.13.jar:hamcrest-core-1.3.jar:$KIWIX_LIB_JAR:." \
3644
org.junit.runner.JUnitCore test \
3745
|| die "Unit test failed"
46+
47+
java -jar jacoco-0.8.7/lib/jacococli.jar report jacoco.exec \
48+
--classfiles org/kiwix/kiwixlib/ \
49+
--html ../../../../../../../build/coverage-report --xml coverage.xml
285 KB
Binary file not shown.
554 KB
Binary file not shown.

lib/src/test/small.zim

-41.1 KB
Binary file not shown.

lib/src/test/small.zim.embedded

-41.1 KB
Binary file not shown.

lib/src/test/test.java

Lines changed: 151 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,158 @@
1-
21
import java.io.*;
32
import java.util.*;
43
import org.junit.Test;
54
import static org.junit.Assert.*;
65
import org.kiwix.kiwixlib.*;
76

87
public class test {
9-
static {
10-
System.loadLibrary("kiwix");
11-
}
12-
13-
private static byte[] getFileContent(String path)
14-
throws IOException
15-
{
16-
File file = new File(path);
17-
DataInputStream in = new DataInputStream(
18-
new BufferedInputStream(
19-
new FileInputStream(file)));
20-
byte[] data = new byte[(int)file.length()];
21-
in.read(data);
22-
return data;
23-
}
24-
25-
private static byte[] getFileContentPartial(String path, int offset, int size)
26-
throws IOException
27-
{
28-
File file = new File(path);
29-
DataInputStream in = new DataInputStream(
30-
new BufferedInputStream(
31-
new FileInputStream(file)));
32-
byte[] data = new byte[size];
33-
in.skipBytes(offset);
34-
in.read(data, 0, size);
35-
return data;
36-
}
37-
38-
private static String getTextFileContent(String path)
39-
throws IOException
40-
{
41-
return new String(getFileContent(path));
42-
}
43-
44-
@Test
45-
public void testReader()
46-
throws JNIKiwixException, IOException
47-
{
48-
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
49-
assertEquals("Test ZIM file", reader.getTitle());
50-
assertEquals(45, reader.getFileSize()); // The file size is in KiB
51-
assertEquals("A/main.html", reader.getMainPage());
52-
String s = getTextFileContent("small_zimfile_data/main.html");
53-
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
54-
new JNIKiwixString(),
55-
new JNIKiwixString(),
56-
new JNIKiwixInt());
57-
assertEquals(s, new String(c));
58-
59-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
60-
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
61-
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
62-
new JNIKiwixString(),
63-
new JNIKiwixString(),
64-
new JNIKiwixInt());
65-
assertTrue(Arrays.equals(faviconData, c));
66-
67-
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
68-
assertNotEquals("", dai.filename);
69-
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
70-
assertTrue(Arrays.equals(faviconData, c));
71-
}
72-
73-
@Test
74-
public void testReaderByFd()
75-
throws JNIKiwixException, IOException
76-
{
77-
FileInputStream fis = new FileInputStream("small.zim");
78-
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
79-
assertEquals("Test ZIM file", reader.getTitle());
80-
assertEquals(45, reader.getFileSize()); // The file size is in KiB
81-
assertEquals("A/main.html", reader.getMainPage());
82-
String s = getTextFileContent("small_zimfile_data/main.html");
83-
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
84-
new JNIKiwixString(),
85-
new JNIKiwixString(),
86-
new JNIKiwixInt());
87-
assertEquals(s, new String(c));
88-
89-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
90-
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
91-
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
92-
new JNIKiwixString(),
93-
new JNIKiwixString(),
94-
new JNIKiwixInt());
95-
assertTrue(Arrays.equals(faviconData, c));
96-
97-
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
98-
assertNotEquals("", dai.filename);
99-
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
100-
assertTrue(Arrays.equals(faviconData, c));
101-
}
102-
103-
@Test
104-
public void testReaderWithAnEmbeddedArchive()
105-
throws JNIKiwixException, IOException
106-
{
107-
File plainArchive = new File("small.zim");
108-
FileInputStream fis = new FileInputStream("small.zim.embedded");
109-
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
110-
assertEquals("Test ZIM file", reader.getTitle());
111-
assertEquals(45, reader.getFileSize()); // The file size is in KiB
112-
assertEquals("A/main.html", reader.getMainPage());
113-
String s = getTextFileContent("small_zimfile_data/main.html");
114-
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
115-
new JNIKiwixString(),
116-
new JNIKiwixString(),
117-
new JNIKiwixInt());
118-
assertEquals(s, new String(c));
119-
120-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
121-
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
122-
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
123-
new JNIKiwixString(),
124-
new JNIKiwixString(),
125-
new JNIKiwixInt());
126-
assertTrue(Arrays.equals(faviconData, c));
127-
128-
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
129-
assertNotEquals("", dai.filename);
130-
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
131-
assertTrue(Arrays.equals(faviconData, c));
132-
}
133-
134-
@Test
135-
public void testLibrary()
136-
throws IOException
137-
{
138-
Library lib = new Library();
139-
Manager manager = new Manager(lib);
140-
String content = getTextFileContent("catalog.xml");
141-
manager.readOpds(content, "http://localhost");
142-
assertEquals(lib.getBookCount(true, true), 1);
143-
String[] bookIds = lib.getBooksIds();
144-
assertEquals(bookIds.length, 1);
145-
Book book = lib.getBookById(bookIds[0]);
146-
assertEquals(book.getTitle(), "Test ZIM file");
147-
assertEquals(book.getTags(), "unit;test");
148-
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
149-
assertEquals(book.getUrl(), "http://localhost/small.zim");
150-
}
151-
152-
static
153-
public void main(String[] args) {
154-
Library lib = new Library();
155-
lib.getBookCount(true, true);
156-
}
157-
158-
159-
160-
}
8+
static {
9+
System.loadLibrary("kiwix");
10+
System.loadLibrary("zim");
11+
System.loadLibrary("buildkiwix");
12+
}
13+
14+
private static byte[] getFileContent(String path)
15+
throws IOException
16+
{
17+
File file = new File(path);
18+
DataInputStream in = new DataInputStream(
19+
new BufferedInputStream(
20+
new FileInputStream(file)));
21+
byte[] data = new byte[(int)file.length()];
22+
in.read(data);
23+
return data;
24+
}
25+
26+
private static byte[] getFileContentPartial(String path, int offset, int size)
27+
throws IOException
28+
{
29+
File file = new File(path);
30+
DataInputStream in = new DataInputStream(
31+
new BufferedInputStream(
32+
new FileInputStream(file)));
33+
byte[] data = new byte[size];
34+
in.skipBytes(offset);
35+
in.read(data, 0, size);
36+
return data;
37+
}
38+
39+
private static String getTextFileContent(String path)
40+
throws IOException
41+
{
42+
return new String(getFileContent(path));
43+
}
44+
45+
@Test
46+
public void testReader()
47+
throws JNIKiwixException, IOException
48+
{
49+
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
50+
assertEquals("Test ZIM file", reader.getTitle());
51+
assertEquals(3, reader.getFileSize()); // The file size is in KiB
52+
assertEquals("A/main.html", reader.getMainPage());
53+
String s = getTextFileContent("small_zimfile_data/main.html");
54+
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
55+
new JNIKiwixString(),
56+
new JNIKiwixString(),
57+
new JNIKiwixInt());
58+
assertEquals(s, new String(c));
59+
60+
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
61+
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
62+
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
63+
new JNIKiwixString(),
64+
new JNIKiwixString(),
65+
new JNIKiwixInt());
66+
assertTrue(Arrays.equals(faviconData, c));
67+
68+
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
69+
assertNotEquals("", dai.filename);
70+
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
71+
assertTrue(Arrays.equals(faviconData, c));
72+
}
73+
74+
@Test
75+
public void testReaderByFd()
76+
throws JNIKiwixException, IOException
77+
{
78+
FileInputStream fis = new FileInputStream("small.zim");
79+
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
80+
assertEquals("Test ZIM file", reader.getTitle());
81+
assertEquals(3, reader.getFileSize()); // The file size is in KiB
82+
assertEquals("A/main.html", reader.getMainPage());
83+
String s = getTextFileContent("small_zimfile_data/main.html");
84+
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
85+
new JNIKiwixString(),
86+
new JNIKiwixString(),
87+
new JNIKiwixInt());
88+
assertEquals(s, new String(c));
89+
90+
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
91+
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
92+
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
93+
new JNIKiwixString(),
94+
new JNIKiwixString(),
95+
new JNIKiwixInt());
96+
assertTrue(Arrays.equals(faviconData, c));
97+
98+
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
99+
assertNotEquals("", dai.filename);
100+
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
101+
assertTrue(Arrays.equals(faviconData, c));
102+
}
103+
104+
@Test
105+
public void testReaderWithAnEmbeddedArchive()
106+
throws JNIKiwixException, IOException
107+
{
108+
File plainArchive = new File("small.zim");
109+
FileInputStream fis = new FileInputStream("small.zim.embedded");
110+
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
111+
assertEquals("Test ZIM file", reader.getTitle());
112+
assertEquals(3, reader.getFileSize()); // The file size is in KiB
113+
assertEquals("A/main.html", reader.getMainPage());
114+
String s = getTextFileContent("small_zimfile_data/main.html");
115+
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
116+
new JNIKiwixString(),
117+
new JNIKiwixString(),
118+
new JNIKiwixInt());
119+
assertEquals(s, new String(c));
120+
121+
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
122+
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
123+
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
124+
new JNIKiwixString(),
125+
new JNIKiwixString(),
126+
new JNIKiwixInt());
127+
assertTrue(Arrays.equals(faviconData, c));
128+
129+
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
130+
assertNotEquals("", dai.filename);
131+
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
132+
assertTrue(Arrays.equals(faviconData, c));
133+
}
134+
135+
@Test
136+
public void testLibrary()
137+
throws IOException
138+
{
139+
Library lib = new Library();
140+
Manager manager = new Manager(lib);
141+
String content = getTextFileContent("catalog.xml");
142+
manager.readOpds(content, "http://localhost");
143+
assertEquals(lib.getBookCount(true, true), 1);
144+
String[] bookIds = lib.getBooksIds();
145+
assertEquals(bookIds.length, 1);
146+
Book book = lib.getBookById(bookIds[0]);
147+
assertEquals(book.getTitle(), "Test ZIM file");
148+
assertEquals(book.getTags(), "unit;test");
149+
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
150+
assertEquals(book.getUrl(), "http://localhost/small.zim");
151+
}
152+
153+
static
154+
public void main(String[] args) {
155+
Library lib = new Library();
156+
lib.getBookCount(true, true);
157+
}
158+
}

0 commit comments

Comments
 (0)