MyPrivilegedExceptionAction action1 = new MyPrivilegedExceptionAction();
try {
String result = AccessController.doPrivileged(action1);
assertEquals("unexpected result", "ok", result);
assertTrue("method not called", action1.called);
} catch (PrivilegedActionException e) {
fail("unexpected exception : " + e);
}
Exception[] exceptions = {new NullPointerException(), new IOException(), null};
for (int i = 0; i < exceptions.length; i++) {
Exception exception = exceptions[i];
MyPrivilegedExceptionAction2 action2 = new MyPrivilegedExceptionAction2(exception);
try {
String result = AccessController.doPrivileged(action2);
assertTrue("method not called", action1.called);
if (exception == null)
{
assertEquals("unexpected result", "ok", result);
}
else {
fail("privileged action exception expected");
}
} catch (PrivilegedActionException e) {
assertTrue("method not called", action2.called);
assertSame("expected exception not thrown", exception, e.getCause());
// ok
} catch (RuntimeException e) {
assertSame("expected exception not thrown", exception, e);
}
}