org.junit.contrib.java.lang.system.ProvideSecurityManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of system-rules Show documentation
Show all versions of system-rules Show documentation
A collection of JUnit rules for testing code which uses java.lang.System.
package org.junit.contrib.java.lang.system;
import static java.lang.System.getSecurityManager;
import static java.lang.System.setSecurityManager;
import org.junit.rules.ExternalResource;
/**
* The {@code ProvideSecurityManager} rule provides an arbitrary security
* manager to a test. After the test the original security manager is restored.
*
*
* public void MyTest {
* private final MySecurityManager securityManager
* = new MySecurityManager();
*
* @Rule
* public final ProvideSecurityManager provideSecurityManager
* = new ProvideSecurityManager(securityManager);
*
* @Test
* public void overrideProperty() {
* assertEquals(securityManager, System.getSecurityManager());
* }
* }
*
*/
public class ProvideSecurityManager extends ExternalResource {
private final SecurityManager manager;
private SecurityManager originalManager;
public ProvideSecurityManager(SecurityManager manager) {
this.manager = manager;
}
@Override
protected void before() throws Throwable {
originalManager = getSecurityManager();
setSecurityManager(manager);
}
@Override
protected void after() {
setSecurityManager(originalManager);
}
}