maven-surefire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krosenv...@apache.org
Subject svn commit: r1056680 - in /maven/surefire/trunk/surefire-providers: surefire-junit/src/main/java/org/apache/maven/surefire/junit/ surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/
Date Sat, 08 Jan 2011 09:56:26 GMT
Author: krosenvold
Date: Sat Jan  8 09:56:26 2011
New Revision: 1056680

URL: http://svn.apache.org/viewvc?rev=1056680&view=rev
Log:
o Cleaned JUnit3 provider

Added:
    maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java
  (with props)
Modified:
    maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
    maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
    maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java

Modified: maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java?rev=1056680&r1=1056679&r2=1056680&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Provider.java
Sat Jan  8 09:56:26 2011
@@ -48,6 +48,9 @@ public class JUnit3Provider
 
     private final JUnit3TestChecker jUnit3TestChecker;
 
+    private final JUnit3Reflector reflector;
+
+
     private TestsToRun testsToRun;
 
     public JUnit3Provider( ProviderParameters booterParameters )
@@ -55,8 +58,8 @@ public class JUnit3Provider
         this.reporterFactory = booterParameters.getReporterFactory();
         this.testClassLoader = booterParameters.getTestClassLoader();
         this.directoryScanner = booterParameters.getDirectoryScanner();
-        this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
-
+        this.reflector = new JUnit3Reflector( testClassLoader );
+        this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader ); // Todo; use reflector
     }
 
     public RunResult invoke( Object forkTestSet )
@@ -67,12 +70,13 @@ public class JUnit3Provider
             testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass( (Class)
forkTestSet );
         }
 
+        ReporterManager reporter = (ReporterManager) reporterFactory.createReporter();
+
         for ( Iterator iter = testsToRun.iterator(); iter.hasNext(); )
         {
             Class clazz = (Class) iter.next();
-            ReporterManager reporter = (ReporterManager) reporterFactory.createReporter();
             SurefireTestSet surefireTestSet = createTestSet( clazz );
-            executeTestSet( surefireTestSet, reporterFactory, testClassLoader );
+            executeTestSet( surefireTestSet, reporter, testClassLoader );
         }
 
         return reporterFactory.close();
@@ -81,27 +85,22 @@ public class JUnit3Provider
     private SurefireTestSet createTestSet( Class clazz )
         throws TestSetFailedException
     {
-        return jUnit3TestChecker.isJunit3Test( clazz )
-            ? new JUnitTestSet( clazz )
+        return reflector.isJUnit3Available() && jUnit3TestChecker.isJunit3Test( clazz
)
+            ? new JUnitTestSet( clazz, reflector )
             : (SurefireTestSet) new PojoTestSet( clazz );
 
     }
 
-    private void executeTestSet( SurefireTestSet testSet, ReporterFactory reporterManagerFactory,
-                                 ClassLoader classLoader )
+    private void executeTestSet( SurefireTestSet testSet, ReporterManager reporterManager,
ClassLoader classLoader )
         throws ReporterException, TestSetFailedException
     {
 
-        ReporterManager reporterManager = (ReporterManager) reporterManagerFactory.createReporter();
-
         ReportEntry report = new SimpleReportEntry( this.getClass().getName(), testSet.getName()
);
 
         reporterManager.testSetStarting( report );
 
         testSet.execute( reporterManager, classLoader );
 
-        report = new SimpleReportEntry( this.getClass().getName(), testSet.getName() );
-
         reporterManager.testSetCompleted( report );
 
         reporterManager.reset();

Added: maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java?rev=1056680&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java
(added)
+++ maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java
Sat Jan  8 09:56:26 2011
@@ -0,0 +1,216 @@
+package org.apache.maven.surefire.junit;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.surefire.util.ReflectionUtils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+public final class JUnit3Reflector
+{
+    private static final String TEST_CASE = "junit.framework.Test";
+
+    private static final String TEST_RESULT = "junit.framework.TestResult";
+
+    private static final String TEST_LISTENER = "junit.framework.TestListener";
+
+    private static final String TEST = "junit.framework.Test";
+
+    private static final String ADD_LISTENER_METHOD = "addListener";
+
+    private static final String RUN_METHOD = "run";
+
+    private static final String TEST_SUITE = "junit.framework.TestSuite";
+
+    private final Class[] interfacesImplementedByDynamicProxy;
+
+    private final Class testResultClass;
+
+    private final Method addListenerMethod;
+
+    private final Method testInterfaceRunMethod;
+
+    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
+
+    private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
+
+    private final Class testInterface;
+
+    private final Class testCase;
+
+    private final Constructor testsSuiteConstructor;
+
+    public JUnit3Reflector( ClassLoader testClassLoader )
+    {
+        testResultClass = ReflectionUtils.tryLoadClass( testClassLoader, TEST_RESULT );
+        testCase = ReflectionUtils.tryLoadClass( testClassLoader, TEST_CASE );
+        testInterface = ReflectionUtils.tryLoadClass( testClassLoader, TEST );
+        interfacesImplementedByDynamicProxy =
+            new Class[]{ ReflectionUtils.tryLoadClass( testClassLoader, TEST_LISTENER ) };
+        Class[] constructorParamTypes = { Class.class };
+
+        Class testSuite = ReflectionUtils.tryLoadClass( testClassLoader, TEST_SUITE );
+
+        // The interface implemented by the dynamic proxy (TestListener), happens to be
+        // the same as the param types of TestResult.addTestListener
+        Class[] addListenerParamTypes = interfacesImplementedByDynamicProxy;
+
+        if ( isJUnit3Available() )
+        {
+            testsSuiteConstructor = ReflectionUtils.getConstructor( testSuite, constructorParamTypes
);
+            addListenerMethod = tryGetMethod( testResultClass, ADD_LISTENER_METHOD, addListenerParamTypes
);
+            testInterfaceRunMethod = getMethod( testInterface, RUN_METHOD, new Class[]{ testResultClass
} );
+        }
+        else
+        {
+            testsSuiteConstructor = null;
+            addListenerMethod = null;
+            testInterfaceRunMethod = null;
+        }
+    }
+
+    // Switch to reflectionutils when building with 2.7.2
+    private static Method tryGetMethod( Class clazz, String methodName, Class[] parameters
)
+    {
+        try
+        {
+            return clazz.getMethod( methodName, parameters );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            return null;
+        }
+    }
+
+    public static Method getMethod( Class clazz, String methodName, Class[] parameters )
+    {
+        try
+        {
+            return clazz.getMethod( methodName, parameters );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new RuntimeException( "When finding method " + methodName, e );
+        }
+    }
+
+
+    Object constructTestObject( Class testClass )
+        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
InstantiationException,
+        ClassNotFoundException
+    {
+        Object testObject = createInstanceFromSuiteMethod( testClass );
+
+        if ( testObject == null && testCase.isAssignableFrom( testClass ) )
+        {
+            Object[] constructorParams = { testClass };
+
+            testObject = testsSuiteConstructor.newInstance( constructorParams );
+        }
+
+        if ( testObject == null )
+        {
+            Constructor testConstructor = getTestConstructor( testClass );
+
+            if ( testConstructor.getParameterTypes().length == 0 )
+            {
+                testObject = testConstructor.newInstance( EMPTY_OBJECT_ARRAY );
+            }
+            else
+            {
+                testObject = testConstructor.newInstance( new Object[]{ testClass.getName()
} );
+            }
+        }
+        return testObject;
+    }
+
+    static Object createInstanceFromSuiteMethod( Class testClass )
+        throws IllegalAccessException, InvocationTargetException
+    {
+        Object testObject = null;
+        try
+        {
+            Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY );
+
+            if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic(
suiteMethod.getModifiers() ) )
+            {
+                testObject = suiteMethod.invoke( null, EMPTY_CLASS_ARRAY );
+            }
+        }
+        catch ( NoSuchMethodException e )
+        {
+            // No suite method
+        }
+        return testObject;
+    }
+
+    static Constructor getTestConstructor( Class testClass )
+        throws NoSuchMethodException
+    {
+        Constructor constructor;
+        try
+        {
+            constructor = testClass.getConstructor( new Class[]{ String.class } );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            constructor = testClass.getConstructor( EMPTY_CLASS_ARRAY );
+        }
+        return constructor;
+    }
+
+    public Class[] getInterfacesImplementedByDynamicProxy()
+    {
+        return interfacesImplementedByDynamicProxy;
+    }
+
+    public Class getTestResultClass()
+    {
+        return testResultClass;
+    }
+
+    public Method getAddListenerMethod()
+    {
+        return addListenerMethod;
+    }
+
+    public Method getTestInterfaceRunMethod()
+    {
+        return testInterfaceRunMethod;
+    }
+
+    public Class getTestInterface()
+    {
+        return testInterface;
+    }
+
+    public Method getRunMethod( Class testClass )
+    {
+        return getMethod( testClass, RUN_METHOD, new Class[]{ getTestResultClass() } );
+    }
+
+    public boolean isJUnit3Available()
+    {
+        return testResultClass != null;
+    }
+}

Propchange: maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3Reflector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java?rev=1056680&r1=1056679&r2=1056680&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
Sat Jan  8 09:56:26 2011
@@ -22,44 +22,20 @@ package org.apache.maven.surefire.junit;
 import org.apache.maven.surefire.report.ReporterManager;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.lang.reflect.Proxy;
 
 public final class JUnitTestSet
     implements SurefireTestSet
 {
-    public static final String TEST_CASE = "junit.framework.Test";
-
-    public static final String TEST_RESULT = "junit.framework.TestResult";
-
-    public static final String TEST_LISTENER = "junit.framework.TestListener";
-
-    public static final String TEST = "junit.framework.Test";
-
-    public static final String ADD_LISTENER_METHOD = "addListener";
-
-    public static final String RUN_METHOD = "run";
-
-    private static final String TEST_SUITE = "junit.framework.TestSuite";
-
-    private Class[] interfacesImplementedByDynamicProxy;
-
-    private Class testResultClass;
-
-    private Method addListenerMethod;
-
     private Method runMethod;
 
-    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
+    private final Class testClass;
 
-    private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
+    private final JUnit3Reflector reflector;
 
-    private Class testClass;
-
-    public JUnitTestSet( Class testClass )
+    public JUnitTestSet( Class testClass, JUnit3Reflector reflector )
         throws TestSetFailedException
     {
         if ( testClass == null )
@@ -68,118 +44,32 @@ public final class JUnitTestSet
         }
 
         this.testClass = testClass;
+        this.reflector = reflector;
 
-        processTestClass();
-    }
+        // ----------------------------------------------------------------------
+        // Strategy for executing JUnit tests
+        //
+        // o look for the suite method and if that is present execute that method
+        //   to get the test object.
+        //
+        // o look for test classes that are assignable from TestCase
+        //
+        // o look for test classes that only implement the Test interface
+        // ----------------------------------------------------------------------
 
-    private void processTestClass()
-        throws TestSetFailedException
-    {
-        try
-        {
-            Class testClass = getTestClass();
-            ClassLoader loader = testClass.getClassLoader();
+        // The interface implemented by the dynamic proxy (TestListener), happens to be
+        // the same as the param types of TestResult.addTestListener
 
-            testResultClass = loader.loadClass( TEST_RESULT );
-
-            Class testListenerInterface = loader.loadClass( TEST_LISTENER );
-
-            Class testInterface = loader.loadClass( TEST );
-
-            // ----------------------------------------------------------------------
-            // Strategy for executing JUnit tests
-            //
-            // o look for the suite method and if that is present execute that method
-            //   to get the test object.
-            //
-            // o look for test classes that are assignable from TestCase
-            //
-            // o look for test classes that only implement the Test interface
-            // ----------------------------------------------------------------------
-
-            interfacesImplementedByDynamicProxy = new Class[1];
-
-            interfacesImplementedByDynamicProxy[0] = testListenerInterface;
-
-            // The interface implemented by the dynamic proxy (TestListener), happens to
be
-            // the same as the param types of TestResult.addTestListener
-            Class[] addListenerParamTypes = interfacesImplementedByDynamicProxy;
-
-            addListenerMethod = testResultClass.getMethod( ADD_LISTENER_METHOD, addListenerParamTypes
);
-
-            if ( testInterface.isAssignableFrom( testClass ) )//testObject.getClass() ) )
-            {
-                runMethod = testInterface.getMethod( RUN_METHOD, new Class[]{testResultClass}
);
-
-            }
-            else
-            {
-                runMethod = testClass.getMethod( RUN_METHOD, new Class[]{testResultClass}
);
-            }
-        }
-        catch ( ClassNotFoundException e )
+        if ( this.reflector.getTestInterface().isAssignableFrom( this.testClass ) )//testObject.getClass()
) )
         {
-            throw new TestSetFailedException( "JUnit classes not available", e );
+            runMethod = this.reflector.getTestInterfaceRunMethod();
         }
-        catch ( NoSuchMethodException e )
+        else
         {
-            throw new TestSetFailedException( "Class is not a JUnit TestCase", e );
+            runMethod = reflector.getRunMethod( this.testClass );
         }
     }
 
-    private static Object constructTestObject( Class testClass )
-        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
InstantiationException,
-        ClassNotFoundException
-    {
-        Object testObject = createInstanceFromSuiteMethod( testClass );
-
-        if ( testObject == null && testClass.getClassLoader().loadClass( TEST_CASE
).isAssignableFrom( testClass ) )
-        {
-            Class[] constructorParamTypes = {Class.class};
-
-            Constructor constructor =
-                testClass.getClassLoader().loadClass( TEST_SUITE ).getConstructor( constructorParamTypes
);
-
-            Object[] constructorParams = {testClass};
-
-            testObject = constructor.newInstance( constructorParams );
-        }
-
-        if ( testObject == null )
-        {
-            Constructor testConstructor = getTestConstructor( testClass );
-
-            if ( testConstructor.getParameterTypes().length == 0 )
-            {
-                testObject = testConstructor.newInstance( EMPTY_OBJECT_ARRAY );
-            }
-            else
-            {
-                testObject = testConstructor.newInstance( new Object[]{testClass.getName()}
);
-            }
-        }
-        return testObject;
-    }
-
-    private static Object createInstanceFromSuiteMethod( Class testClass )
-        throws IllegalAccessException, InvocationTargetException
-    {
-        Object testObject = null;
-        try
-        {
-            Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY );
-
-            if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic(
suiteMethod.getModifiers() ) )
-            {
-                testObject = suiteMethod.invoke( null, EMPTY_CLASS_ARRAY );
-            }
-        }
-        catch ( NoSuchMethodException e )
-        {
-            // No suite method
-        }
-        return testObject;
-    }
 
     public void execute( ReporterManager reportManager, ClassLoader loader )
         throws TestSetFailedException
@@ -188,21 +78,21 @@ public final class JUnitTestSet
 
         try
         {
-            Object testObject = constructTestObject( testClass );
+            Object testObject = reflector.constructTestObject( testClass );
 
-            Object instanceOfTestResult = testResultClass.newInstance();
+            Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
 
             TestListenerInvocationHandler invocationHandler =
                 new TestListenerInvocationHandler( reportManager, instanceOfTestResult, loader
);
 
             Object testListener =
-                Proxy.newProxyInstance( loader, interfacesImplementedByDynamicProxy, invocationHandler
);
+                Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(),
invocationHandler );
 
-            Object[] addTestListenerParams = {testListener};
+            Object[] addTestListenerParams = { testListener };
 
-            addListenerMethod.invoke( instanceOfTestResult, addTestListenerParams );
+            reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams
);
 
-            Object[] runParams = {instanceOfTestResult};
+            Object[] runParams = { instanceOfTestResult };
 
             runMethod.invoke( testObject, runParams );
         }
@@ -232,21 +122,6 @@ public final class JUnitTestSet
         }
     }
 
-    private static Constructor getTestConstructor( Class testClass )
-        throws NoSuchMethodException
-    {
-        Constructor constructor;
-        try
-        {
-            constructor = testClass.getConstructor( new Class[]{String.class} );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            constructor = testClass.getConstructor( EMPTY_CLASS_ARRAY );
-        }
-        return constructor;
-    }
-
     public String getName()
     {
         return testClass.getName();

Modified: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java?rev=1056680&r1=1056679&r2=1056680&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
Sat Jan  8 09:56:26 2011
@@ -59,7 +59,6 @@ public class JUnitCoreProvider
 
     private final ReporterConfiguration reporterConfiguration;
 
-    @SuppressWarnings( { "UnusedDeclaration" } )
     public JUnitCoreProvider( ProviderParameters booterParameters )
     {
         this.reporterFactory = booterParameters.getReporterFactory();



Mime
View raw message