From ef4715b94b3cbaef60eb2119cc7dabb4cb0e6482 Mon Sep 17 00:00:00 2001 From: Michael Tucek Date: Tue, 4 May 2021 12:03:53 +0200 Subject: [PATCH] #3758 - removed maven dependency for JNI test - re-enabled test - TODO fix test --- scripts/cmake/ElektraClasspath.cmake | 8 ---- src/bindings/jna/README.md | 66 ++++------------------------ src/bindings/jna/java-library.gradle | 11 +++++ src/plugins/jni/CMakeLists.txt | 35 +++++---------- src/plugins/jni/testmod_jni.h.in | 2 +- 5 files changed, 33 insertions(+), 89 deletions(-) delete mode 100644 scripts/cmake/ElektraClasspath.cmake diff --git a/scripts/cmake/ElektraClasspath.cmake b/scripts/cmake/ElektraClasspath.cmake deleted file mode 100644 index 0ee2c67fd5d..00000000000 --- a/scripts/cmake/ElektraClasspath.cmake +++ /dev/null @@ -1,8 +0,0 @@ -set (SOURCE_FILE "${SOURCE_PATH}/testmod_jni.h.in") -set (DESTINATION_FILE "${DESTINATION_PATH}/testmod_jni.h") -set (CLASSPATH_FILE "${LIBELEKTRA5J_PATH}/classpath.txt") - -set (libelektra_jar ${LIBELEKTRA5J_PATH}/target/libelektra5j-${KDB_VERSION}.jar) -file (READ "${LIBELEKTRA5J_PATH}/classpath.txt" jna) - -configure_file ("${SOURCE_FILE}" "${DESTINATION_FILE}") diff --git a/src/bindings/jna/README.md b/src/bindings/jna/README.md index ca179bffec1..6684096378d 100644 --- a/src/bindings/jna/README.md +++ b/src/bindings/jna/README.md @@ -20,12 +20,12 @@ you enable the JNA bindings, e.g., with `cmake -DBINDINGS=jna`, see also [COMPILE](/doc/COMPILE.md#bindings). Internally, Gradle will be used to actually compile the plugin. -Please note that the [jni plugin](/src/plugins/jni) serves a different purpose. We -use the jni plugin to develop plugins for Elektra itself, whereas the JNA -bindings allow to use Elektra to access configuration in Java projects. The jni +Please note that the [JNI plugin](/src/plugins/jni) serves a different purpose. We +use the JNI plugin to develop plugins for Elektra itself, whereas the JNA +bindings allow to use Elektra to access configuration in Java projects. The JNI plugin is _not_ required for the JNA bindings to work. But, to develop -jni plugins, JNA can be used. [Here](libelektra/plugin) are example -plugins, which need jni at runtime. +JNI plugins, JNA can be used. [Here](libelektra/plugin) are example +plugins, which need JNI at runtime. ### Command line @@ -168,59 +168,11 @@ which can be used to save and load `.properties` files into Elektra. ### Command Line -You can run unit tests after importing jUnit, JNA and the libelektra java -library into a project (eclipse, netbeans, intelliJ, ...). +You can run unit tests by invoking: -Tested library versions are: - -- JNA: 4.5.0 -- jUnit: 4.12 [jUnit 3 is not supported] -- hamcrest-core: 1.13 (required by newer jUnit versions) - -Tested JDK versions are: - -- Oracle JDK 1.8.0_112 on macOS 10.12 Sierra -- OpenJDK 1.8.0_121 on Arch Linux -- OpenJDK 1.9 on Ubuntu -- Oracle JDK 1.9 build 9+181 on macOS 10.12 Sierra - -Both libraries should work on version 4.0 too, though. - -It should also be possible to run the tests by command line: - -1. Compile the library and tests (run in root directory; make sure junit4 and - jna are installed and/or path is correct). Execute the following commands inside - the libelektra folder: - - ```sh - mkdir ./target (if it does not exist yet) - javac -cp -d \ - ./target src/main/java/org/libelektra/*.java \ - src/main/java/org/libelektra/plugin/*.java \ - src/test/java/org/libelektra/*.java - ``` - - If you copied the jna.jar, junit.jar and hamcrest-core.jar directly to the - jna directory, the correct path would be `./jna.jar:./junit.jar:./hamcrest-core.jar` - (separated by : on Mac and Linux, by ; on Windows), otherwise specify the - appropriate locations. - - For Linux users they are usually in `/usr/share/java/jna.jar:/usr/share/java/junit4.jar` - -2. Run all jUnit tests (please note that the -cp parameter now also has to - include the target directory we created in the first step, where the compiled - classfiles are): - - ```sh - java -cp org.junit.runner.JUnitCore org.libelektra.AllTests - ``` - - Or run all tests on their own: - - ```sh - java -cp org.junit.runner.JUnitCore org.libelektra.KeyTest - java -cp org.junit.runner.JUnitCore org.libelektra.KeySetTest - ``` +```sh +./gradlew test +``` ## Limitations diff --git a/src/bindings/jna/java-library.gradle b/src/bindings/jna/java-library.gradle index d463e43ee0c..e35b926d0b2 100644 --- a/src/bindings/jna/java-library.gradle +++ b/src/bindings/jna/java-library.gradle @@ -3,3 +3,14 @@ apply plugin: 'java-library' // apply common java project cofiguration apply from: "$rootProject.projectDir/java-shared.gradle" + +// add task to generate fat jar (includes all runtime dependencies) +// (primarily intended for using JNA binding for testing JNI plugin interface) +task fatJar(type: Jar) { + classifier = 'all' + from { + configurations.runtimeClasspath.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) } + } + with jar +} +assemble.dependsOn fatJar \ No newline at end of file diff --git a/src/plugins/jni/CMakeLists.txt b/src/plugins/jni/CMakeLists.txt index d547e7ad6a1..2d2c6b45ccb 100644 --- a/src/plugins/jni/CMakeLists.txt +++ b/src/plugins/jni/CMakeLists.txt @@ -40,29 +40,18 @@ add_plugin ( if (ADDTESTING_PHASE) check_binding_was_added ("jna" BINDING_WAS_ADDED) - find_program (MAVEN_EXECUTABLE mvn) - if (ENABLE_BROKEN_TESTS) - if (BUILD_TESTING - AND BINDING_WAS_ADDED - AND MAVEN_EXECUTABLE) - add_plugintest (jni MEMLEAK) - include_directories (${CMAKE_CURRENT_BINARY_DIR}) + if (BUILD_TESTING AND BINDING_WAS_ADDED) + add_plugintest (jni MEMLEAK) + include_directories (${CMAKE_CURRENT_BINARY_DIR}) - # Generate header file - set (LIBELEKTRA5J_PATH ${CMAKE_BINARY_DIR}/src/bindings/jna/libelektra) - add_custom_command ( - COMMAND ${MAVEN_EXECUTABLE} ARGS dependency:build-classpath -q -Dmdep.outputFile=classpath.txt - COMMAND - ${CMAKE_COMMAND} ARGS -D SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR} -D - DESTINATION_PATH=${CMAKE_CURRENT_BINARY_DIR} -D LIBELEKTRA5J_PATH=${LIBELEKTRA5J_PATH} -D - KDB_VERSION=${KDB_VERSION} -P ${CMAKE_SOURCE_DIR}/scripts/cmake/ElektraClasspath.cmake - OUTPUT "${LIBELEKTRA5J_PATH}/classpath.txt" "${CMAKE_CURRENT_BINARY_DIR}/testmod_jni.h" - WORKING_DIRECTORY ${LIBELEKTRA5J_PATH} - DEPENDS "${LIBELEKTRA5J_PATH}/pom.xml jna") - add_custom_target (jni_header DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/testmod_jni.h") - else () - message (WARNING "jna bindings are required for testing, test deactivated") - endif () - endif (ENABLE_BROKEN_TESTS) + # Generate header file + set (libelektra_jar_all ${CMAKE_BINARY_DIR}/src/bindings/jna/libelektra/build/libs/libelektra-${KDB_VERSION}-all.jar) + configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/testmod_jni.h.in" "${CMAKE_CURRENT_BINARY_DIR}/testmod_jni.h") + + add_custom_target (jni_header DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/testmod_jni.h") + message (INFO "JNI test activated.") + else () + message (WARNING "JNA bindings are required for testing but binding was not enabled. JNI test deactivated.") + endif () endif () diff --git a/src/plugins/jni/testmod_jni.h.in b/src/plugins/jni/testmod_jni.h.in index 11e3b8b1d09..ec4619f0c52 100644 --- a/src/plugins/jni/testmod_jni.h.in +++ b/src/plugins/jni/testmod_jni.h.in @@ -9,6 +9,6 @@ #ifndef TESTMOD_JNI_H_IN #define TESTMOD_JNI_H_IN -#define CLASSPATH "@jna@:@libelektra_jar@" +#define CLASSPATH "@libelektra_jar_all@" #endif