MCPcopy Create free account
hub / github.com/java-native-access/jna / JNALoadTest

Class JNALoadTest

test/com/sun/jna/JNALoadTest.java:41–276  ·  view source on GitHub ↗

Test loading and unloading native support from various locations. Note that no JNI classes are directly referenced in these tests.

Source from the content-addressed store, hash-verified

39 * that no JNI classes are directly referenced in these tests.
40 */
41public class JNALoadTest extends TestCase implements Paths {
42
43 private class TestLoader extends URLClassLoader {
44 public TestLoader(boolean fromJar) throws MalformedURLException {
45 super(new URL[]{
46 Platform.isWindowsCE()
47 ? new File("/Storage Card/" + (fromJar ? "jna.jar" : "test.jar")).toURI().toURL()
48 : new File(BUILDDIR + (fromJar ? "/jna.jar" : "/classes")).toURI().toURL()},
49 new CloverLoader());
50 if (fromJar) {
51 assertJarExists();
52 }
53 else {
54 assertLibraryExists();
55 }
56 }
57
58 @Override
59 protected Class<?> findClass(String name) throws ClassNotFoundException {
60 String boot = System.getProperty("jna.boot.library.path");
61 if (boot != null) {
62 System.setProperty("jna.boot.library.path", "");
63 }
64 Class<?> cls = super.findClass(name);
65 if (boot != null) {
66 System.setProperty("jna.boot.library.path", boot);
67 }
68 return cls;
69 }
70 }
71
72 protected void assertJarExists() {
73 File jar = new File(JNAJAR);
74 if (!jar.exists()) {
75 throw new Error("Expected JNA jar file at " + jar + " is missing");
76 }
77 }
78
79 protected void assertLibraryExists() {
80 String osPrefix = Platform.getNativeLibraryResourcePrefix();
81 String name = System.mapLibraryName("jnidispatch").replace(".dylib", ".jnilib");
82 if(Platform.isAIX()) {
83 name = name.replaceAll(".so$", ".a");
84 }
85 File lib = new File(CLASSES + "/com/sun/jna/" + osPrefix + "/" + name);
86 if (!lib.exists()) {
87 throw new Error("Expected JNA library at " + lib + " is missing");
88 }
89 }
90
91 public void testAvoidJarUnpacking() throws Exception {
92 System.setProperty("jna.nounpack", "true");
93 try {
94 Class<?> cls = Class.forName("com.sun.jna.Native", true, new TestLoader(true));
95
96 fail("Class com.sun.jna.Native should not be loadable if jna.nounpack=true: "
97 + cls.getClassLoader());
98 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…