From d4f2f82e982391c2024a092484f62b78da6b7732 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Jun 2023 17:19:40 +0200 Subject: [PATCH] Introduce test package. Jacoco don't track native methods, so our code coverage don't include all the native methods, and it is exactly want we want to check. By introducing a test package wrapping all native methods with a small java function, we have the coverage of the wrapping methods. --- lib/build.gradle | 4 +- .../org/kiwix/test/libzim/TestArchive.java | 97 +++++++++++++++++++ lib/src/test/test.java | 11 ++- 3 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 lib/src/test/org/kiwix/test/libzim/TestArchive.java diff --git a/lib/build.gradle b/lib/build.gradle index 7a051d5..a58f976 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -274,7 +274,7 @@ task testSourceJar(type: Jar) { task compileTestFile(type: JavaCompile) { dependsOn testSourceJar - source = file('src/test/test.java') + source = file('src/test') destinationDirectory = file("$buildDir") classpath = files("src/test/junit-4.13.jar" , "src/test/hamcrest-core-1.4.jar", "build/libs/test-sources.jar") } @@ -298,7 +298,7 @@ task createCodeCoverageReport(type: JavaExec) { main = 'org.jacoco.cli.internal.Main' args = [ 'report', '../../build/jacoco/jacoco.exec', - '--classfiles', 'java/org/kiwix/libkiwix/', '--classfiles', 'java/org/kiwix/libzim/', + '--classfiles', 'java/org/kiwix', '--classfiles', '../../build/org/kiwix', '--html', '../../build/coverage-report', '--xml', '../../build/coverage.xml' ] } diff --git a/lib/src/test/org/kiwix/test/libzim/TestArchive.java b/lib/src/test/org/kiwix/test/libzim/TestArchive.java new file mode 100644 index 0000000..5546075 --- /dev/null +++ b/lib/src/test/org/kiwix/test/libzim/TestArchive.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2022 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.test.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Entry; +import org.kiwix.libzim.Item; +import org.kiwix.libzim.EntryIterator; +import org.kiwix.libzim.Archive; +import java.io.FileDescriptor; +import org.kiwix.libzim.*; + +public class TestArchive +{ + private Archive inner; + + public TestArchive(String filename) throws ZimFileFormatException + { + inner = new Archive(filename); + } + + public TestArchive(FileDescriptor fd) throws ZimFileFormatException + { + inner = new Archive(fd); + } + + public TestArchive(FileDescriptor fd, long offset, long size) + throws ZimFileFormatException + { + inner = new Archive(fd, offset, size); + } + + public String getFilename() { return inner.getFilename(); } + public long getFilesize() { return inner.getFilesize(); } + public int getAllEntryCount() { return inner.getAllEntryCount(); } + public int getEntryCount() { return inner.getEntryCount(); } + 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 Item getMetadataItem(String name) { return inner.getMetadataItem(name); } + public String[] getMetadataKeys() { return inner.getMetadataKeys(); } + + public Item getIllustrationItem(int size) { return inner.getIllustrationItem(size); } + public boolean hasIllustration(int size) { return inner.hasIllustration(size); } + public long[] getIllustrationSizes() { return inner.getIllustrationSizes(); } + + public Entry getEntryByPath(String path) { return inner.getEntryByPath(path); } + public Entry getEntryByPath(int index) { return inner.getEntryByPath(index); } + public boolean hasEntryByPath(String path) { return inner.hasEntryByPath(path); } + + public Entry getEntryByTitle(String title) { return inner.getEntryByTitle(title); } + public Entry getEntryByTitle(int index) { return inner.getEntryByTitle(index); } + public boolean hasEntryByTitle(String title) { return inner.hasEntryByTitle(title); } + + public Entry getEntryByClusterOrder(int index) { return inner.getEntryByClusterOrder(index); } + + public Entry getMainEntry() { return inner.getMainEntry(); } + public boolean hasMainEntry() { return inner.hasMainEntry(); } + + public Entry getRandomEntry() { return inner.getRandomEntry(); } + + public boolean hasFulltextIndex() { return inner.hasFulltextIndex(); } + public boolean hasTitleIndex() { return inner.hasTitleIndex(); } + + public boolean hasChecksum() { return inner.hasChecksum(); } + public String getChecksum() { return inner.getChecksum(); } + public boolean check() { return inner.check(); } + + public boolean isMultiPart() { return inner.isMultiPart(); } + public boolean hasNewNamespaceScheme() { return inner.hasNewNamespaceScheme(); } + + public EntryIterator iterByPath() { return inner.iterByPath(); } + public EntryIterator iterByTitle() { return inner.iterByTitle(); } + public EntryIterator iterEfficient() { return inner.iterEfficient(); } + public EntryIterator findByPath(String path) { return inner.findByPath(path); } + public EntryIterator findByTitle(String path) { return inner.findByTitle(path); } + + public void dispose() { inner.dispose(); } +} diff --git a/lib/src/test/test.java b/lib/src/test/test.java index a95a1e2..8602aa2 100644 --- a/lib/src/test/test.java +++ b/lib/src/test/test.java @@ -7,6 +7,7 @@ import static org.junit.Assert.*; import org.kiwix.libkiwix.*; import org.kiwix.libzim.*; +import org.kiwix.test.libzim.*; public class test { static { @@ -44,7 +45,7 @@ public class test { return new String(getFileContent(path)); } - private void testArchive(Archive archive) + private void testArchive(TestArchive archive) throws IOException { // test the zim file main page title assertEquals("Test ZIM file", archive.getMainEntry().getTitle()); @@ -74,14 +75,14 @@ public class test { @Test public void testArchiveDirect() throws JNIKiwixException, IOException, ZimFileFormatException { - Archive archive = new Archive("small.zim"); + TestArchive archive = new TestArchive("small.zim"); testArchive(archive); archive.dispose(); // test reader with invalid zim file String zimFile = "test.zim"; try { - Archive archive1 = new Archive(zimFile); + TestArchive archive1 = new TestArchive(zimFile); fail("ERROR: Archive created with invalid Zim file!"); } catch (ZimFileFormatException zimFileFormatException) { assertEquals("Cannot open zimfile " + zimFile, zimFileFormatException.getMessage()); @@ -92,7 +93,7 @@ public class test { public void testArchiveByFd() throws JNIKiwixException, IOException, ZimFileFormatException { FileInputStream fis = new FileInputStream("small.zim"); - Archive archive = new Archive(fis.getFD()); + TestArchive archive = new TestArchive(fis.getFD()); testArchive(archive); archive.dispose(); } @@ -102,7 +103,7 @@ public class test { throws JNIKiwixException, IOException, ZimFileFormatException { File plainArchive = new File("small.zim"); FileInputStream fis = new FileInputStream("small.zim.embedded"); - Archive archive = new Archive(fis.getFD(), 8, plainArchive.length()); + TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length()); testArchive(archive); archive.dispose(); }