Skip to content

Commit

Permalink
refactor: SecurityManagerTestRule to junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
Realtin committed Nov 19, 2024
1 parent c77ac8d commit b15b1e9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.test.junit;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class SecurityManagerExtension implements BeforeEachCallback, AfterEachCallback {
private SecurityManager securityManagerBefore;
private final SecurityManager securityManager;

public SecurityManagerExtension(final SecurityManager securityManager) {
this.securityManager = securityManager;
}

public void beforeEach(ExtensionContext ctx) {
securityManagerBefore = System.getSecurityManager();
System.setSecurityManager(securityManager);
}

public void afterEach(ExtensionContext ctx) {
System.setSecurityManager(securityManagerBefore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.logging.log4j.message;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.io.Serializable;
import org.apache.logging.log4j.test.AbstractSerializationTest;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.apache.logging.log4j.util;

import java.security.Permission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -37,8 +37,8 @@
@ResourceLock("java.lang.SecurityManager")
public class EnvironmentPropertySourceSecurityManagerIT {

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any environment variables permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.security.Permission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

@ResourceLock("java.lang.SecurityManager")
public class LoaderUtilSecurityManagerTest {
@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

private static class TestSecurityManager extends SecurityManager {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/
package org.apache.logging.log4j.util;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.FilePermission;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Permission;
import java.util.PropertyPermission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -46,13 +46,13 @@
@ResourceLock("java.lang.SecurityManager")
public class PropertyFilePropertySourceSecurityManagerIT {

@BeforeClass
public static void beforeClass() {
assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));
@BeforeAll
public static void beforeAll() {
assertTrue(Files.exists(Paths.get(TEST_FIXTURE_PATH)), TEST_FIXTURE_PATH);
}

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

private static final String TEST_FIXTURE_PATH = "src/test/resources/PropertiesUtilTest.properties";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import java.security.Permission;
import java.util.Deque;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -38,8 +38,8 @@
*/
@ResourceLock("java.lang.SecurityManager")
public class StackLocatorTestIT {
@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any reques to create a new SecurityManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.apache.logging.log4j.util;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.security.Permission;
import java.util.PropertyPermission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -41,8 +41,8 @@
@ResourceLock("java.lang.SecurityManager")
public class SystemPropertiesPropertySourceSecurityManagerIT {

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any environment variables permission
Expand Down

0 comments on commit b15b1e9

Please sign in to comment.