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 kr...@apache.org on 2010/12/11 20:42:48 UTC
svn commit: r1044698 - in /maven/surefire/trunk:
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/
surefire-api/src/main/java/org/apache/maven/surefire/booter/
surefire-api/src/main/java/org/apache/maven/surefire/report...
Author: krosenvold
Date: Sat Dec 11 19:42:47 2010
New Revision: 1044698
URL: http://svn.apache.org/viewvc?rev=1044698&view=rev
Log:
o Extracted reflection into separate class
Added:
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java (with props)
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java (with props)
Modified:
maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReportEntry.java
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReporterManagerFactory.java
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/suite/SuiteDefinition.java
maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/Foo.java
maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java (original)
+++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java Sat Dec 11 19:42:47 2010
@@ -4,13 +4,9 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.lang.reflect.Array;
import java.net.URL;
-import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
import java.util.Set;
/**
@@ -20,33 +16,6 @@ import java.util.Set;
*/
public class ProviderDetector
{
- /**
- * Method loadServices loads the services of a class that are
- * defined using the SPI mechanism.
- *
- * @param clazz The interface / abstract class defining the service.
- * @param classLoader of type ClassLoader the classloader to use.
- * @return An array of instances.
- * @throws IOException When unable to read/load the manifests
- */
- public static Object[] loadServices( Class clazz, ClassLoader classLoader )
- throws IOException
- {
- final String resourceName = "META-INF/services/" + clazz.getName();
-
- if ( classLoader == null )
- {
- return new Object[0];
- }
- final Enumeration urlEnumeration = classLoader.getResources( resourceName );
- final Set names = getNames( urlEnumeration );
- if ( names == null || names.size() == 0 )
- {
- return (Object[]) Array.newInstance( clazz, 0 );
- }
-
- return instantiateServices( clazz, classLoader, names );
- }
public static Set getServiceNames( Class clazz, ClassLoader classLoader )
throws IOException
@@ -61,37 +30,6 @@ public class ProviderDetector
return getNames( urlEnumeration );
}
- private static Object[] instantiateServices( Class clazz, ClassLoader classLoader, Set names )
- {
- List result = new ArrayList();
- for ( Iterator i = names.iterator(); i.hasNext(); )
- {
- String name = (String) i.next();
- try
- {
- Class implClass = classLoader.loadClass( name );
- if ( !clazz.isAssignableFrom( implClass ) )
- {
- continue;
- }
- result.add( implClass.newInstance() );
- }
- catch ( ClassNotFoundException e )
- {
- // ignore
- }
- catch ( IllegalAccessException e )
- {
- // ignore
- }
- catch ( InstantiationException e )
- {
- // ignore
- }
- }
- return result.toArray( (Object[]) Array.newInstance( clazz, result.size() ) );
- }
-
/**
* Method loadServices loads the services of a class that are
Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java (original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java Sat Dec 11 19:42:47 2010
@@ -32,11 +32,12 @@ import org.apache.maven.surefire.suite.R
import org.apache.maven.surefire.testset.DirectoryScannerParameters;
import org.apache.maven.surefire.testset.TestArtifactInfo;
import org.apache.maven.surefire.testset.TestRequest;
+import org.apache.maven.surefire.util.ReflectionUtils;
+import org.apache.maven.surefire.util.SurefireReflectionException;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;
@@ -76,10 +77,6 @@ public class SurefireReflector
private final Class booterParameters;
- private static final Class[] noargs = new Class[0];
-
- private static final Object[] noargsValues = new Object[0];
-
public SurefireReflector( ClassLoader surefireClassLoader )
{
this.classLoader = surefireClassLoader;
@@ -97,11 +94,11 @@ public class SurefireReflector
reporterConfigurationAware = surefireClassLoader.loadClass( ReporterConfigurationAware.class.getName() );
providerPropertiesAware = surefireClassLoader.loadClass( ProviderPropertiesAware.class.getName() );
runResult = surefireClassLoader.loadClass( RunResult.class.getName() );
- booterParameters= surefireClassLoader.loadClass( ProviderParameters.class.getName() );
+ booterParameters = surefireClassLoader.loadClass( ProviderParameters.class.getName() );
}
catch ( ClassNotFoundException e )
{
- throw new RuntimeException( "When loading class", e );
+ throw new SurefireReflectionException( e );
}
}
@@ -111,12 +108,12 @@ public class SurefireReflector
{
return result;
}
- final Integer getCompletedCount1 = (Integer) invokeGetter( result, "getCompletedCount" );
- final Integer getErrors = (Integer) invokeGetter( result, "getErrors" );
- final Integer getSkipped = (Integer) invokeGetter( result, "getSkipped" );
- final Integer getFailures = (Integer) invokeGetter( result, "getFailures" );
- return new RunResult( getCompletedCount1.intValue(), getErrors.intValue(),
- getFailures.intValue(), getSkipped.intValue() );
+ final Integer getCompletedCount1 = (Integer) ReflectionUtils.invokeGetter( result, "getCompletedCount" );
+ final Integer getErrors = (Integer) ReflectionUtils.invokeGetter( result, "getErrors" );
+ final Integer getSkipped = (Integer) ReflectionUtils.invokeGetter( result, "getSkipped" );
+ final Integer getFailures = (Integer) ReflectionUtils.invokeGetter( result, "getFailures" );
+ return new RunResult( getCompletedCount1.intValue(), getErrors.intValue(), getFailures.intValue(),
+ getSkipped.intValue() );
}
@@ -147,17 +144,16 @@ public class SurefireReflector
}
- Object createTestSuiteDefinition( TestRequest suiteDefinition )
+ Object createTestRequest( TestRequest suiteDefinition )
{
if ( suiteDefinition == null )
{
return null;
}
Class[] arguments = { List.class, File.class, String.class };
- Constructor constructor = getConstructor( this.testRequest, arguments );
- return newInstance( constructor,
- new Object[]{ suiteDefinition.getSuiteXmlFiles(), suiteDefinition.getTestSourceDirectory(),
- suiteDefinition.getRequestedTest() } );
+ Constructor constructor = ReflectionUtils.getConstructor( this.testRequest, arguments );
+ return ReflectionUtils.newInstance( constructor, new Object[]{ suiteDefinition.getSuiteXmlFiles(),
+ suiteDefinition.getTestSourceDirectory(), suiteDefinition.getRequestedTest() } );
}
@@ -168,10 +164,12 @@ public class SurefireReflector
return null;
}
Class[] arguments = { File.class, List.class, List.class, Boolean.class };
- Constructor constructor = getConstructor( this.directoryScannerParameters, arguments );
- return newInstance( constructor, new Object[]{ directoryScannerParameters.getTestClassesDirectory(),
- directoryScannerParameters.getIncludes(), directoryScannerParameters.getExcludes(),
- directoryScannerParameters.isFailIfNoTests() } );
+ Constructor constructor = ReflectionUtils.getConstructor( this.directoryScannerParameters, arguments );
+ return ReflectionUtils.newInstance( constructor,
+ new Object[]{ directoryScannerParameters.getTestClassesDirectory(),
+ directoryScannerParameters.getIncludes(),
+ directoryScannerParameters.getExcludes(),
+ directoryScannerParameters.isFailIfNoTests() } );
}
Object createTestArtifactInfo( TestArtifactInfo testArtifactInfo )
@@ -181,168 +179,30 @@ public class SurefireReflector
return null;
}
Class[] arguments = { String.class, String.class };
- Constructor constructor = getConstructor( this.testArtifactInfo, arguments );
- return newInstance( constructor,
- new Object[]{ testArtifactInfo.getVersion(), testArtifactInfo.getClassifier() } );
+ Constructor constructor = ReflectionUtils.getConstructor( this.testArtifactInfo, arguments );
+ return ReflectionUtils.newInstance( constructor, new Object[]{ testArtifactInfo.getVersion(),
+ testArtifactInfo.getClassifier() } );
}
Object createReporterConfiguration( ReporterConfiguration reporterConfiguration )
{
- Constructor constructor =
- getConstructor( this.reporterConfiguration, new Class[]{ List.class, File.class, Boolean.class } );
- return newInstance( constructor, new Object[]{ reporterConfiguration.getReports(), reporterConfiguration.getReportsDirectory(),
- reporterConfiguration.isTrimStackTrace() } );
- }
-
- private Constructor getConstructor( Class clazz, Class[] arguments )
- {
- try
- {
- return clazz.getConstructor( arguments );
- }
- catch ( NoSuchMethodException e )
- {
- throw new RuntimeException( e );
- }
- }
-
- private Object newInstance( Constructor constructor, Object[] params )
- {
- try
- {
- return constructor.newInstance( params );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InstantiationException e )
- {
- throw new RuntimeException( e );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
+ Constructor constructor = ReflectionUtils.getConstructor( this.reporterConfiguration,
+ new Class[]{ List.class, File.class,
+ Boolean.class } );
+ return ReflectionUtils.newInstance( constructor, new Object[]{ reporterConfiguration.getReports(),
+ reporterConfiguration.getReportsDirectory(), reporterConfiguration.isTrimStackTrace() } );
}
public Object createBooterConfiguration()
{
- try
- {
-
- Class clazz = classLoader.loadClass( BaseProviderFactory.class.getName());
- return clazz.newInstance();
- }
- catch ( InstantiationException e )
- {
- throw new RuntimeException( e );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( ClassNotFoundException e )
- {
- throw new RuntimeException( e );
- }
+ return ReflectionUtils.instantiate( classLoader, BaseProviderFactory.class.getName() );
}
- public Object instantiateProvider( String providerClassName, Object booterParameters )
- {
- try
- {
- Class aClass = classLoader.loadClass( providerClassName );
- Constructor constructor = getConstructor( aClass, new Class[]{ this.booterParameters } );
- return constructor.newInstance( new Object []{booterParameters} );
- }
- catch ( ClassNotFoundException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InstantiationException e )
- {
- throw new RuntimeException( e );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- }
-
-
- private static Object invokeSetter( Object surefire, Method method, Object value )
-
- {
- try
- {
- return method.invoke( surefire, new Object[]{ value } );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( "When instantiating surefire", e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e.getTargetException().getMessage(), e.getTargetException() );
- }
-
- }
-
- private static Object invokeMethod( Object surefire, Method method, Object[] args )
-
- {
- try
- {
- return method.invoke( surefire, args );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( "When instantiating surefire", e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e.getTargetException().getMessage(), e.getTargetException() );
- }
-
- }
-
- private static Object invokeGetter( Object instance, String methodName )
+ public Object instantiateProvider( String providerClassName, Object booterParameters )
{
- try
- {
- final Method method = instance.getClass().getMethod( methodName, noargs );
- return method.invoke( instance, noargsValues );
- }
- catch ( NoSuchMethodException e )
- {
- throw new RuntimeException( "When finding method " + methodName, e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( "When running method " + methodName, e );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( "When accessing method " + methodName, e );
- }
+ return ReflectionUtils.instantiateOneArg( classLoader, providerClassName, this.booterParameters, booterParameters );
}
- private static Method getMethod( Object instance, String methodName, Class[] parameters )
- {
- try
- {
- return instance.getClass().getMethod( methodName, parameters );
- }
- catch ( NoSuchMethodException e )
- {
- throw new RuntimeException( "When finding method " + methodName, e );
- }
- }
public void setIfDirScannerAware( Object o, DirectoryScannerParameters dirScannerParams )
{
@@ -360,9 +220,7 @@ public class SurefireReflector
public void setDirectoryScannerParameters( Object o, DirectoryScannerParameters dirScannerParams )
{
final Object param = createDirectoryScannerParameters( dirScannerParams );
- final Method setter =
- getMethod( o, "setDirectoryScannerParameters", new Class[]{ directoryScannerParameters } );
- invokeSetter( o, setter, param );
+ ReflectionUtils.invokeSetter( o, "setDirectoryScannerParameters", this.directoryScannerParameters, param );
}
public void setTestSuiteDefinitionAware( Object o, TestRequest testSuiteDefinition2 )
@@ -381,9 +239,8 @@ public class SurefireReflector
void setTestSuiteDefinition( Object o, TestRequest testSuiteDefinition1 )
{
- final Object param = createTestSuiteDefinition( testSuiteDefinition1 );
- final Method setter = getMethod( o, "setTestRequest", new Class[]{ testRequest } );
- invokeSetter( o, setter, param );
+ final Object param = createTestRequest( testSuiteDefinition1 );
+ ReflectionUtils.invokeSetter( o, "setTestRequest", this.testRequest, param );
}
public void setProviderPropertiesAware( Object o, Properties properties )
@@ -402,8 +259,7 @@ public class SurefireReflector
void setProviderProperties( Object o, Properties providerProperties )
{
- final Method setter = getMethod( o, "setProviderProperties", new Class[]{ Properties.class } );
- invokeSetter( o, setter, providerProperties );
+ ReflectionUtils.invokeSetter( o, "setProviderProperties", Properties.class, providerProperties );
}
public void setReporterConfigurationAware( Object o, ReporterConfiguration reporterConfiguration1 )
@@ -418,8 +274,7 @@ public class SurefireReflector
void setReporterConfiguration( Object o, ReporterConfiguration reporterConfiguration )
{
final Object param = createReporterConfiguration( reporterConfiguration );
- final Method setter = getMethod( o, "setReporterConfiguration", new Class[]{ this.reporterConfiguration } );
- invokeSetter( o, setter, param );
+ ReflectionUtils.invokeSetter( o, "setReporterConfiguration", this.reporterConfiguration, param );
}
boolean isReporterConfigurationAwareAware( Object o )
@@ -427,7 +282,7 @@ public class SurefireReflector
return reporterConfigurationAware.isAssignableFrom( o.getClass() );
}
- public void setTestClassLoaderAware( Object o, ClassLoader surefireClassLoader, ClassLoader testClassLoader)
+ public void setTestClassLoaderAware( Object o, ClassLoader surefireClassLoader, ClassLoader testClassLoader )
{
if ( isTestClassLoaderAware( o ) )
{
@@ -442,8 +297,9 @@ public class SurefireReflector
void setTestClassLoader( Object o, ClassLoader surefireClassLoader, ClassLoader testClassLoader )
{
- final Method setter = getMethod( o, "setClassLoaders", new Class[]{ ClassLoader.class, ClassLoader.class } );
- invokeMethod( o, setter, new Object[]{ surefireClassLoader, testClassLoader } );
+ final Method setter = ReflectionUtils.getMethod( o, "setClassLoaders",
+ new Class[]{ ClassLoader.class, ClassLoader.class } );
+ ReflectionUtils.invokeMethodWithArray( o, setter, new Object[]{ surefireClassLoader, testClassLoader } );
}
public void setTestArtifactInfoAware( Object o, TestArtifactInfo testArtifactInfo1 )
@@ -462,8 +318,7 @@ public class SurefireReflector
void setTestArtifactInfo( Object o, TestArtifactInfo testArtifactInfo )
{
final Object param = createTestArtifactInfo( testArtifactInfo );
- final Method setter = getMethod( o, "setTestArtifactInfo", new Class[]{ this.testArtifactInfo } );
- invokeSetter( o, setter, param );
+ ReflectionUtils.invokeSetter( o, "setTestArtifactInfo", this.testArtifactInfo, param );
}
public boolean isRunResult( Object o )
Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReportEntry.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReportEntry.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReportEntry.java (original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReportEntry.java Sat Dec 11 19:42:47 2010
@@ -50,6 +50,11 @@ public class ReportEntry
this( source, name, null, message, null );
}
+ public ReportEntry( String source, String name, String message, StackTraceWriter stackTraceWriter )
+ {
+ this( source, name, null, message, stackTraceWriter );
+ }
+
protected ReportEntry( String source, String name, String group, String message, StackTraceWriter stackTraceWriter )
{
if ( source == null )
Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReporterManagerFactory.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReporterManagerFactory.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReporterManagerFactory.java (original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/ReporterManagerFactory.java Sat Dec 11 19:42:47 2010
@@ -20,12 +20,10 @@ package org.apache.maven.surefire.report
*/
import org.apache.maven.surefire.suite.RunResult;
-import org.apache.maven.surefire.testset.SurefireConfigurationException;
-import org.apache.maven.surefire.testset.TestSetFailedException;
-import org.apache.maven.surefire.util.NestedRuntimeException;
+import org.apache.maven.surefire.util.ReflectionUtils;
+import org.apache.maven.surefire.util.SurefireReflectionException;
import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -86,7 +84,6 @@ public class ReporterManagerFactory
}
public ReporterManager createReporterManager()
- throws TestSetFailedException
{
final List reports = instantiateReports( reportDefinitions, surefireClassLoader );
return (ReporterManager) setupReporter( reports );
@@ -135,7 +132,6 @@ public class ReporterManagerFactory
}
private List instantiateReports( List reportDefinitions, ClassLoader classLoader )
- throws TestSetFailedException
{
if ( reportDefinitions.size() == 0 )
{
@@ -152,7 +148,6 @@ public class ReporterManagerFactory
}
private List instantiateReportsOldStyle( List reportDefinitions, ClassLoader classLoader )
- throws TestSetFailedException
{
List reports = new ArrayList();
@@ -194,136 +189,47 @@ public class ReporterManagerFactory
{
try
{
- return (Reporter) instantiateObject( className, params, classLoader );
- }
- catch ( ClassNotFoundException e )
- {
- throw new SurefireConfigurationException( "Unable to find class to create report '" + className + "'", e );
- }
- catch ( NoSuchMethodException e )
- {
- throw new SurefireConfigurationException(
- "Unable to find appropriate constructor to create report: " + e.getMessage(), e );
- }
- }
+ Class clazz = ReflectionUtils.loadClass( classLoader, className );
-
- private static Object instantiateObject( String className, ReporterConfiguration params, ClassLoader classLoader )
- throws SurefireConfigurationException, ClassNotFoundException, NoSuchMethodException
- {
- Class clazz = classLoader.loadClass( className );
-
- Object object;
- try
- {
if ( params != null )
{
Class[] paramTypes = new Class[1];
- paramTypes[0] = classLoader.loadClass( ReporterConfiguration.class.getName() );
+ paramTypes[0] = ReflectionUtils.loadClass( classLoader, ReporterConfiguration.class.getName() );
Constructor constructor = clazz.getConstructor( paramTypes );
- object = constructor.newInstance( new Object[]{ params } );
+ return (Reporter) ReflectionUtils.newInstance( constructor, new Object[]{ params } );
}
else
{
- object = clazz.newInstance();
+ return (Reporter) clazz.newInstance();
}
}
catch ( IllegalAccessException e )
{
- throw new SurefireConfigurationException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- catch ( InvocationTargetException e )
- {
- throw new SurefireConfigurationException( e.getTargetException().getMessage(), e.getTargetException() );
+ throw new SurefireReflectionException( e );
}
catch ( InstantiationException e )
{
- throw new SurefireConfigurationException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- return object;
- }
-
-
- private static Reporter instantiateReport( String className, Object[] params, ClassLoader classLoader )
- throws TestSetFailedException
- {
- try
- {
- return (Reporter) instantiateObject( className, params, classLoader );
- }
- catch ( ClassNotFoundException e )
- {
- throw new TestSetFailedException( "Unable to find class to create report '" + className + "'", e );
+ throw new SurefireReflectionException( e );
}
catch ( NoSuchMethodException e )
{
- throw new TestSetFailedException(
- "Unable to find appropriate constructor to create report: " + e.getMessage(), e );
+ throw new SurefireReflectionException( e );
}
}
- private static Object instantiateObject( String className, Object[] params, ClassLoader classLoader )
- throws TestSetFailedException, ClassNotFoundException, NoSuchMethodException
- {
- Class clazz = classLoader.loadClass( className );
-
- Object object;
- try
- {
- if ( params != null )
- {
- Class[] paramTypes = new Class[params.length];
-
- for ( int j = 0; j < params.length; j++ )
- {
- if ( params[j] == null )
- {
- paramTypes[j] = String.class;
- }
- else
- {
- paramTypes[j] = params[j].getClass();
- }
- }
-
- Constructor constructor = clazz.getConstructor( paramTypes );
- object = constructor.newInstance( params );
- }
- else
- {
- object = clazz.newInstance();
- }
- }
- catch ( IllegalAccessException e )
- {
- throw new TestSetFailedException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- catch ( InvocationTargetException e )
- {
- throw new TestSetFailedException( e.getTargetException().getMessage(), e.getTargetException() );
- }
- catch ( InstantiationException e )
- {
- throw new TestSetFailedException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- return object;
+ private static Reporter instantiateReport( String className, Object[] params, ClassLoader classLoader )
+ {
+ return (Reporter) ReflectionUtils.instantiateObject( className, params, classLoader );
}
private void warnIfNoTests()
{
if ( getGlobalRunStatistics().getRunResult().getCompletedCount() == 0 )
{
- try
- {
- createReporterManager().writeMessage( "There are no tests to run." );
- }
- catch ( TestSetFailedException e )
- {
- throw new NestedRuntimeException( e );
- }
+ createReporterManager().writeMessage( "There are no tests to run." );
}
}
}
Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/suite/SuiteDefinition.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/suite/SuiteDefinition.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/suite/SuiteDefinition.java (original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/suite/SuiteDefinition.java Sat Dec 11 19:42:47 2010
@@ -20,10 +20,8 @@ package org.apache.maven.surefire.suite;
*/
import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.ReflectionUtils;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Collections;
import java.util.List;
// todo: Remove once we build with 2.7
@@ -47,11 +45,6 @@ public class SuiteDefinition
return instantiateSuite( suiteClassName, params, surefireClassLoader );
}
- public List asBooterFormat()
- {
- return Collections.singletonList( new Object[]{ suiteClassName, params } );
- }
-
public static SuiteDefinition fromBooterFormat( List testSuiteDefinitions )
{
if ( testSuiteDefinitions.size() != 1 )
@@ -64,70 +57,8 @@ public class SuiteDefinition
}
// This reflection is not due to linkage issues, but only an attempt at being generic.
- private static Object instantiateObject( String className, Object[] params, ClassLoader classLoader )
- throws TestSetFailedException, ClassNotFoundException, NoSuchMethodException
- {
- Class clazz = classLoader.loadClass( className );
-
- Object object;
- try
- {
- if ( params != null )
- {
- Class[] paramTypes = new Class[params.length];
-
- for ( int j = 0; j < params.length; j++ )
- {
- if ( params[j] == null )
- {
- paramTypes[j] = String.class;
- }
- else
- {
- paramTypes[j] = params[j].getClass();
- }
- }
-
- Constructor constructor = clazz.getConstructor( paramTypes );
-
- object = constructor.newInstance( params );
- }
- else
- {
- object = clazz.newInstance();
- }
- }
- catch ( IllegalAccessException e )
- {
- throw new TestSetFailedException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- catch ( InvocationTargetException e )
- {
- throw new TestSetFailedException( e.getTargetException().getMessage(), e.getTargetException() );
- }
- catch ( InstantiationException e )
- {
- throw new TestSetFailedException( "Unable to instantiate object: " + e.getMessage(), e );
- }
- return object;
- }
-
- // This reflection is not due to linkage issues, but only an attempt at being generic.
private static SurefireTestSuite instantiateSuite( String suiteClass, Object[] params, ClassLoader classLoader )
- throws TestSetFailedException
{
- try
- {
- return (SurefireTestSuite) instantiateObject( suiteClass, params, classLoader );
- }
- catch ( ClassNotFoundException e )
- {
- throw new TestSetFailedException( "Unable to find class to create suite '" + suiteClass + "'", e );
- }
- catch ( NoSuchMethodException e )
- {
- throw new TestSetFailedException(
- "Unable to find appropriate constructor to create suite: " + e.getMessage(), e );
- }
+ return (SurefireTestSuite) ReflectionUtils.instantiateObject( suiteClass, params, classLoader );
}
}
Added: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java?rev=1044698&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java (added)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java Sat Dec 11 19:42:47 2010
@@ -0,0 +1,235 @@
+package org.apache.maven.surefire.util;
+
+/*
+ * 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 java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * @author Kristian Rosenvold
+ */
+public class ReflectionUtils
+{
+ private static final Class[] noargs = new Class[0];
+
+ private static final Object[] noargsValues = new Object[0];
+
+
+ public static Method getMethod( Object instance, String methodName, Class[] parameters )
+ {
+ try
+ {
+ return instance.getClass().getMethod( methodName, parameters );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ throw new RuntimeException( "When finding method " + methodName, e );
+ }
+ }
+
+ public static Object invokeGetter( Object instance, String methodName )
+ {
+ final Method method = getMethod( instance, methodName, noargs );
+ return invokeMethodWithArray( instance, method, noargsValues );
+ }
+
+ public static Constructor getConstructor( Class clazz, Class[] arguments )
+ {
+ try
+ {
+ return clazz.getConstructor( arguments );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+
+ public static Object newInstance( Constructor constructor, Object[] params )
+ {
+ try
+ {
+ return constructor.newInstance( params );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( InstantiationException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+
+ public static Object instantiate( ClassLoader classLoader, String classname )
+ {
+ try
+ {
+
+
+ Class clazz = loadClass( classLoader, classname );
+ return clazz.newInstance();
+ }
+ catch ( InstantiationException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+
+ public static Object instantiateOneArg( ClassLoader classLoader, String className, Class param1Class,
+ Object param1 )
+ {
+
+ try
+ {
+ Class aClass = loadClass(classLoader, className );
+ Constructor constructor = ReflectionUtils.getConstructor( aClass, new Class[]{ param1Class } );
+ return constructor.newInstance( new Object[]{ param1 } );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( InstantiationException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+
+ public static Object invokeSetter( Object o, String name, Class value1clazz, Object value )
+
+ {
+ final Method setter = getMethod( o, name, new Class[]{ value1clazz } );
+ return invokeSetter( o, setter, value );
+ }
+
+ public static Object invokeSetter( Object target, Method method, Object value )
+
+ {
+ return invokeMethodWithArray( target, method, new Object[]{ value } );
+ }
+
+ public static Object invokeMethodWithArray( Object target, Method method, Object[] args )
+
+ {
+ try
+ {
+ return method.invoke( target, args );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+
+ }
+
+ public static Object instantiateObject( String className, Object[] params, ClassLoader classLoader )
+ {
+ try
+ {
+ Class clazz = loadClass( classLoader, className );
+
+ Object object;
+ if ( params != null )
+ {
+ Class[] paramTypes = new Class[params.length];
+
+ for ( int j = 0; j < params.length; j++ )
+ {
+ if ( params[j] == null )
+ {
+ paramTypes[j] = String.class;
+ }
+ else
+ {
+ paramTypes[j] = params[j].getClass();
+ }
+ }
+
+
+ Constructor constructor = getConstructor( clazz, paramTypes );
+
+ object = newInstance( constructor, params );
+ }
+ else
+ {
+ object = clazz.newInstance();
+ }
+ return object;
+
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( InstantiationException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+
+ public static Class tryLoadClass( ClassLoader classLoader, String className )
+ {
+ try
+ {
+ return classLoader.loadClass( className );
+ }
+ catch ( NoClassDefFoundError ignore )
+ {
+ }
+ catch ( ClassNotFoundException ignore )
+ {
+ }
+ return null;
+ }
+
+ public static Class loadClass( ClassLoader classLoader, String className )
+ {
+ try
+ {
+ return classLoader.loadClass( className );
+ }
+ catch ( NoClassDefFoundError e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ catch ( ClassNotFoundException e )
+ {
+ throw new SurefireReflectionException( e );
+ }
+ }
+}
Propchange: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java?rev=1044698&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java (added)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java Sat Dec 11 19:42:47 2010
@@ -0,0 +1,41 @@
+package org.apache.maven.surefire.util;
+
+/*
+ * 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.
+ */
+
+/**
+ * Exception indicating that surefire hard problems with reflection. This may be due
+ * to programming errors, incorrect configuration or incorrect dependencies, but is
+ * generally not recoverable and not relevant to handle.
+ *
+ * @author Kristian Rosenvold
+ */
+public class SurefireReflectionException
+ extends NestedRuntimeException
+{
+ /**
+ * Create a <code>SurefireReflectionException</code> with the specified cause. The
+ * <code>getMessage</code> method of this exception object will return
+ * <code>(cause == null ? "" : cause.toString())</code>.
+ */
+ public SurefireReflectionException( Throwable cause )
+ {
+ super( cause == null ? "" : cause.toString(), cause );
+ }
+}
Propchange: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/SurefireReflectionException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/Foo.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/Foo.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/Foo.java (original)
+++ maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/Foo.java Sat Dec 11 19:42:47 2010
@@ -36,7 +36,7 @@ import java.util.Properties;
/**
* @author Kristian Rosenvold
*/
-class Foo
+public class Foo
implements DirectoryScannerParametersAware, TestRequestAware, ProviderPropertiesAware, ReporterConfigurationAware,
SurefireClassLoadersAware, TestArtifactInfoAware
{
Modified: maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java?rev=1044698&r1=1044697&r2=1044698&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java (original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java Sat Dec 11 19:42:47 2010
@@ -18,6 +18,7 @@ package org.apache.maven.surefire.junit4
* under the License.
*/
+import org.apache.maven.surefire.util.ReflectionUtils;
import org.apache.maven.surefire.util.ScannerFilter;
import java.lang.annotation.Annotation;
@@ -87,20 +88,7 @@ public class JUnit4TestChecker
private Class getJUnitClass( ClassLoader classLoader, String className )
{
- Class junitClass = null;
- try
- {
- junitClass = classLoader.loadClass( className );
- }
- catch ( NoClassDefFoundError e )
- {
- // ignore this
- }
- catch ( ClassNotFoundException e )
- {
- // ignore this
- }
- return junitClass;
+ return ReflectionUtils.tryLoadClass( classLoader, className );
}
}