Skip to content

Commit

Permalink
Fix tests per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Nov 9, 2023
1 parent 1b619ca commit cd632f9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
15 changes: 7 additions & 8 deletions src/test/java/jssc/SerialNativeInterfaceTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jssc;

import jssc.bootpath.Helper;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -11,6 +11,10 @@
import static org.junit.Assert.fail;

public class SerialNativeInterfaceTest {
// Ensure library is loaded from /target/cmake/natives
static {
Helper.fixNativesPath();
}


@Test
Expand Down Expand Up @@ -41,15 +45,10 @@ public void testPrintVersion() {

}

@Before
public void runNext() {
// Skip in CI
Assume.assumeTrue(System.getenv("CI") == null);
// Skip on Windows
Assume.assumeFalse(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS);
}
@Test(expected = java.io.IOException.class)
public void reportsWriteErrorsAsIOException() throws Exception {
Assume.assumeFalse(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS);

long fd = -1; /*bad file by intent*/
byte[] buf = new byte[]{ 0x6A, 0x73, 0x73, 0x63, 0x0A };
SerialNativeInterface testTarget = new SerialNativeInterface();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/jssc/VirtualPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import jssc.bootpath.Helper;
import jssc.junit.rules.VirtualPortRule;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;

public class VirtualPortTest {
static {
Helper.fixNativesPath();
}

private static final String HELLO_WORLD = "Hello, world!";

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/jssc/bootpath/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jssc.bootpath;

import org.scijava.nativelib.NativeLibraryUtil;

/**
* TODO: Invocations MUST be in their own class to run in a separate JVM (https://stackoverflow.com/questions/68657855)
* - JUnit does NOT currently offer JVM unloading between methods.
* - maven-surefire-plugin DOES offer JVM unloading between classes using <code>reuseForks=false</code>
* - Unloading is needed due to NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static
* */
public class Helper {
public static void fixNativesPath() {
String nativeLibDir = NativeLibraryUtil.getPlatformLibraryPath(System.getProperty("user.dir") + "/target/cmake/natives/");
System.setProperty("jssc.boot.library.path", nativeLibDir);
}
public static void breakNativesPath() {
String nativeLibDir = "/";
System.setProperty("jssc.boot.library.path", nativeLibDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@
* Tests if a valid <code>jssc.boot.library.path</code> which does NOT contain a native library
* will predictably fail. This test can be run regardless of whether or not a native binary was
* created during the build process.
*
* TODO: This MUST be in its own class to run in a separate JVM (https://stackoverflow.com/questions/68657855)
* - JUnit does NOT currently offer JVM unloading between methods.
* - maven-surefire-plugin DOES offer JVM unloading between classes using <code>reuseForks=false</code>
* - Unloading is needed due to NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static
*/
public class ManualBootLibraryPathFailedTest {
@Test
public void testBootPathOverride() {
String nativeLibDir = "/"; // This should be valid on all platforms
System.setProperty("jssc.boot.library.path", nativeLibDir);
// Ensure library is not found
Helper.breakNativesPath();
try {
SerialNativeInterface.getNativeLibraryVersion();
fail("Library loading should fail if path provided exists but does not contain a native library");
Expand Down
10 changes: 2 additions & 8 deletions src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jssc.SerialNativeInterface;
import org.junit.Test;
import org.scijava.nativelib.NativeLibraryUtil;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
Expand All @@ -12,17 +11,12 @@
* Tests if a valid <code>jssc.boot.library.path</code> which DOES contain a native library
* will predictably pass. This test can ONLY be run regardless if a native binary was created
* during the build process. See also <code>maven.exclude.tests</code>.
*
* TODO: This MUST be in its own class to run in a separate JVM (https://stackoverflow.com/questions/68657855)
* - JUnit does NOT currently offer JVM unloading between methods.
* - maven-surefire-plugin DOES offer JVM unloading between classes using <code>reuseForks=false</code>
* - Unloading is needed due to NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static
*/
public class ManualBootLibraryPathTest {
@Test
public void testBootPathOverride() {
String nativeLibDir = NativeLibraryUtil.getPlatformLibraryPath(System.getProperty("user.dir") + "/target/cmake/natives/");
System.setProperty("jssc.boot.library.path", nativeLibDir);
// Ensure library is loaded from /target/cmake/natives
Helper.fixNativesPath();
try {
final String nativeLibraryVersion = SerialNativeInterface.getNativeLibraryVersion();
assertThat(nativeLibraryVersion, is(not(nullValue())));
Expand Down

0 comments on commit cd632f9

Please sign in to comment.