Skip to content

Commit 9074e35

Browse files
committed
Add unit tests
Simplify stub logic
1 parent a4076f6 commit 9074e35

5 files changed

Lines changed: 76 additions & 7 deletions

File tree

ant/build.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<entry key="maven.nativelibdir.path" value="${maven.nativelibdir.path}"/>
99
<entry key="maven.assembly.id" value="${maven.assembly.id}"/>
1010
<entry key="maven.test.skip" value="${maven.test.skip}"/>
11+
<entry key="maven.exclude.tests" value="${maven.exclude.tests}"/>
1112
</propertyfile>
1213
</target>
1314

@@ -193,6 +194,11 @@
193194
</and>
194195
</condition>
195196

197+
<!-- Skip ManualBootLibraryPathTest test if we're not building a native lib -->
198+
<condition property="maven.exclude.tests" value="**/ManualBootLibraryPathTest.java" else="">
199+
<equals arg1="${cmake.build.skip}" arg2="true"/>
200+
</condition>
201+
196202
<!-- Summarize host/target -->
197203
<echo level="info">Tests will run only if the TARGET and HOST match:${line.separator}${line.separator}</echo>
198204
<echo level="info">TARGET: ${os.target.classifier}</echo>

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<plugin.osmaven.version>1.7.0</plugin.osmaven.version>
6969
<plugin.signature.version>1.1</plugin.signature.version>
7070
<plugin.source.version>3.0.1</plugin.source.version>
71-
<plugin.surfire.version>3.0.0-M3</plugin.surfire.version>
71+
<plugin.surfire.version>3.0.0-M4</plugin.surfire.version>
7272
</properties>
7373

7474
<dependencies>
@@ -253,6 +253,12 @@
253253
<groupId>org.apache.maven.plugins</groupId>
254254
<artifactId>maven-surefire-plugin</artifactId>
255255
<version>${plugin.surfire.version}</version>
256+
<configuration>
257+
<reuseForks>false</reuseForks>
258+
<excludes>
259+
<exclude>${maven.exclude.tests}</exclude>
260+
</excludes>
261+
</configuration>
256262
</plugin>
257263

258264
<plugin>

src/main/java/jssc/DefaultJniExtractorStub.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
*/
1818
public class DefaultJniExtractorStub extends DefaultJniExtractor {
1919
private File bootPath;
20-
private boolean useStub;
2120

2221
/**
2322
* Default constructor
2423
*/
2524
public DefaultJniExtractorStub(Class libraryJarClass) throws IOException {
2625
super(libraryJarClass);
27-
useStub = false;
2826
}
2927

3028
/**
@@ -37,13 +35,12 @@ public DefaultJniExtractorStub(Class libraryJarClass) throws IOException {
3735
*/
3836
public DefaultJniExtractorStub(Class libraryJarClass, String bootPath) throws IOException {
3937
this(libraryJarClass);
40-
this.bootPath = new File(bootPath);
4138

4239
if(bootPath != null) {
4340
File bootTest = new File(bootPath);
4441
if(bootTest.exists()) {
4542
// assume a static, existing directory will contain the native libs
46-
this.useStub = true;
43+
this.bootPath = bootTest;
4744
} else {
4845
System.err.println("WARNING " + DefaultJniExtractorStub.class.getCanonicalName() + ": Boot path " + bootPath + " not found, falling back to default extraction behavior.");
4946
}
@@ -60,7 +57,7 @@ public DefaultJniExtractorStub(Class libraryJarClass, String bootPath) throws IO
6057
@Override
6158
public File extractJni(String libPath, String libName) throws IOException {
6259
// Lie and pretend it's already extracted at the bootPath location
63-
if(useStub) {
60+
if(bootPath != null) {
6461
return new File(bootPath, NativeLibraryUtil.getPlatformLibraryName(libName));
6562
}
6663
// Fallback on default behavior
@@ -69,7 +66,7 @@ public File extractJni(String libPath, String libName) throws IOException {
6966

7067
@Override
7168
public void extractRegistered() throws IOException {
72-
if(useStub) {
69+
if(bootPath != null) {
7370
return; // no-op
7471
}
7572
super.extractRegistered();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package jssc.bootpath;
2+
3+
import jssc.SerialNativeInterface;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.fail;
8+
9+
public class ManualBootLibraryPathFailedTest {
10+
@Test
11+
public void testBootPathOverride() {
12+
/**
13+
* This must be in its own class to run in a separate JVM
14+
* - See also: https://stackoverflow.com/questions/68657855
15+
* - NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static
16+
* - maven-surefire-plugin must be configured with reuseForks=false to use a new JVM for each class
17+
* - TODO: If JUnit adds JVM unloading between methods, this class can be consolidated
18+
*/
19+
String nativeLibDir = "/"; // This should be valid on all platforms
20+
System.setProperty("jssc.boot.library.path", nativeLibDir);
21+
try {
22+
SerialNativeInterface.getNativeLibraryVersion();
23+
fail("Library loading should fail if path provided exists but does not contain a native library");
24+
} catch (UnsatisfiedLinkError ignore) {
25+
assertTrue("Library loading failed as expected with an invalid jssc.boot.library.path", true);
26+
}
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package jssc.bootpath;
2+
3+
import jssc.SerialNativeInterface;
4+
import org.junit.Test;
5+
import org.scijava.nativelib.NativeLibraryUtil;
6+
7+
import static org.hamcrest.CoreMatchers.*;
8+
import static org.junit.Assert.assertThat;
9+
import static org.junit.Assert.fail;
10+
11+
public class ManualBootLibraryPathTest {
12+
@Test
13+
public void testBootPathOverride() {
14+
/**
15+
* This must be in its own class to run in a separate JVM
16+
* - See also: https://stackoverflow.com/questions/68657855
17+
* - NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static
18+
* - maven-surefire-plugin must be configured with reuseForks=false to use a new JVM for each class
19+
* - TODO: If JUnit adds JVM unloading between methods, this class can be consolidated
20+
*/
21+
String nativeLibDir = NativeLibraryUtil.getPlatformLibraryPath(System.getProperty("user.dir") + "/target/cmake/natives/");
22+
System.setProperty("jssc.boot.library.path", nativeLibDir);
23+
try {
24+
final String nativeLibraryVersion = SerialNativeInterface.getNativeLibraryVersion();
25+
assertThat(nativeLibraryVersion, is(not(nullValue())));
26+
assertThat(nativeLibraryVersion, is(not("")));
27+
} catch (UnsatisfiedLinkError linkError) {
28+
linkError.printStackTrace();
29+
fail("Should be able to call method!");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)