Good Rule Gary, I wonder: do we have a convention for fields? Order might not be so important if you use IDEs, but when reviewing patches or pull requests I still prefer to have my fields at the beginning of a class.
Gruss Bernd Gruss Bernd -- http://bernd.eckenfels.net ________________________________ From: ggreg...@apache.org <ggreg...@apache.org> Sent: Thursday, March 1, 2018 11:09:37 PM To: comm...@commons.apache.org Subject: commons-testing git commit: Add SecurityManagerTestRule from Apache Log4j. Repository: commons-testing Updated Branches: refs/heads/master 10ea280b9 -> 1696bcfef Add SecurityManagerTestRule from Apache Log4j. Project: http://git-wip-us.apache.org/repos/asf/commons-testing/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-testing/commit/1696bcfe Tree: http://git-wip-us.apache.org/repos/asf/commons-testing/tree/1696bcfe Diff: http://git-wip-us.apache.org/repos/asf/commons-testing/diff/1696bcfe Branch: refs/heads/master Commit: 1696bcfef7c9d32cd3a86205c522d0a78c6e2705 Parents: 10ea280 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Thu Mar 1 15:09:35 2018 -0700 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Thu Mar 1 15:09:35 2018 -0700 ---------------------------------------------------------------------- .../testing/junit4/SecurityManagerTestRule.java | 92 ++++++++++++++++++++ 1 file changed, 92 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-testing/blob/1696bcfe/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SecurityManagerTestRule.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SecurityManagerTestRule.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SecurityManagerTestRule.java new file mode 100644 index 0000000..9a600ef --- /dev/null +++ b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SecurityManagerTestRule.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.junit; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Sets a security manager for a test run. The current security manager is first saved then restored after the test is + * run. + * <p> + * Using a security manager can mess up other tests so this is best used from integration tests (classes that end in + * "IT" instead of "Test" and "TestCase".) + * </p> + * + * <p> + * When this test rule is evaluated, it will: + * </p> + * <ol> + * <li>Save the current SecurityManager.</li> + * <li>Set the SecurityManager to the instance supplied to this rule.</li> + * <li>Evaluate the test statement.</li> + * <li>Reset the current SecurityManager to the one from step (1).</li> + * </ol> + */ +public class SecurityManagerTestRule implements TestRule { + + /** + * Constructs a new instance with the given {@link SecurityManager}. + * <p> + * When this test rule is evaluated, it will: + * </p> + * <ol> + * <li>Save the current SecurityManager.</li> + * <li>Set the SecurityManager to the instance supplied to this rule.</li> + * <li>Evaluate the test statement.</li> + * <li>Reset the current SecurityManager to the one from step (1).</li> + * </ol> + * + * @param securityManager + * the {@link SecurityManager} to use while running a test. + */ + public SecurityManagerTestRule(final SecurityManager securityManager) { + super(); + this.securityManager = securityManager; + } + + private SecurityManager securityManagerBefore; + private final SecurityManager securityManager; + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + before(); + try { + base.evaluate(); + } finally { + after(); + } + } + + private void after() { + System.setSecurityManager(securityManagerBefore); + } + + private void before() { + securityManagerBefore = System.getSecurityManager(); + System.setSecurityManager(securityManager); + + } + }; + } + +}