Skip to content

Commit

Permalink
Add new test RunOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Mar 21, 2022
1 parent a314f53 commit 8b7e662
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.apache.maven.surefire.api.report.ReporterConfiguration;
import org.apache.maven.surefire.api.report.ReporterFactory;
import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
import org.apache.maven.surefire.api.testset.ResolvedTest;
import org.apache.maven.surefire.api.testset.RunOrderParameters;
import org.apache.maven.surefire.api.testset.TestArtifactInfo;
import org.apache.maven.surefire.api.testset.TestListResolver;
import org.apache.maven.surefire.api.testset.TestRequest;
import org.apache.maven.surefire.api.util.DefaultDirectoryScanner;
import org.apache.maven.surefire.api.util.DefaultRunOrderCalculator;
Expand All @@ -35,6 +37,7 @@
import org.apache.maven.surefire.api.util.RunOrderCalculator;
import org.apache.maven.surefire.api.util.ScanResult;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -115,7 +118,11 @@ private int getThreadCount()
@Override
public RunOrderCalculator getRunOrderCalculator()
{
return new DefaultRunOrderCalculator( runOrderParameters, getThreadCount() );
TestListResolver testListResolver = testRequest.getTestListResolver();
List<ResolvedTest> resolvedTests = new ArrayList<>( testListResolver == null
? emptyList()
: testListResolver.getIncludedPatterns() );
return new DefaultRunOrderCalculator( runOrderParameters, getThreadCount(), resolvedTests );
}

public void setReporterFactory( ReporterFactory reporterFactory )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private boolean match( String testClassFile, String methodName )
return matchClass( testClassFile ) && matchMethod( methodName );
}

private boolean matchClass( String testClassFile )
public boolean matchClass( String testClassFile )
{
return classPattern == null || classMatcher.matchTestClassFile( testClassFile );
}
Expand Down
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 @@ -29,6 +30,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

/**
* Applies the final runorder of the tests
Expand All @@ -42,17 +44,21 @@ public class DefaultRunOrderCalculator

private final RunOrder[] runOrder;

private final List<ResolvedTest> resolvedTests;

private final RunOrderParameters runOrderParameters;

private final int threadCount;

private final Random random;

public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount )
public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount,
List<ResolvedTest> resolvedTests )
{
this.runOrderParameters = runOrderParameters;
this.threadCount = threadCount;
this.runOrder = runOrderParameters.getRunOrder();
this.resolvedTests = resolvedTests;
this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
if ( runOrderRandomSeed == null )
Expand Down Expand Up @@ -121,12 +127,25 @@ else if ( RunOrder.HOURLY.equals( runOrder ) )
final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
}
else if ( RunOrder.TEST.equals( runOrder ) )
{
return getTestOrderComparator( resolvedTests );
}
else
{
return null;
}
}

private Comparator<Class> getTestOrderComparator( List<ResolvedTest> resolvedTests )
{
return Comparator.comparing( testClass -> IntStream.range( 0, resolvedTests.size() )
.filter( i -> resolvedTests.get( i ).matchClass( testClass.getName().replace( '.', '/' ) ) )
.findFirst()
.orElse( -1 )
);
}

private Comparator<Class> getReverseAlphabeticalComparator()
{
return new Comparator<Class>()
Expand Down
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 TEST = new RunOrder( "test" );

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,
TEST };
}

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

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

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

import junit.framework.TestCase;

import static java.util.Arrays.asList;

/**
* @author Kristian Rosenvold
*/
public class RunOrderCalculatorTest
extends TestCase
{

public void testOrderTestClasses()
public void testAlphabeticalOrder()
{
getClassesToRun();
TestsToRun testsToRun = new TestsToRun( getClassesToRun() );
RunOrderCalculator runOrderCalculator = new DefaultRunOrderCalculator( RunOrderParameters.alphabetical(), 1 );
final TestsToRun testsToRun1 = runOrderCalculator.orderTestClasses( testsToRun );
assertEquals( A.class, testsToRun1.iterator().next() );
RunOrderCalculator runOrderCalculator = new DefaultRunOrderCalculator( RunOrderParameters.alphabetical(),
1, Collections.emptyList() );
final TestsToRun orderedTestsToRun = runOrderCalculator.orderTestClasses( testsToRun );
Iterator<Class<?>> iterator = orderedTestsToRun.iterator();
assertEquals( A.class, iterator.next() );
assertEquals( B.class, iterator.next() );
assertEquals( C.class, iterator.next() );
}

public void testTestOrder()
{
TestsToRun testsToRun = new TestsToRun( getClassesToRun() );
ResolvedTest testA = new ResolvedTest( A.class.getName(), null, false );
ResolvedTest testB = new ResolvedTest( B.class.getName(), null, false );
ResolvedTest testC = new ResolvedTest( C.class.getName(), null, false );

List<ResolvedTest> resolvedTests = asList( testB, testC, testA );
RunOrderParameters runOrderParameters = new RunOrderParameters(
RunOrder.valueOfMulti( "test" ), null );
RunOrderCalculator runOrderCalculator = new DefaultRunOrderCalculator( runOrderParameters,
1, resolvedTests );
final TestsToRun orderedTestsToRun = runOrderCalculator.orderTestClasses( testsToRun );
Iterator<Class<?>> iterator = orderedTestsToRun.iterator();
assertEquals( B.class, iterator.next() );
assertEquals( C.class, iterator.next() );
assertEquals( A.class, iterator.next() );
}

private Set<Class<?>> getClassesToRun()
{
Set<Class<?>> classesToRun = new LinkedHashSet<>();
classesToRun.add( B.class );
classesToRun.add( A.class );
classesToRun.add( C.class );
return classesToRun;
}

Expand All @@ -59,4 +89,9 @@ static class B
{

}

static class C
{

}
}

0 comments on commit 8b7e662

Please sign in to comment.