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