@@ -0,0 +1,18 @@
|
||||
package com.baeldung.jna;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Platform;
|
||||
|
||||
class CMathUnitTest {
|
||||
@Test
|
||||
void whenCallNative_thenSuccess() {
|
||||
CMath lib = Native.load(Platform.isWindows() ? "msvcrt" : "c", CMath.class);
|
||||
double result = lib.cosh(0);
|
||||
assertEquals(1.0,result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.jna;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.jna.NativeFS.Stat;
|
||||
import com.sun.jna.LastErrorException;
|
||||
import com.sun.jna.Platform;
|
||||
|
||||
public class NativeFSUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCallNative_thenSuccess() throws IOException {
|
||||
NativeFS lib = NativeFS.INSTANCE;
|
||||
|
||||
File f = Files.createTempFile("junit", ".bin").toFile();
|
||||
f.deleteOnExit();
|
||||
Stat stat = new Stat();
|
||||
try {
|
||||
if (Platform.isWindows()) {
|
||||
int rc = lib.stat(f.getAbsolutePath(), stat);
|
||||
assertEquals(0, rc);
|
||||
assertEquals(0,stat.st_size.longValue());
|
||||
}
|
||||
}
|
||||
catch(LastErrorException error) {
|
||||
fail("stat failed: error code=" + error.getErrorCode());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
47
java-native/src/test/java/com/baeldung/jna/StdCUnitTest.java
Normal file
47
java-native/src/test/java/com/baeldung/jna/StdCUnitTest.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.jna;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Platform;
|
||||
import com.sun.jna.Pointer;
|
||||
|
||||
class StdCUnitTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setupProtectedMode() {
|
||||
Native.setProtected(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMalloc_thenSuccess() {
|
||||
StdC lib = StdC.INSTANCE;
|
||||
Pointer p = lib.malloc(1024);
|
||||
p.setMemory(0l, 1024l, (byte) 0);
|
||||
lib.free(p);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAccessViolation_thenShouldThrowError() {
|
||||
// Running this test on Linux requires additional setup using libjsig.so
|
||||
// Details here: http://java-native-access.github.io/jna/5.6.0/javadoc/overview-summary.html#crash-protection
|
||||
// IMPORTANT NOTICE: Code for illustration purposes only. DON'T DO THIS IN YOUR OWN CODE
|
||||
if ( Platform.isWindows()) {
|
||||
Error e = null;
|
||||
Pointer p = new Pointer(0l);
|
||||
|
||||
try {
|
||||
p.setMemory(0, 100*1024, (byte) 0);
|
||||
}
|
||||
catch(Error err) {
|
||||
e = err;
|
||||
}
|
||||
|
||||
assertNotNull(e, "Should throw Error");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jni;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JNINativeManualTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
System.loadLibrary("native");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNativeHelloWorld_thenOutputIsAsExpected() {
|
||||
HelloWorldJNI helloWorld = new HelloWorldJNI();
|
||||
String helloFromNative = helloWorld.sayHello();
|
||||
assertTrue(!helloFromNative.isEmpty() && helloFromNative.equals("Hello from C++ !!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSumNative_thenResultIsArithmeticallyCorrect() {
|
||||
ExampleParametersJNI parametersNativeMethods = new ExampleParametersJNI();
|
||||
assertTrue(parametersNativeMethods.sumIntegers(200, 400) == 600L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSayingNativeHelloToMe_thenResultIsAsExpected() {
|
||||
ExampleParametersJNI parametersNativeMethods = new ExampleParametersJNI();
|
||||
assertEquals(parametersNativeMethods.sayHelloToMe("Orange", true), "Ms. Orange");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingNativeObject_thenObjectIsNotNullAndHasCorrectData() {
|
||||
String name = "Iker Casillas";
|
||||
double balance = 2378.78;
|
||||
ExampleObjectsJNI objectsNativeMethods = new ExampleObjectsJNI();
|
||||
UserData userFromNative = objectsNativeMethods.createUser(name, balance);
|
||||
assertNotNull(userFromNative);
|
||||
assertEquals(userFromNative.name, name);
|
||||
assertTrue(userFromNative.balance == balance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNativeCallingObjectMethod_thenResultIsAsExpected() {
|
||||
String name = "Sergio Ramos";
|
||||
double balance = 666.77;
|
||||
ExampleObjectsJNI objectsNativeMethods = new ExampleObjectsJNI();
|
||||
UserData userData = new UserData();
|
||||
userData.name = name;
|
||||
userData.balance = balance;
|
||||
assertEquals(objectsNativeMethods.printUserData(userData), "[name]=" + name + ", [balance]=" + balance);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user