SecurityManagerHelper { public static boolean isSecurityManagerEnabled() { return System.getSecurityManager() != null; }  static boolean isSecurityManagerEnabled(); static T doPrivileged(PrivilegedAction<T> privilegedAction); static T doPrivileged(PrivilegedAction<T> privilegedAction, Collection<Permission> permissions);  }
@Test public void testDoPrivilegedWhenNoPermissionsRestrictedActionThrows() { Class<? extends Exception> expectedExceptionType = null; if (SecurityManagerHelper.isSecurityManagerEnabled()) { expectedExceptionType = AccessControlException.class; } testDoPrivileged(expectedExceptionType, new PrivilegedAction<Void>() { @Override public Void run() { System.getProperty("test"); return null; } }); }
@Test public void testDoPrivilegedWhenNoMatchingPermission() { Class<? extends Exception> expectedException = null; if (SecurityManagerHelper.isSecurityManagerEnabled()) { expectedException = AccessControlException.class; } testDoPrivileged(expectedException, new PrivilegedAction<Void>() { @Override public Void run() { System.getProperty("test"); return null; } }, Arrays.<Permission> asList(new PropertyPermission("anotherValue", "read"))); }