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 org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMap;
23 import org.apache.maven.surefire.testset.RunOrderParameters;
24
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30
31
32
33
34
35
36 public class DefaultRunOrderCalculator
37 implements RunOrderCalculator
38 {
39 private final Comparator<Class> 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 {
49 this.runOrderParameters = runOrderParameters;
50 this.threadCount = threadCount;
51 this.runOrder = runOrderParameters.getRunOrder();
52 this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
53 }
54
55 @SuppressWarnings( "checkstyle:magicnumber" )
56 public TestsToRun orderTestClasses( TestsToRun scannedClasses )
57 {
58
59 List<Class> result = new ArrayList<Class>( 500 );
60
61 for ( Class scannedClass : scannedClasses )
62 {
63 result.add( scannedClass );
64 }
65
66 orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
67 return new TestsToRun( result );
68 }
69
70 private void orderTestClasses( List<Class> testClasses, RunOrder runOrder )
71 {
72 if ( RunOrder.RANDOM.equals( runOrder ) )
73 {
74 Collections.shuffle( testClasses );
75 }
76 else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
77 {
78 RunEntryStatisticsMap runEntryStatisticsMap =
79 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
80 final List<Class> prioritized = runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( testClasses );
81 testClasses.clear();
82 testClasses.addAll( prioritized );
83
84 }
85 else if ( RunOrder.BALANCED.equals( runOrder ) )
86 {
87 RunEntryStatisticsMap runEntryStatisticsMap =
88 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
89 final List<Class> prioritized =
90 runEntryStatisticsMap.getPrioritizedTestsClassRunTime( testClasses, threadCount );
91 testClasses.clear();
92 testClasses.addAll( prioritized );
93
94 }
95 else if ( sortOrder != null )
96 {
97 Collections.sort( testClasses, sortOrder );
98 }
99 }
100
101 private Comparator<Class> getSortOrderComparator( RunOrder runOrder )
102 {
103 if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
104 {
105 return getAlphabeticalComparator();
106 }
107 else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
108 {
109 return getReverseAlphabeticalComparator();
110 }
111 else if ( RunOrder.HOURLY.equals( runOrder ) )
112 {
113 final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
114 return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
115 }
116 else
117 {
118 return null;
119 }
120 }
121
122 private Comparator<Class> getReverseAlphabeticalComparator()
123 {
124 return new Comparator<Class>()
125 {
126 public int compare( Class o1, Class o2 )
127 {
128 return o2.getName().compareTo( o1.getName() );
129 }
130 };
131 }
132
133 private Comparator<Class> getAlphabeticalComparator()
134 {
135 return new Comparator<Class>()
136 {
137 public int compare( Class o1, Class o2 )
138 {
139 return o1.getName().compareTo( o2.getName() );
140 }
141 };
142 }
143
144 }