mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-08-04 03:26:08 -04:00
Copy test from branch Issue#19
zim files has been changed but I don't know why. Change came from commits 3cd8ed98 and 8bff08ca but it is not explain what are the changes. @MohitMaliFtechiz ?
This commit is contained in:
parent
a0d95cb9c6
commit
3e02f1733f
39
lib/src/test/CMakeLists.txt
Normal file
39
lib/src/test/CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
cmake_minimum_required (VERSION 3.16)
|
||||||
|
project (buildkiwix)
|
||||||
|
|
||||||
|
add_library(buildkiwix
|
||||||
|
SHARED
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/base64.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/book.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/filter.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixicu.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixreader.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixsearcher.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/kiwixserver.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/library.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/manager.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/utils.h
|
||||||
|
)
|
||||||
|
|
||||||
|
find_package(JNI)
|
||||||
|
|
||||||
|
if (JNI_FOUND)
|
||||||
|
message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
|
||||||
|
message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
/opt/hostedtoolcache/jdk/11.0.17/x64/include
|
||||||
|
/opt/hostedtoolcache/jdk/11.0.17/x64/include/linux
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/libkiwix
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/libzim
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/javah_generated
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../main/cpp/include/utils
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(buildkiwix
|
||||||
|
LINK_PUBLIC
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../../build/libkiwix.so
|
||||||
|
${CMAKE_SOURCE_DIR}/../../../../../../build/libzim.so
|
||||||
|
)
|
@ -9,6 +9,14 @@ die()
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# This is for building wrapper so file
|
||||||
|
cmake .
|
||||||
|
make
|
||||||
|
|
||||||
|
# Copy generated .so file to build directory to run test cases
|
||||||
|
cd ../../../../../../../
|
||||||
|
./gradlew copyBuildKiwixSoFile
|
||||||
|
cd app/src/androidTests/java/org/kiwix/kiwixlib/
|
||||||
|
|
||||||
KIWIX_LIB_JAR=$1
|
KIWIX_LIB_JAR=$1
|
||||||
if [ -z $KIWIX_LIB_JAR ]
|
if [ -z $KIWIX_LIB_JAR ]
|
||||||
@ -32,6 +40,10 @@ javac -g -d . -s . -cp "junit-4.13.jar:$KIWIX_LIB_JAR" test.java \
|
|||||||
|| die "Compilation failed"
|
|| die "Compilation failed"
|
||||||
|
|
||||||
java -Djava.library.path="$KIWIX_LIB_DIR" \
|
java -Djava.library.path="$KIWIX_LIB_DIR" \
|
||||||
-cp "junit-4.13.jar:hamcrest-core-1.3.jar:$KIWIX_LIB_JAR:." \
|
-javaagent:jacoco-0.8.7/lib/jacocoagent.jar -cp "junit-4.13.jar:hamcrest-core-1.3.jar:$KIWIX_LIB_JAR:." \
|
||||||
org.junit.runner.JUnitCore test \
|
org.junit.runner.JUnitCore test \
|
||||||
|| die "Unit test failed"
|
|| die "Unit test failed"
|
||||||
|
|
||||||
|
java -jar jacoco-0.8.7/lib/jacococli.jar report jacoco.exec \
|
||||||
|
--classfiles org/kiwix/kiwixlib/ \
|
||||||
|
--html ../../../../../../../build/coverage-report --xml coverage.xml
|
BIN
lib/src/test/jacoco-0.8.7/lib/jacocoagent.jar
Normal file
BIN
lib/src/test/jacoco-0.8.7/lib/jacocoagent.jar
Normal file
Binary file not shown.
BIN
lib/src/test/jacoco-0.8.7/lib/jacococli.jar
Normal file
BIN
lib/src/test/jacoco-0.8.7/lib/jacococli.jar
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -6,155 +5,154 @@ import static org.junit.Assert.*;
|
|||||||
import org.kiwix.kiwixlib.*;
|
import org.kiwix.kiwixlib.*;
|
||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
static {
|
static {
|
||||||
System.loadLibrary("kiwix");
|
System.loadLibrary("kiwix");
|
||||||
}
|
System.loadLibrary("zim");
|
||||||
|
System.loadLibrary("buildkiwix");
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] getFileContent(String path)
|
private static byte[] getFileContent(String path)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
DataInputStream in = new DataInputStream(
|
DataInputStream in = new DataInputStream(
|
||||||
new BufferedInputStream(
|
new BufferedInputStream(
|
||||||
new FileInputStream(file)));
|
new FileInputStream(file)));
|
||||||
byte[] data = new byte[(int)file.length()];
|
byte[] data = new byte[(int)file.length()];
|
||||||
in.read(data);
|
in.read(data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getFileContentPartial(String path, int offset, int size)
|
private static byte[] getFileContentPartial(String path, int offset, int size)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
DataInputStream in = new DataInputStream(
|
DataInputStream in = new DataInputStream(
|
||||||
new BufferedInputStream(
|
new BufferedInputStream(
|
||||||
new FileInputStream(file)));
|
new FileInputStream(file)));
|
||||||
byte[] data = new byte[size];
|
byte[] data = new byte[size];
|
||||||
in.skipBytes(offset);
|
in.skipBytes(offset);
|
||||||
in.read(data, 0, size);
|
in.read(data, 0, size);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getTextFileContent(String path)
|
private static String getTextFileContent(String path)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
return new String(getFileContent(path));
|
return new String(getFileContent(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReader()
|
public void testReader()
|
||||||
throws JNIKiwixException, IOException
|
throws JNIKiwixException, IOException
|
||||||
{
|
{
|
||||||
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
|
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
|
||||||
assertEquals("Test ZIM file", reader.getTitle());
|
assertEquals("Test ZIM file", reader.getTitle());
|
||||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||||
assertEquals("A/main.html", reader.getMainPage());
|
assertEquals("A/main.html", reader.getMainPage());
|
||||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertEquals(s, new String(c));
|
assertEquals(s, new String(c));
|
||||||
|
|
||||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
|
|
||||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||||
assertNotEquals("", dai.filename);
|
assertNotEquals("", dai.filename);
|
||||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReaderByFd()
|
public void testReaderByFd()
|
||||||
throws JNIKiwixException, IOException
|
throws JNIKiwixException, IOException
|
||||||
{
|
{
|
||||||
FileInputStream fis = new FileInputStream("small.zim");
|
FileInputStream fis = new FileInputStream("small.zim");
|
||||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
|
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
|
||||||
assertEquals("Test ZIM file", reader.getTitle());
|
assertEquals("Test ZIM file", reader.getTitle());
|
||||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||||
assertEquals("A/main.html", reader.getMainPage());
|
assertEquals("A/main.html", reader.getMainPage());
|
||||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertEquals(s, new String(c));
|
assertEquals(s, new String(c));
|
||||||
|
|
||||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
|
|
||||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||||
assertNotEquals("", dai.filename);
|
assertNotEquals("", dai.filename);
|
||||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReaderWithAnEmbeddedArchive()
|
public void testReaderWithAnEmbeddedArchive()
|
||||||
throws JNIKiwixException, IOException
|
throws JNIKiwixException, IOException
|
||||||
{
|
{
|
||||||
File plainArchive = new File("small.zim");
|
File plainArchive = new File("small.zim");
|
||||||
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
||||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
|
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
|
||||||
assertEquals("Test ZIM file", reader.getTitle());
|
assertEquals("Test ZIM file", reader.getTitle());
|
||||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||||
assertEquals("A/main.html", reader.getMainPage());
|
assertEquals("A/main.html", reader.getMainPage());
|
||||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertEquals(s, new String(c));
|
assertEquals(s, new String(c));
|
||||||
|
|
||||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixString(),
|
new JNIKiwixString(),
|
||||||
new JNIKiwixInt());
|
new JNIKiwixInt());
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
|
|
||||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||||
assertNotEquals("", dai.filename);
|
assertNotEquals("", dai.filename);
|
||||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||||
assertTrue(Arrays.equals(faviconData, c));
|
assertTrue(Arrays.equals(faviconData, c));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLibrary()
|
public void testLibrary()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Library lib = new Library();
|
Library lib = new Library();
|
||||||
Manager manager = new Manager(lib);
|
Manager manager = new Manager(lib);
|
||||||
String content = getTextFileContent("catalog.xml");
|
String content = getTextFileContent("catalog.xml");
|
||||||
manager.readOpds(content, "http://localhost");
|
manager.readOpds(content, "http://localhost");
|
||||||
assertEquals(lib.getBookCount(true, true), 1);
|
assertEquals(lib.getBookCount(true, true), 1);
|
||||||
String[] bookIds = lib.getBooksIds();
|
String[] bookIds = lib.getBooksIds();
|
||||||
assertEquals(bookIds.length, 1);
|
assertEquals(bookIds.length, 1);
|
||||||
Book book = lib.getBookById(bookIds[0]);
|
Book book = lib.getBookById(bookIds[0]);
|
||||||
assertEquals(book.getTitle(), "Test ZIM file");
|
assertEquals(book.getTitle(), "Test ZIM file");
|
||||||
assertEquals(book.getTags(), "unit;test");
|
assertEquals(book.getTags(), "unit;test");
|
||||||
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
|
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
|
||||||
assertEquals(book.getUrl(), "http://localhost/small.zim");
|
assertEquals(book.getUrl(), "http://localhost/small.zim");
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
public void main(String[] args) {
|
public void main(String[] args) {
|
||||||
Library lib = new Library();
|
Library lib = new Library();
|
||||||
lib.getBookCount(true, true);
|
lib.getBookCount(true, true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user