Author: jvanzyl
Date: Sun Dec 11 23:08:32 2005
New Revision: 356189

URL: http://svn.apache.org/viewcvs?rev=356189&view=rev
Log:
o pushing the forking code into the booter itself

Removed:
    
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/ForkedSurefireRunner.java
Modified:
    
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/SurefireBooter.java

Modified: 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/SurefireBooter.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/SurefireBooter.java?rev=356189&r1=356188&r2=356189&view=diff
==============================================================================
--- 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/SurefireBooter.java
 (original)
+++ 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/SurefireBooter.java
 Sun Dec 11 23:08:32 2005
@@ -29,14 +29,16 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.io.FileInputStream;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.Arrays;
 
 /**
- * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
+ * @author Jason van Zyl
  * @author <a href="mail-to:[EMAIL PROTECTED]">Emmanuel Venisse</a>
  * @version $Id$
  */
@@ -46,7 +48,7 @@
 
     protected static final String PS = System.getProperty( "path.separator" );
 
-    private static String RUNNER = 
"org.apache.maven.surefire.ForkedSurefireRunner";
+    private static String RUNNER = "org.apache.maven.surefire.SurefireBooter";
 
     private static String BATTERY_EXECUTOR = 
"org.apache.maven.surefire.Surefire";
 
@@ -62,7 +64,7 @@
 
     private String forkMode;
 
-    private String basedir;
+    private static String basedir;
 
     private String jvm;
 
@@ -71,6 +73,30 @@
     private String argLine;
 
     // ----------------------------------------------------------------------
+    //
+    // ----------------------------------------------------------------------
+
+    public static final String FORK_ONCE = "once";
+
+    public static final String FORK_PERTEST = "pertest";
+
+    public static final String FORK_NONE = "none";
+
+    public static final String SUREFIRE_PROPERTIES = "surefire.properties";
+
+    public static final String SYSTEM_PROPERTIES = 
"surefire-system.properties";
+
+    public static final String CLASSLOADER_PROPERTIES = 
"surefire-classloader.properties";
+
+    static int TESTS_SUCCEEDED = 0;
+
+    static int TESTS_FAILED = 255;
+
+    static int ILLEGAL_ARGUMENT_EXCEPTION = 100;
+
+    static int OTHER_EXCEPTION = 200;
+
+    // ----------------------------------------------------------------------
     // Accessors
     // ----------------------------------------------------------------------
 
@@ -145,15 +171,15 @@
     {
         boolean result = false;
 
-        if ( forkMode.equals( ForkedSurefireRunner.FORK_NONE ) )
+        if ( forkMode.equals( FORK_NONE ) )
         {
             result = runTestsInProcess();
         }
-        else if ( forkMode.equals( ForkedSurefireRunner.FORK_ONCE ) )
+        else if ( forkMode.equals( FORK_ONCE ) )
         {
             result = runTestsForkOnce();
         }
-        else if ( forkMode.equals( ForkedSurefireRunner.FORK_PERTEST ) )
+        else if ( forkMode.equals( FORK_PERTEST ) )
         {
             result = runTestsForkEach();
         }
@@ -252,13 +278,13 @@
 
         cli.createArgument().setValue( "-classpath" );
 
-        cli.createArgument().setValue( surefireBooterJar );
+        cli.createArgument().setValue( surefireBooterJar + PS + plexusUtilsJar 
);
 
         cli.createArgument().setValue( RUNNER );
 
         cli.createArgument().setValue( basedir );
 
-        System.out.println( Commandline.toString( cli.getCommandline() ) );
+        //System.out.println( Commandline.toString( cli.getCommandline() ) );
 
         Writer stringWriter = new StringWriter();
 
@@ -348,6 +374,8 @@
 
     private String surefireBooterJar;
 
+    private String plexusUtilsJar;
+
     private void getForkArgs( String batteryConfig )
         throws Exception
     {
@@ -364,6 +392,10 @@
             {
                 surefireBooterJar = entry;
             }
+            else if ( entry.indexOf( "plexus-utils" ) > 0 )
+            {
+                plexusUtilsJar = entry;
+            }
             else
             {
                 p.setProperty( Integer.toString( i ), entry );
@@ -371,7 +403,7 @@
 
         }
 
-        FileOutputStream fos = new FileOutputStream( new File( basedir, 
ForkedSurefireRunner.CLASSLOADER_PROPERTIES ) );
+        FileOutputStream fos = new FileOutputStream( new File( basedir, 
CLASSLOADER_PROPERTIES ) );
 
         p.store( fos, "classpath entries" );
 
@@ -379,7 +411,7 @@
 
         if ( systemProperties != null )
         {
-            File f = new File( basedir, ForkedSurefireRunner.SYSTEM_PROPERTIES 
);
+            File f = new File( basedir, SYSTEM_PROPERTIES );
 
             fos = new FileOutputStream( f );
 
@@ -400,7 +432,7 @@
 
         p.setProperty( "batteryConfig", batteryConfig );
 
-        fos = new FileOutputStream( new File( basedir, 
ForkedSurefireRunner.SUREFIRE_PROPERTIES ) );
+        fos = new FileOutputStream( new File( basedir, SUREFIRE_PROPERTIES ) );
 
         p.store( fos, "surefire properties" );
 
@@ -479,6 +511,189 @@
         }
 
         return batteryConfig;
+    }
+
+    // ----------------------------------------------------------------------
+    //
+    // ----------------------------------------------------------------------
+
+    private static Properties loadProperties( String file )
+        throws Exception
+    {
+        File f = new File( basedir, file );
+
+        Properties p = new Properties();
+
+        if ( !f.exists() )
+        {
+            return p;
+        }
+
+        f.deleteOnExit();
+
+        p.load( new FileInputStream( f ) );
+
+        return p;
+    }
+
+    private static Properties getSurefireProperties()
+        throws Exception
+    {
+        return loadProperties( SUREFIRE_PROPERTIES );
+    }
+
+    private static void  setSystemProperties()
+        throws Exception
+    {
+        Properties p = loadProperties( SYSTEM_PROPERTIES );
+
+        for ( Iterator i = p.keySet().iterator(); i.hasNext(); )
+        {
+            String key = (String) i.next();
+
+            System.setProperty( key, p.getProperty( key ) );
+        }
+    }
+
+    private static ClassLoader createForkingClassLoader()
+        throws Exception
+    {
+        Properties p = loadProperties( CLASSLOADER_PROPERTIES );
+
+        IsolatedClassLoader classLoader = new IsolatedClassLoader( 
ClassLoader.getSystemClassLoader() );
+
+        for ( Iterator i = p.values().iterator(); i.hasNext(); )
+        {
+            String entry = (String) i.next();
+
+            classLoader.addURL( new File( entry ).toURL() );
+        }
+
+        return classLoader;
+    }
+
+    /**
+     * This method is invoked when Surefire is forked - this method parses and
+     * organizes the arguments passed to it and then calls the Surefire class'
+     * run method.
+     *
+     * @param args
+     * @throws Exception
+     */
+    public static void main( String[] args )
+        throws Exception
+    {
+        ClassLoader classLoader = createForkingClassLoader();
+
+        setSystemProperties();
+
+        Properties p = getSurefireProperties();
+
+        String batteryExecutorName = p.getProperty( "batteryExecutorName" );
+
+        Class batteryExecutorClass = classLoader.loadClass( 
batteryExecutorName );
+
+        Object batteryExecutor = batteryExecutorClass.newInstance();
+
+        String reports = p.getProperty( "reportClassNames" );
+
+        String[] reportClasses = reports.split( "," );
+
+        List reportList = Arrays.asList( reportClasses );
+
+        String batteryConfig = p.getProperty( "batteryConfig" );
+
+        String[] batteryParts = batteryConfig.split( "\\|" );
+
+        String batteryClassName = batteryParts[0];
+
+        Object[] batteryParms;
+
+        String forkMode = p.getProperty( "forkMode" );
+
+        if ( forkMode.equals( FORK_ONCE ) )
+        {
+            batteryParms = new Object[batteryParts.length - 1];
+
+            batteryParms[0] = new File( batteryParts[1] );
+
+            String stringList = batteryParts[2];
+
+            if ( stringList.startsWith( "[" ) && stringList.endsWith( "]" ) )
+            {
+                stringList = stringList.substring( 1, stringList.length() - 1 
);
+            }
+
+            ArrayList includesList = new ArrayList();
+
+            String[] stringArray = stringList.split( "," );
+
+            for ( int i = 0; i < stringArray.length; i++ )
+            {
+                includesList.add( stringArray[i].trim() );
+            }
+
+            batteryParms[1] = includesList;
+
+            stringList = batteryParts[3];
+
+            ArrayList excludesList = new ArrayList();
+
+            if ( stringList.startsWith( "[" ) && stringList.endsWith( "]" ) )
+            {
+                stringList = stringList.substring( 1, stringList.length() - 1 
);
+            }
+
+            stringArray = stringList.split( "," );
+
+            for ( int i = 0; i < stringArray.length; i++ )
+            {
+                excludesList.add( stringArray[i].trim() );
+            }
+
+            batteryParms[2] = excludesList;
+        }
+        else
+        {
+            batteryParms = new Object[1];
+
+            batteryParms[0] = batteryParts[1];
+        }
+
+        List batteryHolders = new ArrayList();
+
+        batteryHolders.add( new Object[]{batteryClassName, batteryParms} );
+
+        String reportsDirectory = p.getProperty( "reportsDirectory" );
+
+        Method run = batteryExecutorClass.getMethod( "run", new 
Class[]{List.class, List.class, String.class} );
+
+        Object[] parms = new Object[]{reportList, batteryHolders, 
reportsDirectory};
+
+        int returnCode = TESTS_FAILED;
+
+        try
+        {
+            boolean result = ( (Boolean) run.invoke( batteryExecutor, parms ) 
).booleanValue();
+
+            if ( result )
+            {
+                returnCode = TESTS_SUCCEEDED;
+            }
+
+        }
+        catch ( IllegalArgumentException e )
+        {
+            returnCode = ILLEGAL_ARGUMENT_EXCEPTION;
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+
+            returnCode = OTHER_EXCEPTION;
+        }
+
+        System.exit( returnCode );
     }
 }
 


Reply via email to