Skip to content

Commit

Permalink
add tests for library loading from resource path
Browse files Browse the repository at this point in the history
  • Loading branch information
twall committed Apr 18, 2013
1 parent 605dfcb commit 9740335
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Bug Fixes
* Use TLS to indicate callback detach state, to avoid any potential conflicts with last error storage - [@twall](https://github.com/twall).
* [#173](https://github.com/twall/jna/issues/173): Fix OSX 10.8/Xcode 4+ builds, web start path with Oracle 1.7 JDK - [@mkjellman](https://github.com/mkjellman).
* [#215](https://github.com/twall/jna/issues/215): Force use of XSI `strerror_r` on linux - [LionelCons](https://github.com/LionelCons).
* [#214](https://github.com/twall/jna/issues/214): Don't map library names when an absolute path is provided - [@twall](https://github.com/twall).

Release 3.5.2
=============
Expand Down
20 changes: 10 additions & 10 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -725,26 +725,26 @@ osname=macosx;processor=x86;processor=x86-64;processor=ppc
<src path="${test.src}"/>
<exclude name="${tests.exclude}"/>
</javac>
<!-- Move (not copy) embedded testlib to test class folder so that it will be -->
<!-- packaged into the test jar and NOT available in the FS-based class path -->
<move todir="${test.classes}/${os.prefix}">
<!-- Set up versions of test library for resource path loading -->
<copy todir="${test.classes}/${os.prefix}">
<fileset dir="${build.native}">
<patternset id="embedded-testlib">
<include name="**/*embedded-testlib*"/>
<patternset>
<include name="**/*testlib-jar*"/>
<include name="**/*testlib-path*"/>
</patternset>
</fileset>
</move>
<!-- Create a jar for easy movement of tests, and embedded load test -->
</copy>
<!-- Create a jar for easy movement of tests, and jar load test -->
<jar jarfile="${build}/${testjar}">
<fileset dir="${test.classes}">
<patternset refid="jar-compiled"/>
<include name="**/*embedded-testlib*"/>
<include name="**/*testlib-jar*"/>
</fileset>
</jar>
<!-- Ensure embedded library unavailable on FS-based class path -->
<!-- Ensure jar-based library unavailable on FS-based class path -->
<delete>
<fileset dir="${test.classes}">
<include name="**/*embedded-testlib*"/>
<include name="**/*testlib-jar*"/>
</fileset>
</delete>
<mkdir dir="${build}/jws"/>
Expand Down
10 changes: 6 additions & 4 deletions native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ FFI_CONFIG=--enable-static --disable-shared --with-pic=yes
endif
LIBRARY=$(BUILD)/$(LIBPFX)jnidispatch$(JNISFX)
TESTLIB=$(BUILD)/$(LIBPFX)testlib$(LIBSFX)
TESTLIB_EMBEDDED=$(BUILD)/$(LIBPFX)embedded-testlib$(LIBSFX)
TESTLIB_JAR=$(BUILD)/$(LIBPFX)testlib-jar$(LIBSFX)
TESTLIB_PATH=$(BUILD)/$(LIBPFX)testlib-path$(LIBSFX)
TESTLIB_TRUNC=$(BUILD)/testlib-truncated
TESTLIB2=$(BUILD)/$(LIBPFX)testlib2$(LIBSFX)

# Reasonable defaults based on GCC
Expand Down Expand Up @@ -342,7 +344,7 @@ else
$(CC) $(CFLAGS) -c $< $(COUT)
endif

all: $(LIBRARY) $(TESTLIB) $(TESTLIB2) $(TESTLIB_EMBEDDED)
all: $(LIBRARY) $(TESTLIB) $(TESTLIB2) $(TESTLIB_JAR) $(TESTLIB_PATH) $(TESTLIB_TRUNC)

install:
mkdir $(INSTALLDIR)
Expand All @@ -365,8 +367,8 @@ $(LIBRARY): $(JNIDISPATCH_OBJS) $(FFI_LIB)
$(TESTLIB): $(BUILD)/testlib.o
$(LD) $(LDFLAGS) $< $(LIBS)

$(TESTLIB_EMBEDDED): $(BUILD)/testlib.o
$(LD) $(LDFLAGS) $< $(LIBS)
$(TESTLIB_JAR) $(TESTLIB_PATH) $(TESTLIB_TRUNC): $(TESTLIB)
@cp $< $@

ifeq ($(ARSFX),.lib)
TESTDEP=$(TESTLIB:.dll=.lib)
Expand Down
13 changes: 8 additions & 5 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private static void loadNativeDispatchLibrary() {
*/
private static void loadNativeDispatchLibraryFromClasspath() {
try {
String prefix = "com/sun/jna/" + getNativeLibraryResourcePrefix();
String prefix = "/com/sun/jna/" + getNativeLibraryResourcePrefix();
File lib = extractFromResourcePath("jnidispatch", prefix, Native.class.getClassLoader());
if (lib == null) {
throw new UnsatisfiedLinkError("Could not find JNA native support");
Expand Down Expand Up @@ -733,7 +733,7 @@ static boolean isUnpacked(File file) {
* @throws IOException if resource not found
*/
static File extractFromResourcePath(String name) throws IOException {
return extractFromResourcePath(name, getNativeLibraryResourcePrefix(), Thread.currentThread().getContextClassLoader());
return extractFromResourcePath(name, "/" + getNativeLibraryResourcePrefix(), Thread.currentThread().getContextClassLoader());
}

/** Attempt to extract a native library from the current resource path.
Expand All @@ -747,8 +747,11 @@ static File extractFromResourcePath(String name) throws IOException {
* @throws IOException if resource not found
*/
static File extractFromResourcePath(String name, String resourcePrefix, ClassLoader loader) throws IOException {
String libname = System.mapLibraryName(name);
String resourcePath = resourcePrefix + "/" + libname;
String libname = name.startsWith("/") ? name : System.mapLibraryName(name);
String resourcePath = name.startsWith("/") ? name : resourcePrefix + "/" + libname;
if (resourcePath.startsWith("/")) {
resourcePath = resourcePath.substring(1);
}
URL url = loader.getResource(resourcePath);

// User libraries will have '.dylib'
Expand All @@ -764,7 +767,7 @@ else if (resourcePath.endsWith(".dylib")) {
url = loader.getResource(resourcePath);
}
if (url == null) {
throw new IOException("JNA native support (" + resourcePath + ") not found in resource path (" + System.getProperty("java.class.path") + ")");
throw new IOException("Native library (" + resourcePath + ") not found in resource path (" + System.getProperty("java.class.path") + ")");
}

File lib = null;
Expand Down
29 changes: 29 additions & 0 deletions test/com/sun/jna/LibraryLoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ public void testLoadAWTAfterJNA() {
}
}

public interface TestLibrary extends Library {
}

public void testLoadFromJNALibraryPath() {
NativeLibrary.getInstance("testlib");
}

public void testLoadFromClasspath() {
NativeLibrary.getInstance("testlib-path");
}

public void testLoadFromClasspathAbsolute() {
String name = System.mapLibraryName("testlib-path").replace(".jnilib", ".dylib");
NativeLibrary.getInstance("/" + Platform.RESOURCE_PREFIX + "/" + name);
}

public void testLoadFromJar() {
NativeLibrary.getInstance("testlib-jar");
}

public void testLoadFromJarAbsolute() {
String name = System.mapLibraryName("testlib-jar").replace(".jnilib", ".dylib");
NativeLibrary.getInstance("/" + Platform.RESOURCE_PREFIX + "/" + name);
}

public void testLoadExplicitAbsolutePath() {
NativeLibrary.getInstance(new File(BUILDDIR + "/native/testlib-truncated").getAbsolutePath());
}

public static interface CLibrary extends Library {
int wcslen(WString wstr);
int strlen(String str);
Expand Down
4 changes: 0 additions & 4 deletions test/com/sun/jna/NativeLibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@ public void testLoadLibraryWithOptions() {
Native.loadLibrary("testlib", TestLibrary.class, options);
}

public void testEmbeddedLibrary() {
Native.loadLibrary("embedded-testlib", TestLibrary.class);
}

public static void main(String[] args) {
junit.textui.TestRunner.run(NativeLibraryTest.class);
}
Expand Down

0 comments on commit 9740335

Please sign in to comment.