All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.contrib.java.lang.system.ProvideSecurityManager Maven / Gradle / Ivy

There is a newer version: 1.19.0
Show newest version
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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy