forked from apache/maven-surefire
/
DefaultRunOrderCalculator.java
134 lines (116 loc) · 4.52 KB
/
DefaultRunOrderCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.apache.maven.surefire.api.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 org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
import org.apache.maven.surefire.api.testset.RunOrderParameters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
/**
* Applies the final runorder of the tests
*
* @author Kristian Rosenvold
*/
public class DefaultRunOrderCalculator
implements RunOrderCalculator
{
private final Comparator<Class<?>> sortOrder;
private final RunOrder[] runOrder;
private final RunOrderParameters runOrderParameters;
private final int threadCount;
private final Random random;
public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount )
{
this.runOrderParameters = runOrderParameters;
this.threadCount = threadCount;
this.runOrder = runOrderParameters.getRunOrder();
this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
random = new Random( runOrderRandomSeed == null ? System.nanoTime() : runOrderRandomSeed );
}
@Override
@SuppressWarnings( "checkstyle:magicnumber" )
public TestsToRun orderTestClasses( TestsToRun scannedClasses )
{
List<Class<?>> result = new ArrayList<>( 512 );
for ( Class<?> scannedClass : scannedClasses )
{
result.add( scannedClass );
}
orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
return new TestsToRun( new LinkedHashSet<>( result ) );
}
private void orderTestClasses( List<Class<?>> testClasses, RunOrder runOrder )
{
if ( RunOrder.RANDOM.equals( runOrder ) )
{
Collections.shuffle( testClasses, random );
}
else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
{
RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst( testClasses );
testClasses.clear();
testClasses.addAll( prioritized );
}
else if ( RunOrder.BALANCED.equals( runOrder ) )
{
RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
List<Class<?>> prioritized = stat.getPrioritizedTestsClassRunTime( testClasses, threadCount );
testClasses.clear();
testClasses.addAll( prioritized );
}
else if ( sortOrder != null )
{
testClasses.sort( sortOrder );
}
}
private static Comparator<Class<?>> getSortOrderComparator( RunOrder runOrder )
{
if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
{
return getAlphabeticalComparator();
}
else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
{
return getReverseAlphabeticalComparator();
}
else if ( RunOrder.HOURLY.equals( runOrder ) )
{
final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
}
else
{
return null;
}
}
private static Comparator<Class<?>> getReverseAlphabeticalComparator()
{
return ( o1, o2 ) -> o2.getName().compareTo( o1.getName() );
}
private static Comparator<Class<?>> getAlphabeticalComparator()
{
return Comparator.comparing( Class::getName );
}
}