1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.crypto;
20
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assumptions.assumeTrue;
25
26 import java.io.File;
27
28 import org.junit.jupiter.api.Disabled;
29 import org.junit.jupiter.api.Test;
30
31
32 public class NativeCodeLoaderTest {
33
34 @Test
35 public void test() {
36 assertTrue(NativeCodeLoader.isNativeCodeLoaded(), "Native (JNI) code loaded successfully");
37 }
38
39 @Test
40 @Disabled("Causes crash on Ubuntu when compiled with Java 17")
41
42
43
44
45
46 public void testCanLoadIfPresent() {
47 assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
48
49 assertNull(NativeCodeLoader.loadLibrary());
50 }
51
52 @Test
53 public void testNativeNotPresent() {
54 assumeTrue(!NativeCodeLoader.isNativeCodeLoaded());
55 assertNotNull(NativeCodeLoader.getLoadingError());
56 }
57
58 @Test
59 public void testNativePresent() {
60 assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
61 assertNull(NativeCodeLoader.getLoadingError());
62 }
63
64 @Test
65 @Disabled("Seems to cause issues with other tests on Linux; disable for now")
66
67
68
69 public void testUnSuccessfulLoad() throws Exception {
70 final String nameKey = System.getProperty(Crypto.LIB_NAME_KEY);
71 final String pathKey = System.getProperty(Crypto.LIB_PATH_KEY);
72
73 final File empty = File.createTempFile("NativeCodeLoaderTest", "tmp");
74 try {
75 System.setProperty(Crypto.LIB_PATH_KEY, empty.getParent());
76 System.setProperty(Crypto.LIB_NAME_KEY, empty.getName());
77 final Throwable result = NativeCodeLoader.loadLibrary();
78 assertNotNull(result);
79 assertTrue(result instanceof UnsatisfiedLinkError);
80 } finally {
81 empty.delete();
82 if (nameKey != null) {
83 System.setProperty(Crypto.LIB_NAME_KEY, nameKey);
84 }
85 if (pathKey != null) {
86 System.setProperty(Crypto.LIB_PATH_KEY, pathKey);
87 }
88 }
89 }
90 }