Skip to content

Commit dbf4fcd

Browse files
committed
refactor: SecurityManagerTestRule to junit5
1 parent eade0ca commit dbf4fcd

7 files changed

+72
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.test.junit;
18+
19+
import org.junit.jupiter.api.extension.AfterEachCallback;
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
public class SecurityManagerExtension implements BeforeEachCallback, AfterEachCallback {
24+
private SecurityManager securityManagerBefore;
25+
private final SecurityManager securityManager;
26+
27+
public SecurityManagerExtension(final SecurityManager securityManager) {
28+
this.securityManager = securityManager;
29+
}
30+
31+
public void beforeEach(ExtensionContext ctx) {
32+
securityManagerBefore = System.getSecurityManager();
33+
System.setSecurityManager(securityManager);
34+
}
35+
36+
public void afterEach(ExtensionContext ctx) {
37+
System.setSecurityManager(securityManagerBefore);
38+
}
39+
}

log4j-api-test/src/test/java/org/apache/logging/log4j/message/MessageFormatMessageSerializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package org.apache.logging.log4j.message;
1818

19+
import java.io.Serializable;
1920
import java.util.Arrays;
2021
import java.util.Collection;
21-
import java.io.Serializable;
2222
import org.apache.logging.log4j.test.AbstractSerializationTest;
2323
import org.junit.jupiter.api.parallel.ResourceAccessMode;
2424
import org.junit.jupiter.api.parallel.ResourceLock;

log4j-api-test/src/test/java/org/apache/logging/log4j/util/EnvironmentPropertySourceSecurityManagerIT.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.apache.logging.log4j.util;
1818

1919
import java.security.Permission;
20-
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
21-
import org.junit.Rule;
22-
import org.junit.Test;
20+
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
2323
import org.junit.jupiter.api.parallel.ResourceLock;
2424

2525
/**
@@ -37,8 +37,8 @@
3737
@ResourceLock("java.lang.SecurityManager")
3838
public class EnvironmentPropertySourceSecurityManagerIT {
3939

40-
@Rule
41-
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
40+
@RegisterExtension
41+
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());
4242

4343
/**
4444
* Always throws a SecurityException for any environment variables permission

log4j-api-test/src/test/java/org/apache/logging/log4j/util/LoaderUtilSecurityManagerTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121

2222
import java.security.Permission;
23-
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
23+
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
2626
import org.junit.jupiter.api.parallel.ResourceLock;
2727

2828
@ResourceLock("java.lang.SecurityManager")
2929
public class LoaderUtilSecurityManagerTest {
30-
@Rule
31-
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
30+
@RegisterExtension
31+
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());
3232

3333
private static class TestSecurityManager extends SecurityManager {
3434
@Override

log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertyFilePropertySourceSecurityManagerIT.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
*/
1717
package org.apache.logging.log4j.util;
1818

19-
import static org.junit.Assert.assertNull;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.io.FilePermission;
2323
import java.nio.file.Files;
2424
import java.nio.file.Paths;
2525
import java.security.Permission;
2626
import java.util.PropertyPermission;
27-
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
28-
import org.junit.BeforeClass;
29-
import org.junit.Rule;
30-
import org.junit.Test;
27+
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
3131
import org.junit.jupiter.api.parallel.ResourceLock;
3232

3333
/**
@@ -46,13 +46,13 @@
4646
@ResourceLock("java.lang.SecurityManager")
4747
public class PropertyFilePropertySourceSecurityManagerIT {
4848

49-
@BeforeClass
50-
public static void beforeClass() {
51-
assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));
49+
@BeforeAll
50+
public static void beforeAll() {
51+
assertTrue(Files.exists(Paths.get(TEST_FIXTURE_PATH)), TEST_FIXTURE_PATH);
5252
}
5353

54-
@Rule
55-
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
54+
@RegisterExtension
55+
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());
5656

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

log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorTestIT.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import java.security.Permission;
2222
import java.util.Deque;
23-
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
23+
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
2626
import org.junit.jupiter.api.parallel.ResourceLock;
2727

2828
/**
@@ -39,8 +39,8 @@
3939
*/
4040
@ResourceLock("java.lang.SecurityManager")
4141
public class StackLocatorTestIT {
42-
@Rule
43-
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
42+
@RegisterExtension
43+
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());
4444

4545
/**
4646
* Always throws a SecurityException for any reques to create a new SecurityManager

log4j-api-test/src/test/java/org/apache/logging/log4j/util/SystemPropertiesPropertySourceSecurityManagerIT.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
*/
1717
package org.apache.logging.log4j.util;
1818

19-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
2020

2121
import java.security.Permission;
2222
import java.util.PropertyPermission;
23-
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
23+
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
2626
import org.junit.jupiter.api.parallel.ResourceLock;
2727

2828
/**
@@ -41,8 +41,8 @@
4141
@ResourceLock("java.lang.SecurityManager")
4242
public class SystemPropertiesPropertySourceSecurityManagerIT {
4343

44-
@Rule
45-
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
44+
@RegisterExtension
45+
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());
4646

4747
/**
4848
* Always throws a SecurityException for any environment variables permission

0 commit comments

Comments
 (0)