You are viewing a plain text version of this content. The canonical link for it is here.
Posted to surefire-commits@maven.apache.org by df...@apache.org on 2008/05/02 07:16:08 UTC

svn commit: r652767 - in /maven/surefire/trunk: maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/ maven-surefire-plugin/src/site/apt/examples/ surefire-booter/src/main/java/org/apache/maven/surefire/booter/ surefire-integration-test...

Author: dfabulich
Date: Thu May  1 22:16:07 2008
New Revision: 652767

URL: http://svn.apache.org/viewvc?rev=652767&view=rev
Log:
[SUREFIRE-490] Provide an option to use simplified classloading

Added:
    maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/TestPlainOldJavaClasspath.java
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/pom.xml
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/BasicTest.java
Modified:
    maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java
    maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/class-loading.apt
    maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java
    maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java

Modified: maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java?rev=652767&r1=652766&r2=652767&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java (original)
+++ maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java Thu May  1 22:16:07 2008
@@ -488,6 +488,20 @@
      * @since 2.3
      */
     private Boolean useSystemClassLoader;
+    
+    /**
+     * By default, Surefire forks your tests using a manifest-only jar; set this parameter
+     * to "false" to force it to launch your tests with a plain old Java classpath.
+     * (See http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.html
+     * for a more detailed explanation of manifest-only jars and their benefits.)
+     * 
+     * Default value is "true".  Beware, setting this to "false" may cause your tests to
+     * fail on Windows if your classpath is too long.
+     * 
+     * @parameter expression="${surefire.useManifestOnlyJar}" default-value="true"
+     * @since 2.4.3
+     */
+    private boolean useManifestOnlyJar;
 
     /**
      * By default, Surefire enables JVM assertions for the execution of your test cases. To disable the assertions, set
@@ -867,6 +881,7 @@
         {
             useSystemClassLoader = useSystemClassLoader == null ? Boolean.TRUE : useSystemClassLoader;
             fork.setUseSystemClassLoader( useSystemClassLoader.booleanValue() );
+            fork.setUseManifestOnlyJar( useManifestOnlyJar );
 
             fork.setSystemProperties( systemProperties );
             

Modified: maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/class-loading.apt
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/class-loading.apt?rev=652767&r1=652766&r2=652767&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/class-loading.apt (original)
+++ maven/surefire/trunk/maven-surefire-plugin/src/site/apt/examples/class-loading.apt Thu May  1 22:16:07 2008
@@ -13,7 +13,7 @@
 
 * Executive Summary
 
- If you're having problems, you'll probably want to tinker with these three settings: forkMode, useSystemClassLoader, and childDelegation.
+ If you're having problems, you'll probably want to tinker with these three settings: forkMode, useSystemClassLoader, and useManifestOnlyJar.
 
 * What problem does Surefire solve?
 
@@ -84,28 +84,37 @@
  tests in the same process as Maven itself.  But that itself can be pretty risky,
  especially if Maven is running embedded in your IDE!
  
+ Finally, of course, you could just try to wire up a plain old Java classpath and hope
+ it's short enough.  The worst case there is that your classpath might work
+ on some machines and not others.  Windows boxes would behave differently from
+ Linux boxes; users with short user names might have more success than users
+ with long user names, etc.  For this reason, we chose not to make the basic
+ classpath the default, though we do provide it as an option (mostly as a last
+ resort).
+ 
 * What does Surefire do?
 
- Surefire provides a mechanism for using either strategy.  The parameter that
+ Surefire provides a mechanism for using multiple strategies.  The main parameter that
  determines this is called "useSystemClassLoader".  If useSystemClassLoader is
  true, then we use a manifest-only jar; otherwise, we use an isolated
- classloader.
+ classloader.  If you want to use a basic plain old Java classpath, you can set
+ useManifestOnlyJar=false which only has an effect when useSystemClassLoader=true.
  
- The default value for useSystemClassLoader changed between Surefire 2.3 and
+ (The default value for useSystemClassLoader changed between Surefire 2.3 and
  Surefire 2.4, which was a pretty significant change.  In Surefire 2.3,
  useSystemClassLoader was false by default, and we used an isolated classloader. 
  In Surefire 2.4, useSystemClassLoader is true by default.  No value works for
  everyone, but we think this default is an improvement; a bunch of
- hard-to-diagnose bugs get better when we useSystemClassLoader=true.
+ hard-to-diagnose bugs get better when we useSystemClassLoader=true.)
  
- Unfortunately, if this value is set incorrectly for your app, you're going to
+ Unfortunately, if useSystemClassLoader is set incorrectly for your app, you're going to
  have a problem on your hands that can be quite difficult to diagnose.  You might
  even be forced to read a long doc page like this one.  ;-)
  
- If you're having problems when upgrading from an older version of Surefire to a
- newer version, try setting useSystemClassLoader=false to see if that helps.  You
- can do that with the POM snippet below, or by setting
- "-Dsurefire.useSystemClassLoader=false".
+ If you're having problems loading classes, try setting useSystemClassLoader=false
+ to see if that helps.  You can do that with the POM snippet below, or by setting
+ "-Dsurefire.useSystemClassLoader=false".  If that doesn't work, try setting
+ useSystemClassLoader back to true and setting useManifestOnlyJar to false.
  
 +---+
 <project>
@@ -131,8 +140,8 @@
 
  * Run mvn with --debug (aka -X) to get more detailed output
 
- * Check your forkMode.  If forkMode=never, it's impossible to use the system classloader; we have to use an isolated classloader.
+ * Check your forkMode.  If forkMode=never, it's impossible to use the system classloader or a plain old Java classpath; we have to use an isolated classloader.
 
- * If useSystemClassLoader=true, look at the surefire booter jar.  Open it up (it's just a zip) and read its manifest.
+ * If you're using the defaults, useSystemClassLoader=true and useManifestOnlyJar=false.  In that case, look at the generated manifest-only surefire booter jar.  Open it up (it's just a zip) and read its manifest.
 
  * Run mvn with -Dmaven.surefire.debug, and attach to the running process with a debugger.
\ No newline at end of file

Modified: maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java?rev=652767&r1=652766&r2=652767&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java (original)
+++ maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java Thu May  1 22:16:07 2008
@@ -52,6 +52,7 @@
     private String forkMode;
 
     private boolean useSystemClassLoader;
+    private boolean useManifestOnlyJar;
 
     private Properties systemProperties;
 
@@ -265,4 +266,14 @@
     {
         return debug;
     }
+
+    public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
+    {
+        this.useManifestOnlyJar = useManifestOnlyJar;
+    }
+    
+    public boolean isUseManifestOnlyJar()
+    {
+        return useManifestOnlyJar;
+    }
 }

Modified: maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java?rev=652767&r1=652766&r2=652767&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java (original)
+++ maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java Thu May  1 22:16:07 2008
@@ -321,7 +321,7 @@
             ClassLoader testsClassLoader;
             String testClassPath = getTestClassPathAsString();
             System.setProperty( "surefire.test.class.path", testClassPath );
-            if (useSystemClassLoader()) {
+            if (useManifestOnlyJar()) {
                 testsClassLoader = getClass().getClassLoader(); // ClassLoader.getSystemClassLoader()
                 // SUREFIRE-459, trick the app under test into thinking its classpath was conventional (instead of a single manifest-only jar) 
                 System.setProperty( "surefire.real.class.path", System.getProperty( "java.class.path" ));
@@ -513,6 +513,7 @@
         properties.setProperty( "childDelegation", String.valueOf( childDelegation ) );
         properties.setProperty( "enableAssertions", String.valueOf( enableAssertions ) );
         properties.setProperty( "useSystemClassLoader", String.valueOf( useSystemClassLoader() ) );
+        properties.setProperty( "useManifestOnlyJar", String.valueOf( useManifestOnlyJar() ) );
         properties.setProperty( "failIfNoTests", String.valueOf( failIfNoTests ) );
     }
 
@@ -604,6 +605,11 @@
     {
         return forkConfiguration.isUseSystemClassLoader() && ( isForked || forkConfiguration.isForking() );
     }
+    
+    private final boolean useManifestOnlyJar()
+    {
+        return forkConfiguration.isUseSystemClassLoader() && forkConfiguration.isUseManifestOnlyJar();
+    }
 
     private int fork( Properties properties, boolean showHeading, boolean showFooter )
         throws SurefireBooterForkException
@@ -632,7 +638,7 @@
             bootClasspath.addAll( classPathUrls );
         }
 
-        Commandline cli = forkConfiguration.createCommandLine( bootClasspath, useSystemClassLoader() );
+        Commandline cli = forkConfiguration.createCommandLine( bootClasspath, useManifestOnlyJar() );
 
         cli.createArg().setFile( surefireProperties );
 
@@ -969,6 +975,11 @@
                     surefireBooter.forkConfiguration.setUseSystemClassLoader( Boolean.valueOf(
                                                                                                p.getProperty( "useSystemClassLoader" ) ).booleanValue() );
                 }
+                else if ( "useManifestOnlyJar".equals( name ) )
+                {
+                    surefireBooter.forkConfiguration.setUseManifestOnlyJar( Boolean.valueOf(
+                                                                                               p.getProperty( "useManifestOnlyJar" ) ).booleanValue() );
+                }
                 else if ( "failIfNoTests".equals( name ) )
                 {
                     surefireBooter.setFailIfNoTests( Boolean.valueOf( p.getProperty( "failIfNoTests" ) ).booleanValue() );

Added: maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/TestPlainOldJavaClasspath.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/TestPlainOldJavaClasspath.java?rev=652767&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/TestPlainOldJavaClasspath.java (added)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/TestPlainOldJavaClasspath.java Thu May  1 22:16:07 2008
@@ -0,0 +1,31 @@
+package org.apache.maven.surefire.its;
+
+
+import junit.framework.TestCase;
+import org.apache.maven.it.Verifier;
+import org.apache.maven.it.util.ResourceExtractor;
+
+import java.io.File;
+
+/**
+ * Test useManifestOnlyJar option
+ * 
+ * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
+ * 
+ */
+public class TestPlainOldJavaClasspath
+    extends TestCase
+{
+    public void testPlainOldJavaClasspath ()
+        throws Exception
+    {
+        File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/plain-old-java-classpath" );
+
+        Verifier verifier = new Verifier( testDir.getAbsolutePath() );
+        verifier.executeGoal( "test" );
+        verifier.verifyErrorFreeLog();
+        verifier.resetStreams();
+        
+        HelperAssertions.assertTestSuiteResults( 1, 0, 0, 0, testDir );        
+    }
+}

Added: maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/pom.xml
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/pom.xml?rev=652767&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/pom.xml (added)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/pom.xml Thu May  1 22:16:07 2008
@@ -0,0 +1,51 @@
+<?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>plain-old-java-classpath</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Test for useManifestOnlyJar=false</name>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <useManifestOnlyJar>false</useManifestOnlyJar>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>

Added: maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/BasicTest.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/BasicTest.java?rev=652767&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/BasicTest.java (added)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/resources/plain-old-java-classpath/src/test/java/plainOldJavaClasspath/BasicTest.java Thu May  1 22:16:07 2008
@@ -0,0 +1,69 @@
+package plainOldJavaClasspath;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class BasicTest
+    extends TestCase
+{
+
+    private boolean setUpCalled = false;
+
+    private static boolean tearDownCalled = false;
+
+    public BasicTest( String name, String extraName )
+    {
+        super( name );
+    }
+
+    public static Test suite()
+    {
+        System.out.println("suite");
+        TestSuite suite = new TestSuite();
+        Test test = new BasicTest( "testSetUp", "dummy" );
+        suite.addTest( test );
+        TestSetup setup = new TestSetup( suite )
+        {
+
+            protected void setUp()
+            {
+                //oneTimeSetUp();
+            }
+
+            protected void tearDown()
+            {
+                oneTimeTearDown();
+            }
+
+        };
+
+        return setup;
+    }
+
+    protected void setUp()
+    {
+        setUpCalled = true;
+        tearDownCalled = false;
+        System.out.println( "Called setUp" );
+    }
+
+    protected void tearDown()
+    {
+        setUpCalled = false;
+        tearDownCalled = true;
+        System.out.println( "Called tearDown" );
+    }
+
+    public void testSetUp()
+    {
+        assertTrue( "setUp was not called", setUpCalled );
+    }
+
+    public static void oneTimeTearDown()
+    {
+        assertTrue( "tearDown was not called", tearDownCalled );
+    }
+
+}