Add classloader test for Java Logger.

This commit is contained in:
Radek Hubner 2024-07-18 18:39:03 +04:00
parent 2f4693ce40
commit eea1dd0ede
2 changed files with 63 additions and 0 deletions

View file

@ -352,6 +352,7 @@ set(JAVA_TEST_CLASSES
src/test/java/org/rocksdb/KeyExistsTest.java
src/test/java/org/rocksdb/KeyMayExistTest.java
src/test/java/org/rocksdb/LRUCacheTest.java
src/test/java/org/rocksdb/LoggerClassloaderTest.java
src/test/java/org/rocksdb/LoggerTest.java
src/test/java/org/rocksdb/MemTableTest.java
src/test/java/org/rocksdb/MemoryUtilTest.java
@ -895,6 +896,9 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy
${ROCKSDBJNI_CLASSES_TEST_DIR}/org/rocksdb/EventListenerClassloaderTest.class
${PROJECT_BINARY_DIR}/java/cl-test/org/rocksdb/EventListenerClassloaderTest.class
COMMAND ${CMAKE_COMMAND} -E copy
${ROCKSDBJNI_CLASSES_TEST_DIR}/org/rocksdb/LoggerClassloaderTest.class
${PROJECT_BINARY_DIR}/java/cl-test/org/rocksdb/LoggerClassloaderTest.class
VERBATIM
)
@ -925,5 +929,9 @@ else()
NAME jtest_org.rocksdb.EventListenerClassloaderTest
COMMAND ${Java_JAVA_EXECUTABLE} -ea -Xcheck:jni -Drocks-jar=${CMAKE_CURRENT_BINARY_DIR}/${ROCKSDB_JAR} -classpath ${JAVA_RUN_TESTCLASSPATH}:${PROJECT_BINARY_DIR}/java/cl-test org.junit.runner.JUnitCore org.rocksdb.EventListenerClassloaderTest
)
add_test(
NAME jtest_org.rocksdb.LoggerClassloaderTest
COMMAND ${Java_JAVA_EXECUTABLE} -ea -Xcheck:jni -Drocks-jar=${CMAKE_CURRENT_BINARY_DIR}/${ROCKSDB_JAR} -classpath ${JAVA_RUN_TESTCLASSPATH}:${PROJECT_BINARY_DIR}/java/cl-test org.junit.runner.JUnitCore org.rocksdb.EventListenerClassloaderTest
)
endif()

View file

@ -0,0 +1,55 @@
package org.rocksdb;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* Only this class can be on default classpath.
* It loads rocksDB code with custom classloader and then test that all
* log levels for log events can be instantiated.
*/
public class LoggerClassloaderTest {
@Rule public TemporaryFolder dbFolder = new TemporaryFolder();
@Test
public void testWithCustomClassLoader() throws Exception {
try {
this.getClass().getClassLoader().loadClass("org.rocksdb.RocksDB");
fail("It looks like RocksDB is on classpath. This test must load RocksDB via custom "
+ "classLoader"
+ " to verify that callback cache all class instances.");
} catch (ClassNotFoundException e) {
;
}
String jarPath = System.getProperty("rocks-jar");
assertThat(jarPath).isNotNull().as("Java property 'rocks-jar' was not setup properly");
Path classesDir = Paths.get(jarPath);
ClassLoader cl = new URLClassLoader(new URL[] {classesDir.toAbsolutePath().toUri().toURL()});
Class rocksDBclazz = cl.loadClass("org.rocksdb.RocksDB");
Method loadLibrary = rocksDBclazz.getMethod("loadLibrary");
loadLibrary.invoke(null);
Class classUnderTest = cl.loadClass("org.rocksdb.LoggerTest");
Method customLogger = classUnderTest.getMethod("customLogger");
Field dbFolderField = classUnderTest.getDeclaredField("dbFolder");
Object testInstance = classUnderTest.getDeclaredConstructor().newInstance();
dbFolderField.set(testInstance, dbFolder);
customLogger.invoke(testInstance);
}
}