|
18 | 18 | public class EventListenerClassloaderTest {
|
19 | 19 | @Test
|
20 | 20 | public void testCallback() throws Exception {
|
21 |
| - try { |
22 |
| - this.getClass().getClassLoader().loadClass("org.rocksdb.RocksDB"); |
23 |
| - fail("It looks like RocksDB is on classpath. This test must load RocksDB via custom " |
24 |
| - + "classLoader" |
25 |
| - + " to verify that callback cache all class instances."); |
26 |
| - } catch (ClassNotFoundException e) { |
27 |
| - ; |
28 |
| - } |
| 21 | + assertThat(hasRocksDBonPath()) |
| 22 | + .as("It looks like RocksDB is on classpath. " |
| 23 | + + "This test must load RocksDB via custom " |
| 24 | + + "classLoader to verify that callback cache all class instances.") |
| 25 | + .isFalse(); |
29 | 26 |
|
30 | 27 | String jarPath = System.getProperty("rocks-jar");
|
31 |
| - assertThat(jarPath).isNotNull().as("Java property 'rocks-jar' was not setup properly"); |
| 28 | + assertThat(jarPath).as("Java property 'rocks-jar' was not setup properly").isNotNull(); |
32 | 29 |
|
33 | 30 | Path classesDir = Paths.get(jarPath);
|
34 |
| - ClassLoader cl = new URLClassLoader(new URL[] {classesDir.toAbsolutePath().toUri().toURL()}); |
| 31 | + try (URLClassLoader cl = |
| 32 | + new URLClassLoader(new URL[] {classesDir.toAbsolutePath().toUri().toURL()})) { |
| 33 | + Class<?> rocksDBclazz = cl.loadClass("org.rocksdb.RocksDB"); |
| 34 | + Method loadLibrary = rocksDBclazz.getMethod("loadLibrary"); |
| 35 | + loadLibrary.invoke(null); |
35 | 36 |
|
36 |
| - Class rocksDBclazz = cl.loadClass("org.rocksdb.RocksDB"); |
37 |
| - Method loadLibrary = rocksDBclazz.getMethod("loadLibrary"); |
38 |
| - loadLibrary.invoke(null); |
| 37 | + Class<?> testableEventListenerClazz = cl.loadClass("org.rocksdb.test.TestableEventListener"); |
| 38 | + Method invokeAllCallbacksInThread = |
| 39 | + testableEventListenerClazz.getMethod("invokeAllCallbacksInThread"); |
| 40 | + Object instance = testableEventListenerClazz.getDeclaredConstructor().newInstance(); |
| 41 | + invokeAllCallbacksInThread.invoke(instance); |
| 42 | + } |
| 43 | + } |
39 | 44 |
|
40 |
| - Class testableEventListenerClazz = cl.loadClass("org.rocksdb.test.TestableEventListener"); |
41 |
| - Method invokeAllCallbacksInThread = |
42 |
| - testableEventListenerClazz.getMethod("invokeAllCallbacksInThread"); |
43 |
| - Object instance = testableEventListenerClazz.getDeclaredConstructor().newInstance(); |
44 |
| - invokeAllCallbacksInThread.invoke(instance); |
| 45 | + private boolean hasRocksDBonPath() { |
| 46 | + try { |
| 47 | + this.getClass().getClassLoader().loadClass("org.rocksdb.RocksDB"); |
| 48 | + return true; |
| 49 | + } catch (ClassNotFoundException e) { |
| 50 | + return false; |
| 51 | + } |
45 | 52 | }
|
46 | 53 | }
|
0 commit comments