mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-08-03 19:16:10 -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
|
||||
}
|
||||
|
||||
# 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
|
||||
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"
|
||||
|
||||
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 \
|
||||
|| 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.util.*;
|
||||
import org.junit.Test;
|
||||
@ -6,155 +5,154 @@ import static org.junit.Assert.*;
|
||||
import org.kiwix.kiwixlib.*;
|
||||
|
||||
public class test {
|
||||
static {
|
||||
System.loadLibrary("kiwix");
|
||||
}
|
||||
static {
|
||||
System.loadLibrary("kiwix");
|
||||
System.loadLibrary("zim");
|
||||
System.loadLibrary("buildkiwix");
|
||||
}
|
||||
|
||||
private static byte[] getFileContent(String path)
|
||||
throws IOException
|
||||
{
|
||||
File file = new File(path);
|
||||
DataInputStream in = new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
byte[] data = new byte[(int)file.length()];
|
||||
in.read(data);
|
||||
return data;
|
||||
}
|
||||
private static byte[] getFileContent(String path)
|
||||
throws IOException
|
||||
{
|
||||
File file = new File(path);
|
||||
DataInputStream in = new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
byte[] data = new byte[(int)file.length()];
|
||||
in.read(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] getFileContentPartial(String path, int offset, int size)
|
||||
throws IOException
|
||||
{
|
||||
File file = new File(path);
|
||||
DataInputStream in = new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
byte[] data = new byte[size];
|
||||
in.skipBytes(offset);
|
||||
in.read(data, 0, size);
|
||||
return data;
|
||||
}
|
||||
private static byte[] getFileContentPartial(String path, int offset, int size)
|
||||
throws IOException
|
||||
{
|
||||
File file = new File(path);
|
||||
DataInputStream in = new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
byte[] data = new byte[size];
|
||||
in.skipBytes(offset);
|
||||
in.read(data, 0, size);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static String getTextFileContent(String path)
|
||||
throws IOException
|
||||
{
|
||||
return new String(getFileContent(path));
|
||||
}
|
||||
private static String getTextFileContent(String path)
|
||||
throws IOException
|
||||
{
|
||||
return new String(getFileContent(path));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReader()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
@Test
|
||||
public void testReader()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
JNIKiwixReader reader = new JNIKiwixReader("small.zim");
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderByFd()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
FileInputStream fis = new FileInputStream("small.zim");
|
||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
@Test
|
||||
public void testReaderByFd()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
FileInputStream fis = new FileInputStream("small.zim");
|
||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD());
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderWithAnEmbeddedArchive()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
File plainArchive = new File("small.zim");
|
||||
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(45, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
@Test
|
||||
public void testReaderWithAnEmbeddedArchive()
|
||||
throws JNIKiwixException, IOException
|
||||
{
|
||||
File plainArchive = new File("small.zim");
|
||||
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
||||
JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length());
|
||||
assertEquals("Test ZIM file", reader.getTitle());
|
||||
assertEquals(3, reader.getFileSize()); // The file size is in KiB
|
||||
assertEquals("A/main.html", reader.getMainPage());
|
||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
||||
byte[] c = reader.getContent(new JNIKiwixString("A/main.html"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertEquals(s, new String(c));
|
||||
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||
assertEquals(faviconData.length, reader.getArticleSize("I/favicon.png"));
|
||||
c = reader.getContent(new JNIKiwixString("I/favicon.png"),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixString(),
|
||||
new JNIKiwixInt());
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
DirectAccessInfo dai = reader.getDirectAccessInformation("I/favicon.png");
|
||||
assertNotEquals("", dai.filename);
|
||||
c = getFileContentPartial(dai.filename, (int)dai.offset, faviconData.length);
|
||||
assertTrue(Arrays.equals(faviconData, c));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLibrary()
|
||||
throws IOException
|
||||
{
|
||||
Library lib = new Library();
|
||||
Manager manager = new Manager(lib);
|
||||
String content = getTextFileContent("catalog.xml");
|
||||
manager.readOpds(content, "http://localhost");
|
||||
assertEquals(lib.getBookCount(true, true), 1);
|
||||
String[] bookIds = lib.getBooksIds();
|
||||
assertEquals(bookIds.length, 1);
|
||||
Book book = lib.getBookById(bookIds[0]);
|
||||
assertEquals(book.getTitle(), "Test ZIM file");
|
||||
assertEquals(book.getTags(), "unit;test");
|
||||
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
|
||||
assertEquals(book.getUrl(), "http://localhost/small.zim");
|
||||
}
|
||||
@Test
|
||||
public void testLibrary()
|
||||
throws IOException
|
||||
{
|
||||
Library lib = new Library();
|
||||
Manager manager = new Manager(lib);
|
||||
String content = getTextFileContent("catalog.xml");
|
||||
manager.readOpds(content, "http://localhost");
|
||||
assertEquals(lib.getBookCount(true, true), 1);
|
||||
String[] bookIds = lib.getBooksIds();
|
||||
assertEquals(bookIds.length, 1);
|
||||
Book book = lib.getBookById(bookIds[0]);
|
||||
assertEquals(book.getTitle(), "Test ZIM file");
|
||||
assertEquals(book.getTags(), "unit;test");
|
||||
assertEquals(book.getFaviconUrl(), "http://localhost/meta?name=favicon&content=small");
|
||||
assertEquals(book.getUrl(), "http://localhost/small.zim");
|
||||
}
|
||||
|
||||
static
|
||||
public void main(String[] args) {
|
||||
Library lib = new Library();
|
||||
lib.getBookCount(true, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
static
|
||||
public void main(String[] args) {
|
||||
Library lib = new Library();
|
||||
lib.getBookCount(true, true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user