Author: krosenvold
Date: Thu May 12 19:02:36 2011
New Revision: 1102423
URL: http://svn.apache.org/viewvc?rev=1102423&view=rev
Log:
[SURFIRE-34] Run tests with a security manager
Only supported for JUnit3. JUnit4 does not seem to support security managers, I
dont know about TestNG
Since it is only supported by a single provider, it is activated by passing a
system property
Added:
maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java
(with props)
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml
(with props)
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java
(with props)
Modified:
maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/junit.apt.vm
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireStarter.java
maven/surefire/trunk/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
Modified:
maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/junit.apt.vm
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/junit.apt.vm?rev=1102423&r1=1102422&r2=1102423&view=diff
==============================================================================
---
maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/junit.apt.vm
(original)
+++
maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/junit.apt.vm
Thu May 12 19:02:36 2011
@@ -159,5 +159,27 @@ else
[...]
+---+
+* Using a security manager (JUnit3 only)
+
+ As long as forkMode!=never and you use JUnit3, you can run your tests with
a java security manager active.
+ The classname of the security manager must be sent as a system property
variable to the JUnit3 provider.
+
+ JUnit4 uses mechanisms internally that are not compatible with the tested
security managers and is not supported
+ by surefire.
+
++---+
+[...]
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.version}</version>
+ <configuration>
+ <systemPropertyVariables>
+
<surefire.security.manager>java.lang.SecurityManager</surefire.security.manager>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+[...]
++---+
For more information on JUnit, see the {{{http://www.junit.org}JUnit web
site}}.
Modified:
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireStarter.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireStarter.java?rev=1102423&r1=1102422&r2=1102423&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireStarter.java
(original)
+++
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireStarter.java
Thu May 12 19:02:36 2011
@@ -81,7 +81,8 @@ public class SurefireStarter
private Object createForkingReporterFactory( SurefireReflector
surefireReflector )
{
final Boolean trimStackTrace =
this.providerConfiguration.getReporterConfiguration().isTrimStackTrace();
- final PrintStream originalSystemOut =
this.providerConfiguration.getReporterConfiguration().getOriginalSystemOut();
+ final PrintStream originalSystemOut =
+
this.providerConfiguration.getReporterConfiguration().getOriginalSystemOut();
return surefireReflector.createForkingReporterFactory( trimStackTrace,
originalSystemOut );
}
@@ -114,8 +115,7 @@ public class SurefireStarter
SurefireReflector surefireReflector = new SurefireReflector(
surefireClassLoader );
- final Object factory =
- surefireReflector.createReportingReporterFactory(
startupReportConfiguration );
+ final Object factory =
surefireReflector.createReportingReporterFactory( startupReportConfiguration );
return invokeProvider( null, testsClassLoader, surefireClassLoader,
factory );
}
@@ -181,8 +181,11 @@ public class SurefireStarter
}
finally
{
- System.setOut( orgSystemOut );
- System.setErr( orgSystemErr );
+ if ( System.getSecurityManager() == null )
+ {
+ System.setOut( orgSystemOut );
+ System.setErr( orgSystemErr );
+ }
}
}
}
Added:
maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java?rev=1102423&view=auto
==============================================================================
---
maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java
(added)
+++
maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java
Thu May 12 19:02:36 2011
@@ -0,0 +1,44 @@
+package org.apache.maven.surefire.its;
+/*
+ * 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.
+ */
+
+
+/**
+ * SUREFIRE-621 Asserts proper test counts when running junit 3 tests in
parallel
+ *
+ * @author Kristian Rosenvold
+ */
+public class Surefire34SecurityManagerIT
+ extends SurefireVerifierTestClass
+{
+
+ public Surefire34SecurityManagerIT()
+ {
+ super( "/surefire-34-securityManager" );
+ }
+
+ public void testSecurityManager()
+ throws Exception
+ {
+ failNever();
+ executeTest();
+ execute( "install" );
+ assertTestSuiteResults( 2, 1, 0, 0 );
+ }
+}
\ No newline at end of file
Propchange:
maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire34SecurityManagerIT.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml?rev=1102423&view=auto
==============================================================================
---
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml
(added)
+++
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml
Thu May 12 19:02:36 2011
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.plugins.surefire</groupId>
+ <artifactId>surefire34</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>Surefire-34-SecurityManager</name>
+
+
+ <properties>
+ <junitVersion>3.8.1</junitVersion>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junitVersion}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.version}</version>
+ <configuration>
+ <systemPropertyVariables>
+
<surefire.security.manager>java.lang.SecurityManager</surefire.security.manager>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
Propchange:
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added:
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java?rev=1102423&view=auto
==============================================================================
---
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java
(added)
+++
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java
Thu May 12 19:02:36 2011
@@ -0,0 +1,41 @@
+package junit4;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+
+public class SecurityManagerTest
+ extends TestCase
+{
+
+ private boolean setUpCalled = false;
+
+ private static boolean tearDownCalled = false;
+
+ public void setUp()
+ {
+ setUpCalled = true;
+ tearDownCalled = false;
+ System.out.println( "Called setUp" );
+ }
+
+ public void tearDown()
+ {
+ setUpCalled = false;
+ tearDownCalled = true;
+ System.out.println( "Called tearDown" );
+ }
+
+ public void testSetUp()
+ {
+ assertTrue( "setUp was not called", setUpCalled );
+ }
+
+ public void testWillFailWhenAccessingCurrentDirectory()
+ {
+ File file = new File( "." );
+ file.isDirectory();
+ }
+
+}
Propchange:
maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-34-securityManager/src/test/java/junit4/SecurityManagerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
maven/surefire/trunk/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java?rev=1102423&r1=1102422&r2=1102423&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
(original)
+++
maven/surefire/trunk/surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
Thu May 12 19:02:36 2011
@@ -34,6 +34,7 @@ import org.apache.maven.surefire.report.
import org.apache.maven.surefire.suite.RunResult;
import org.apache.maven.surefire.testset.TestSetFailedException;
import org.apache.maven.surefire.util.DirectoryScanner;
+import org.apache.maven.surefire.util.ReflectionUtils;
import org.apache.maven.surefire.util.TestsToRun;
/**
@@ -78,6 +79,14 @@ public class JUnit3Provider
final RunListener reporter = reporterFactory.createReporter();
ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
+ final String smClassName = System.getProperty(
"surefire.security.manager" );
+ if ( smClassName != null )
+ {
+ SecurityManager securityManager =
+ (SecurityManager) ReflectionUtils.instantiate(
this.getClass().getClassLoader(), smClassName );
+ System.setSecurityManager( securityManager );
+ }
+
for ( Iterator iter = testsToRun.iterator(); iter.hasNext(); )
{
Class clazz = (Class) iter.next();