This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 04f19a7725a831d5fa33e4d4fc06f181a7014c42
Author: Matt Sicker <[email protected]>
AuthorDate: Sun May 1 17:53:53 2022 -0500

    Disable SecurityManager-using tests on Java 18+
    
    SecurityManager is a deprecated API that is now throwing an 
UnsupportedOperationException on Java 18 on attempt to use a custom 
SecurityManager instance.
    
    Signed-off-by: Matt Sicker <[email protected]>
---
 ...EnvironmentPropertySourceSecurityManagerIT.java | 24 +++++++++------
 ...ropertyFilePropertySourceSecurityManagerIT.java | 36 ++++++++++++----------
 ...mPropertiesPropertySourceSecurityManagerIT.java | 28 +++++++++--------
 .../log4j/core/impl/ThrowableProxyTest.java        | 35 ++++++++++-----------
 4 files changed, 65 insertions(+), 58 deletions(-)

diff --git 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/EnvironmentPropertySourceSecurityManagerIT.java
 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/EnvironmentPropertySourceSecurityManagerIT.java
index 58ed8a15eb..eebb0a73aa 100644
--- 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/EnvironmentPropertySourceSecurityManagerIT.java
+++ 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/EnvironmentPropertySourceSecurityManagerIT.java
@@ -17,15 +17,15 @@
 
 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.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
 import org.junit.jupiter.api.parallel.ResourceLock;
 
+import java.security.Permission;
+
 /**
- * Tests https://issues.apache.org/jira/browse/LOG4J2-2274.
+ * Tests <a 
href="https://issues.apache.org/jira/browse/LOG4J2-2274";>LOG4J2-2274</a>.
  * <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
@@ -37,11 +37,9 @@ import org.junit.jupiter.api.parallel.ResourceLock;
  * @see System#setSecurityManager(SecurityManager)
  */
 @ResourceLock("java.lang.SecurityManager")
+@DisabledForJreRange(min = JRE.JAVA_18) // custom SecurityManager instances 
throw UnsupportedOperationException
 public class EnvironmentPropertySourceSecurityManagerIT {
 
-       @Rule
-       public final SecurityManagerTestRule rule = new 
SecurityManagerTestRule(new TestSecurityManager());
-
        /**
         * Always throws a SecurityException for any environment variables 
permission
         * check.
@@ -75,6 +73,12 @@ public class EnvironmentPropertySourceSecurityManagerIT {
         */
        @Test
        public void test() {
-               PropertiesUtil.getProperties();
+               var existing = System.getSecurityManager();
+               try {
+                       System.setSecurityManager(new TestSecurityManager());
+                       PropertiesUtil.getProperties();
+               } finally {
+                       System.setSecurityManager(existing);
+               }
        }
 }
diff --git 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertyFilePropertySourceSecurityManagerIT.java
 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertyFilePropertySourceSecurityManagerIT.java
index c586c9d29e..40cdcb611f 100644
--- 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertyFilePropertySourceSecurityManagerIT.java
+++ 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertyFilePropertySourceSecurityManagerIT.java
@@ -17,23 +17,21 @@
 
 package org.apache.logging.log4j.util;
 
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
+import org.junit.jupiter.api.parallel.ResourceLock;
+
 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.junit.jupiter.api.parallel.ResourceLock;
-
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
 
 /**
- * Test related to https://issues.apache.org/jira/browse/LOG4J2-2274.
+ * Test related to <a 
href="https://issues.apache.org/jira/browse/LOG4J2-2274";>LOG4J2-2274</a>.
  * <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
@@ -46,15 +44,13 @@ import static org.junit.Assert.assertTrue;
  * @see PropertyPermission
  */
 @ResourceLock("java.lang.SecurityManager")
+@DisabledForJreRange(min = JRE.JAVA_18) // custom SecurityManager instances 
throw UnsupportedOperationException
 public class PropertyFilePropertySourceSecurityManagerIT {
 
-    @BeforeClass
+    @BeforeAll
     public static void beforeClass() {
-        assertTrue(TEST_FIXTURE_PATH, 
Files.exists(Paths.get(TEST_FIXTURE_PATH)));
+        assertThat(Paths.get(TEST_FIXTURE_PATH)).exists();
     }
-    
-    @Rule
-    public final SecurityManagerTestRule rule = new 
SecurityManagerTestRule(new TestSecurityManager());
 
     private static final String TEST_FIXTURE_PATH = 
"src/test/resources/PropertiesUtilTest.properties";
 
@@ -82,7 +78,13 @@ public class PropertyFilePropertySourceSecurityManagerIT {
      */
     @Test
     public void test() {
-        final PropertiesUtil propertiesUtil = new 
PropertiesUtil(TEST_FIXTURE_PATH);
-        assertNull(propertiesUtil.getStringProperty("a.1"));
+        var existing = System.getSecurityManager();
+        try {
+            System.setSecurityManager(new TestSecurityManager());
+            final PropertiesUtil propertiesUtil = new 
PropertiesUtil(TEST_FIXTURE_PATH);
+            assertThat(propertiesUtil.getStringProperty("a.1")).isNull();
+        } finally {
+            System.setSecurityManager(existing);
+        }
     }
 }
diff --git 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SystemPropertiesPropertySourceSecurityManagerIT.java
 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SystemPropertiesPropertySourceSecurityManagerIT.java
index fe7014a8f0..d4df643cf2 100644
--- 
a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SystemPropertiesPropertySourceSecurityManagerIT.java
+++ 
b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SystemPropertiesPropertySourceSecurityManagerIT.java
@@ -17,18 +17,18 @@
 
 package org.apache.logging.log4j.util;
 
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
+import org.junit.jupiter.api.parallel.ResourceLock;
+
 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.junit.jupiter.api.parallel.ResourceLock;
-
-import static org.junit.Assert.assertNull;
+import static org.assertj.core.api.Assertions.assertThat;
 
 /**
- * Test related to https://issues.apache.org/jira/browse/LOG4J2-2274.
+ * Test related to <a 
href="https://issues.apache.org/jira/browse/LOG4J2-2274";>LOG4J2-2274</a>.
  * <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
@@ -41,11 +41,9 @@ import static org.junit.Assert.assertNull;
  * @see PropertyPermission
  */
 @ResourceLock("java.lang.SecurityManager")
+@DisabledForJreRange(min = JRE.JAVA_18) // custom SecurityManager instances 
throw UnsupportedOperationException
 public class SystemPropertiesPropertySourceSecurityManagerIT {
 
-       @Rule
-       public final SecurityManagerTestRule rule = new 
SecurityManagerTestRule(new TestSecurityManager());
-
        /**
         * Always throws a SecurityException for any environment variables 
permission
         * check.
@@ -83,7 +81,13 @@ public class SystemPropertiesPropertySourceSecurityManagerIT 
{
         */
        @Test
        public void test() {
-               final PropertiesUtil propertiesUtil = new 
PropertiesUtil("src/test/resources/PropertiesUtilTest.properties");
-               assertNull(propertiesUtil.getStringProperty("a.1"));
+               var existing = System.getSecurityManager();
+               try {
+                       System.setSecurityManager(new TestSecurityManager());
+                       final PropertiesUtil propertiesUtil = new 
PropertiesUtil("src/test/resources/PropertiesUtilTest.properties");
+                       
assertThat(propertiesUtil.getStringProperty("a.1")).isNull();
+               } finally {
+                       System.setSecurityManager(existing);
+               }
        }
 }
diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
index e4953a817e..e1b3179a1a 100644
--- 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
@@ -16,13 +16,20 @@
  */
 package org.apache.logging.log4j.core.impl;
 
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
+import org.apache.logging.log4j.util.Strings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.IvParameterSpec;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -39,19 +46,7 @@ import java.util.Deque;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.KeyGenerator;
-import javax.crypto.spec.IvParameterSpec;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
-import org.apache.logging.log4j.util.Strings;
-import org.junit.jupiter.api.Test;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import static org.junit.jupiter.api.Assertions.*;
 
 /**
  *
@@ -142,6 +137,7 @@ public class ThrowableProxyTest {
     }
 
     @Test
+    @DisabledForJreRange(min = JRE.JAVA_18) // custom SecurityManager 
instances throw UnsupportedOperationException
     public void testLogStackTraceWithClassThatWillCauseSecurityException() 
throws IOException {
         final SecurityManager sm = System.getSecurityManager();
         try {
@@ -169,6 +165,7 @@ public class ThrowableProxyTest {
     }
 
     @Test
+    @DisabledForJreRange(min = JRE.JAVA_18) // custom SecurityManager 
instances throw UnsupportedOperationException
     public void 
testLogStackTraceWithClassLoaderThatWithCauseSecurityException() throws 
Exception {
         final SecurityManager sm = System.getSecurityManager();
         try {

Reply via email to