Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-2041] Ordering test classes and methods according to -Dtest property #560

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,8 @@ private RunResult executeProvider( @Nonnull ProviderInfo provider, @Nonnull Defa
ClassLoaderConfiguration classLoaderConfiguration = getClassLoaderConfiguration();
provider.addProviderProperties();
RunOrderParameters runOrderParameters =
new RunOrderParameters( getRunOrder(), getStatisticsFile( getConfigChecksum() ), getRunOrderRandomSeed() );
new RunOrderParameters( getRunOrder(), getStatisticsFile( getConfigChecksum() ), getRunOrderRandomSeed(),
getTest() );

if ( isNotForking() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.apache.maven.surefire.booter.BooterConstants.SHUTDOWN;
import static org.apache.maven.surefire.booter.BooterConstants.SOURCE_DIRECTORY;
import static org.apache.maven.surefire.booter.BooterConstants.SPECIFIC_TEST_PROPERTY_PREFIX;
import static org.apache.maven.surefire.booter.BooterConstants.SPECIFIED_RUN_ORDER;
import static org.apache.maven.surefire.booter.BooterConstants.SYSTEM_EXIT_TIMEOUT;
import static org.apache.maven.surefire.booter.BooterConstants.TEST_CLASSES_DIRECTORY;
import static org.apache.maven.surefire.booter.BooterConstants.TEST_SUITE_XML_FILES;
Expand Down Expand Up @@ -162,6 +163,7 @@ File serialize( KeyValueSource sourceProperties, ProviderConfiguration providerC
properties.setProperty( RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
properties.setProperty( RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
properties.setProperty( RUN_ORDER_RANDOM_SEED, runOrderParameters.getRunOrderRandomSeed() );
properties.setProperty( SPECIFIED_RUN_ORDER, runOrderParameters.getSpecifiedRunOrder() );
}

ReporterConfiguration reporterConfiguration = providerConfiguration.getReporterConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ public void testTestArtifact()
assertEquals( cli, reloaded.getMainCliOptions() );
}

public void testRunOrderParameter()
throws IOException
{
ProviderConfiguration reloaded = getReloadedProviderConfiguration();

Assert.assertEquals( RunOrder.DEFAULT[0], reloaded.getRunOrderParameters().getRunOrder()[0] );
Assert.assertNull( reloaded.getRunOrderParameters().getRunStatisticsFile() );
Assert.assertEquals( "c1#m2,c2#m1", reloaded.getRunOrderParameters().getSpecifiedRunOrder() );
Assert.assertEquals( new Long( "65" ), reloaded.getRunOrderParameters().getRunOrderRandomSeed() );
}

public void testTestRequest()
throws IOException
{
Expand Down Expand Up @@ -275,7 +286,8 @@ private ProviderConfiguration getTestProviderConfiguration( DirectoryScannerPara
new TestRequest( getSuiteXmlFileStrings(), getTestSourceDirectory(),
new TestListResolver( USER_REQUESTED_TEST + "#aUserRequestedTestMethod" ),
RERUN_FAILING_TEST_COUNT );
RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null, new Long( "65" ),
"c1#m2,c2#m1" );
return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, reporterConfiguration,
new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new HashMap<String, String>(), TEST_TYPED,
readTestsFromInStream, cli, 0, Shutdown.DEFAULT, 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ private ProviderConfiguration getProviderConfiguration()
new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(),
new TestListResolver( "aUserRequestedTest#aUserRequestedTestMethod" ) );

RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null,
new Long( "65" ), "c1#m2,c2#m1" );
return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, reporterConfiguration,
new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new HashMap<String, String>(),
BooterDeserializerProviderConfigurationTest.TEST_TYPED, true, cli, 0, Shutdown.DEFAULT, 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
* specific language governing permissions and limitations
* under the License.
*/

import org.apache.maven.surefire.api.util.RunOrder;

import java.io.File;
import org.apache.maven.surefire.api.util.RunOrder;
import java.util.ArrayList;
import java.util.List;

import static org.apache.maven.surefire.api.util.RunOrder.ALPHABETICAL;
import static org.apache.maven.surefire.api.util.RunOrder.DEFAULT;
Expand All @@ -36,6 +39,8 @@ public final class RunOrderParameters

private final Long runOrderRandomSeed;

private final String specifiedRunOrder;

public RunOrderParameters( RunOrder[] runOrder, File runStatisticsFile )
{
this( runOrder, runStatisticsFile, null );
Expand All @@ -48,14 +53,28 @@ public RunOrderParameters( String runOrder, File runStatisticsFile )

public RunOrderParameters( String runOrder, File runStatisticsFile, Long runOrderRandomSeed )
{
this( runOrder == null ? DEFAULT : RunOrder.valueOfMulti( runOrder ), runStatisticsFile, runOrderRandomSeed );
this( runOrder, runStatisticsFile, runOrderRandomSeed, null );
}

public RunOrderParameters( RunOrder[] runOrder, File runStatisticsFile, Long runOrderRandomSeed )
{
this( runOrder, runStatisticsFile, runOrderRandomSeed, null );
}

public RunOrderParameters( RunOrder[] runOrder, File runStatisticsFile, Long runOrderRandomSeed,
String specifiedRunOrder )
{
this.runOrder = runOrder;
this.runStatisticsFile = runStatisticsFile;
this.runOrderRandomSeed = runOrderRandomSeed;
this.specifiedRunOrder = specifiedRunOrder;
}

public RunOrderParameters( String runOrder, File runStatisticsFile, Long runOrderRandomSeed,
String specifiedRunOrder )
{
this( runOrder == null ? DEFAULT : RunOrder.valueOfMulti( runOrder ), runStatisticsFile, runOrderRandomSeed,
specifiedRunOrder );
}

public static RunOrderParameters alphabetical()
Expand All @@ -77,4 +96,21 @@ public File getRunStatisticsFile()
{
return runStatisticsFile;
}

public List<ResolvedTest> resolvedSpecifiedRunOrder()
{
if ( specifiedRunOrder != null )
{
return new ArrayList<>( new TestListResolver( specifiedRunOrder ).getIncludedPatterns() );
}
else
{
return null;
}
}

public String getSpecifiedRunOrder()
{
return specifiedRunOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
import org.apache.maven.surefire.api.testset.ResolvedTest;
import org.apache.maven.surefire.api.testset.RunOrderParameters;

import java.util.ArrayList;
Expand All @@ -30,6 +31,8 @@
import java.util.List;
import java.util.Random;

import static org.apache.maven.surefire.api.testset.TestListResolver.toClassFileName;

/**
* Applies the final runorder of the tests
*
Expand All @@ -48,11 +51,14 @@ public class DefaultRunOrderCalculator

private final Random random;

private final List<ResolvedTest> specifiedRunOrder;

public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount )
{
this.runOrderParameters = runOrderParameters;
this.threadCount = threadCount;
this.runOrder = runOrderParameters.getRunOrder();
this.specifiedRunOrder = runOrderParameters.resolvedSpecifiedRunOrder();
this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
random = new Random( runOrderRandomSeed == null ? System.nanoTime() : runOrderRandomSeed );
Expand All @@ -73,9 +79,49 @@ public TestsToRun orderTestClasses( TestsToRun scannedClasses )
return new TestsToRun( new LinkedHashSet<>( result ) );
}

@Override
public Comparator<String> comparatorForTestMethods()
{
if ( RunOrder.TESTORDER.equals( runOrder[0] ) && specifiedRunOrder != null )
{
return new Comparator<String>()
{
@Override
public int compare( String o1, String o2 )
{
String[] classAndMethod1 = getClassAndMethod( o1 );
String className1 = classAndMethod1[0];
String methodName1 = classAndMethod1[1];
String[] classAndMethod2 = getClassAndMethod( o2 );
String className2 = classAndMethod2[0];
String methodName2 = classAndMethod2[1];
return testOrderComparator( className1, className2, methodName1, methodName2 );
}
};
}
else
{
return null;
}
}

private void orderTestClasses( List<Class<?>> testClasses, RunOrder runOrder )
{
if ( RunOrder.RANDOM.equals( runOrder ) )
if ( RunOrder.TESTORDER.equals( runOrder ) )
{
if ( specifiedRunOrder != null )
{
Collections.sort( testClasses, new Comparator<Class<?>>()
{
@Override
public int compare( Class<?> o1, Class<?> o2 )
{
return testOrderComparator( o1.getName(), o2.getName(), null, null );
}
} );
}
}
else if ( RunOrder.RANDOM.equals( runOrder ) )
{
Collections.shuffle( testClasses, random );
}
Expand Down Expand Up @@ -131,4 +177,42 @@ private static Comparator<Class<?>> getAlphabeticalComparator()
{
return Comparator.comparing( Class::getName );
}

public int testOrderComparator( String className1, String className2, String methodName1, String methodName2 )
{
String classFileName1 = toClassFileName( className1 );
String classFileName2 = toClassFileName( className2 );
int index1 = -1;
int index2 = -1;
if ( specifiedRunOrder != null )
{
for ( ResolvedTest filter : specifiedRunOrder )
{
if ( filter.matchAsInclusive( classFileName1, methodName1 ) )
{
index1 = specifiedRunOrder.indexOf( filter );
}
}
for ( ResolvedTest filter : specifiedRunOrder )
{
if ( filter.matchAsInclusive( classFileName2, methodName2 ) )
{
index2 = specifiedRunOrder.indexOf( filter );
}
}
}
return index1 - index2;
}

public String[] getClassAndMethod( String request )
{
String[] classAndMethod = { request, request };
if ( request.contains( "(" ) )
{
String[] nameSplit1 = request.split( "\\(" );
classAndMethod[0] = nameSplit1[1].substring( 0, nameSplit1[1].length() - 1 );
classAndMethod[1] = nameSplit1[0];
}
return classAndMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class RunOrder

public static final RunOrder FAILEDFIRST = new RunOrder( "failedfirst" );

public static final RunOrder TESTORDER = new RunOrder( "testorder" );

public static final RunOrder[] DEFAULT = new RunOrder[]{ FILESYSTEM };

/**
Expand Down Expand Up @@ -108,7 +110,8 @@ private static String createMessageForMissingRunOrder( String name )

private static RunOrder[] values()
{
return new RunOrder[]{ ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST };
return new RunOrder[]{ ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST,
TESTORDER };
}

public static String asString( RunOrder[] runOrder )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
* under the License.
*/

import java.util.Comparator;

/**
* @author Kristian Rosenvold
*/
public interface RunOrderCalculator
{
TestsToRun orderTestClasses( TestsToRun scannedClasses );

Comparator<String> comparatorForTestMethods();
}
Loading