use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.
the class CallbacksTest method testCallbackExceptionHandler.
// Most Callbacks are wrapped in DefaultCallbackProxy, which catches their
// exceptions.
public void testCallbackExceptionHandler() {
final RuntimeException ERROR = new RuntimeException(getName());
final Throwable[] CAUGHT = { null };
final Callback[] CALLBACK = { null };
UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Callback cb, Throwable e) {
CALLBACK[0] = cb;
CAUGHT[0] = e;
}
};
Native.setCallbackExceptionHandler(handler);
try {
TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
@Override
public CbCallback callback(CbCallback arg) {
throw ERROR;
}
};
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
assertNotNull("Exception handler not called", CALLBACK[0]);
assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
assertEquals("Wrong exception passed to handler", ERROR, CAUGHT[0]);
} finally {
Native.setCallbackExceptionHandler(old);
}
}
use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.
the class CallbacksTest method testCallCallbackWithCallbackArgumentAndResult.
public void testCallCallbackWithCallbackArgumentAndResult() {
TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
@Override
public CbCallback callback(CbCallback arg) {
return arg;
}
};
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
assertEquals("Callback reference should be reused", cb, cb2);
}
use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.
the class CallbacksTest method testCallbackExceptionHandlerWithCallbackProxy.
// CallbackProxy is called directly from native.
public void testCallbackExceptionHandlerWithCallbackProxy() throws Throwable {
final RuntimeException ERROR = new RuntimeException(getName());
final Throwable[] CAUGHT = { null };
final Callback[] CALLBACK = { null };
UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Callback cb, Throwable e) {
CALLBACK[0] = cb;
CAUGHT[0] = e;
}
};
Native.setCallbackExceptionHandler(handler);
try {
class TestProxy implements CallbackProxy, TestLibrary.CbCallback {
@Override
public CbCallback callback(CbCallback arg) {
throw new Error("Should never be called");
}
@Override
public Object callback(Object[] args) {
throw ERROR;
}
@Override
public Class<?>[] getParameterTypes() {
return new Class[] { CbCallback.class };
}
@Override
public Class<?> getReturnType() {
return CbCallback.class;
}
}
;
TestLibrary.CbCallback cb = new TestProxy();
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
assertNotNull("Exception handler not called", CALLBACK[0]);
assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
assertEquals("Wrong exception passed to handler", ERROR, CAUGHT[0]);
} finally {
Native.setCallbackExceptionHandler(old);
}
}
use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.
the class CallbacksTest method testDefaultCallbackExceptionHandler.
public void testDefaultCallbackExceptionHandler() {
final RuntimeException ERROR = new RuntimeException(getName());
PrintStream ps = System.err;
ByteArrayOutputStream s = new ByteArrayOutputStream();
System.setErr(new PrintStream(s));
try {
TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
@Override
public CbCallback callback(CbCallback arg) {
throw ERROR;
}
};
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
String output = s.toString();
assertTrue("Default handler not called", output.length() > 0);
} finally {
System.setErr(ps);
}
}
Aggregations