This is an automated email from the ASF dual-hosted git repository. tibordigana pushed a commit to branch SUREFIRE-1975 in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
commit e4d67a5de9b93d29fd94390155ff67e8a749b1aa Author: Tibor Digaňa <[email protected]> AuthorDate: Sat Jan 1 01:19:42 2022 +0100 [SUREFIRE-1975] JDK18 - The Security Manager is deprecated and will be removed in a future release --- .../surefire/api/util/internal/ObjectUtils.java | 9 +++++++ .../java/org/apache/maven/JUnit4SuiteTest.java | 4 ++- .../api/util/internal/ObjectUtilsTest.java} | 30 ++++++++-------------- .../maven/surefire/booter/ProviderFactory.java | 3 ++- .../its/jiras/Surefire34SecurityManagerIT.java | 9 +++++++ .../maven/surefire/junit/JUnit3Provider.java | 26 +++++++++++++------ .../maven/surefire/junit/JUnitTestSetTest.java | 30 ++++++++++++++++++++++ 7 files changed, 82 insertions(+), 29 deletions(-) diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java index b316be2..50acdb1 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java @@ -22,6 +22,8 @@ package org.apache.maven.surefire.api.util.internal; import java.lang.management.ManagementFactory; import java.util.Map; +import static org.apache.maven.surefire.api.util.ReflectionUtils.tryLoadClass; + /** * Similar to Java 7 java.util.Objects. * @@ -44,4 +46,11 @@ public final class ObjectUtils { return ManagementFactory.getRuntimeMXBean().getSystemProperties(); } + + public static boolean isSecurityManagerSupported() + { + ClassLoader classLoader = ObjectUtils.class.getClassLoader(); + Class<?> smClass = tryLoadClass( classLoader, "java.lang.SecurityManager" ); + return smClass != null && !smClass.isAnnotationPresent( Deprecated.class ); + } } diff --git a/surefire-api/src/test/java/org/apache/maven/JUnit4SuiteTest.java b/surefire-api/src/test/java/org/apache/maven/JUnit4SuiteTest.java index c707ed6..016cce4 100644 --- a/surefire-api/src/test/java/org/apache/maven/JUnit4SuiteTest.java +++ b/surefire-api/src/test/java/org/apache/maven/JUnit4SuiteTest.java @@ -42,6 +42,7 @@ import org.apache.maven.surefire.api.util.internal.ChannelsReaderTest; import org.apache.maven.surefire.api.util.internal.ChannelsWriterTest; import org.apache.maven.surefire.api.util.internal.ConcurrencyUtilsTest; import org.apache.maven.surefire.api.util.internal.ImmutableMapTest; +import org.apache.maven.surefire.api.util.internal.ObjectUtilsTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -72,7 +73,8 @@ import org.junit.runners.Suite; ChannelsWriterTest.class, AsyncSocketTest.class, AbstractStreamEncoderTest.class, - AbstractStreamDecoderTest.class + AbstractStreamDecoderTest.class, + ObjectUtilsTest.class } ) @RunWith( Suite.class ) public class JUnit4SuiteTest diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/ObjectUtilsTest.java similarity index 59% copy from surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java copy to surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/ObjectUtilsTest.java index b316be2..07194ea 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ObjectUtils.java +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/ObjectUtilsTest.java @@ -19,29 +19,21 @@ package org.apache.maven.surefire.api.util.internal; * under the License. */ -import java.lang.management.ManagementFactory; -import java.util.Map; +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; /** - * Similar to Java 7 java.util.Objects. - * - * @author <a href="mailto:[email protected]">Tibor Digana (tibor17)</a> - * @since 2.20 + * @since 3.0.0-M6 */ -public final class ObjectUtils +public class ObjectUtilsTest { - private ObjectUtils() - { - throw new IllegalStateException( "no instantiable constructor" ); - } - - public static <T> T useNonNull( T target, T fallback ) - { - return target == null ? fallback : target; - } - - public static Map<String, String> systemProps() + @Test + public void shouldSupportSecurityManager() { - return ManagementFactory.getRuntimeMXBean().getSystemProperties(); + float javaVersion = Float.parseFloat( System.getProperty( "java.specification.version" ) ); + boolean isJava17Plus = javaVersion >= 17; + assertThat( ObjectUtils.isSecurityManagerSupported() ) + .isEqualTo( !isJava17Plus ); } } diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java index 86b430e..43461df 100644 --- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java +++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java @@ -32,6 +32,7 @@ import static org.apache.maven.surefire.api.util.ReflectionUtils.getMethod; import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeGetter; import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray; import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray2; +import static org.apache.maven.surefire.api.util.internal.ObjectUtils.isSecurityManagerSupported; /** * Creates the surefire provider. @@ -85,7 +86,7 @@ public class ProviderFactory } finally { - if ( restoreStreams && System.getSecurityManager() == null ) + if ( restoreStreams && ( !isSecurityManagerSupported() || System.getSecurityManager() == null ) ) { System.setOut( orgSystemOut ); System.setErr( orgSystemErr ); diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire34SecurityManagerIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire34SecurityManagerIT.java index bb80aec..a3be026 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire34SecurityManagerIT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire34SecurityManagerIT.java @@ -21,8 +21,11 @@ package org.apache.maven.surefire.its.jiras; import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; import org.apache.maven.surefire.its.fixture.SurefireLauncher; +import org.junit.BeforeClass; import org.junit.Test; +import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaMaxVersion; + /** * SUREFIRE-621 Asserts proper test counts when running junit 3 tests in parallel * @@ -31,6 +34,12 @@ import org.junit.Test; public class Surefire34SecurityManagerIT extends SurefireJUnit4IntegrationTestCase { + @BeforeClass + public static void checkJavaVersion() + { + assumeJavaMaxVersion( 16 ); + } + @Test public void testSecurityManager() { diff --git a/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java b/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java index f7c2c0c..cc0924e 100644 --- a/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java +++ b/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java @@ -19,6 +19,7 @@ package org.apache.maven.surefire.junit; * under the License. */ +import org.apache.maven.surefire.api.util.internal.ObjectUtils; import org.apache.maven.surefire.common.junit3.JUnit3Reflector; import org.apache.maven.surefire.common.junit3.JUnit3TestChecker; import org.apache.maven.surefire.api.provider.AbstractProvider; @@ -31,13 +32,13 @@ import org.apache.maven.surefire.api.report.SimpleReportEntry; import org.apache.maven.surefire.api.report.TestSetReportEntry; import org.apache.maven.surefire.api.suite.RunResult; import org.apache.maven.surefire.api.testset.TestSetFailedException; -import org.apache.maven.surefire.api.util.ReflectionUtils; import org.apache.maven.surefire.api.util.RunOrderCalculator; import org.apache.maven.surefire.api.util.ScanResult; import org.apache.maven.surefire.api.util.TestsToRun; import java.util.Map; +import static org.apache.maven.surefire.api.util.ReflectionUtils.instantiate; import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps; /** @@ -100,13 +101,7 @@ public class JUnit3Provider final RunListener reporter = reporterFactory.createReporter(); ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter ); Map<String, String> systemProperties = systemProps(); - String smClassName = System.getProperty( "surefire.security.manager" ); - if ( smClassName != null ) - { - SecurityManager securityManager = - ReflectionUtils.instantiate( getClass().getClassLoader(), smClassName, SecurityManager.class ); - System.setSecurityManager( securityManager ); - } + setSystemManager( System.getProperty( "surefire.security.manager" ) ); for ( Class<?> clazz : testsToRun ) { @@ -121,6 +116,21 @@ public class JUnit3Provider return runResult; } + static void setSystemManager( String smClassName ) throws TestSetFailedException + { + if ( smClassName != null ) + { + if ( !ObjectUtils.isSecurityManagerSupported() ) + { + throw new TestSetFailedException( "JDK does not support overriding Security Manager with " + + "a value in system property 'surefire.security.manager'." ); + } + ClassLoader classLoader = JUnit3Provider.class.getClassLoader(); + SecurityManager sm = instantiate( classLoader, smClassName, SecurityManager.class ); + System.setSecurityManager( sm ); + } + } + private SurefireTestSet createTestSet( Class<?> clazz ) { return reflector.isJUnit3Available() && jUnit3TestChecker.accept( clazz ) diff --git a/surefire-providers/surefire-junit3/src/test/java/org/apache/maven/surefire/junit/JUnitTestSetTest.java b/surefire-providers/surefire-junit3/src/test/java/org/apache/maven/surefire/junit/JUnitTestSetTest.java index 594dcc5..b6b93de 100644 --- a/surefire-providers/surefire-junit3/src/test/java/org/apache/maven/surefire/junit/JUnitTestSetTest.java +++ b/surefire-providers/surefire-junit3/src/test/java/org/apache/maven/surefire/junit/JUnitTestSetTest.java @@ -22,15 +22,19 @@ package org.apache.maven.surefire.junit; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.maven.surefire.api.testset.TestSetFailedException; import org.apache.maven.surefire.common.junit3.JUnit3Reflector; import org.apache.maven.surefire.api.report.ReportEntry; import org.apache.maven.surefire.api.report.RunListener; import org.apache.maven.surefire.api.report.RunMode; import org.apache.maven.surefire.api.report.TestSetReportEntry; +import org.apache.maven.surefire.shared.lang3.JavaVersion; import java.util.ArrayList; import java.util.List; +import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeGetter; + /** * */ @@ -54,6 +58,32 @@ public class JUnitTestSetTest succeededTests.get( 0 ).getName() ); } + public void testSystemManager() + { + float javaVersion = Float.parseFloat( JavaVersion.JAVA_RECENT.toString() ); + boolean isDeprecated = javaVersion >= 17; + try + { + JUnit3Provider.setSystemManager( "java.lang.SecurityManager" ); + + if ( isDeprecated ) + { + fail(); + } + + Object sm = invokeGetter( System.class, null, "getSecurityManager" ); + assertNotNull( sm ); + assertEquals( "java.lang.SecurityManager", sm.getClass().getName() ); + } + catch ( TestSetFailedException e ) + { + if ( !isDeprecated ) + { + fail(); + } + } + } + /** * */
