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 public TestsToRun orderTestClasses( TestsToRun scannedClasses )
56 {
57
58 List<Class> result = new ArrayList<Class>( 500 );
59
60 for ( Class scannedClass : scannedClasses )
61 {
62 result.add( scannedClass );
63 }
64
65 orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
66 return new TestsToRun( result );
67 }
68
69 private void orderTestClasses( List<Class> testClasses, RunOrder runOrder )
70 {
71 if ( RunOrder.RANDOM.equals( runOrder ) )
72 {
73 Collections.shuffle( testClasses );
74 }
75 else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
76 {
77 RunEntryStatisticsMap runEntryStatisticsMap =
78 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
79 final List<Class> prioritized = runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( testClasses );
80 testClasses.clear();
81 testClasses.addAll( prioritized );
82
83 }
84 else if ( RunOrder.BALANCED.equals( runOrder ) )
85 {
86 RunEntryStatisticsMap runEntryStatisticsMap =
87 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
88 final List<Class> prioritized = runEntryStatisticsMap.getPrioritizedTestsClassRunTime( testClasses, threadCount );
89 testClasses.clear();
90 testClasses.addAll( prioritized );
91
92 }
93 else if ( sortOrder != null )
94 {
95 Collections.sort( testClasses, sortOrder );
96 }
97 }
98
99 private Comparator<Class> getSortOrderComparator( RunOrder runOrder )
100 {
101 if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
102 {
103 return getAlphabeticalComparator();
104 }
105 else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
106 {
107 return getReverseAlphabeticalComparator();
108 }
109 else if ( RunOrder.HOURLY.equals( runOrder ) )
110 {
111 final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
112 return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
113 }
114 else
115 {
116 return null;
117 }
118 }
119
120 private Comparator<Class> getReverseAlphabeticalComparator()
121 {
122 return new Comparator<Class>()
123 {
124 public int compare( Class o1, Class o2 )
125 {
126 return o2.getName().compareTo( o1.getName() );
127 }
128 };
129 }
130
131 private Comparator<Class> getAlphabeticalComparator()
132 {
133 return new Comparator<Class>()
134 {
135 public int compare( Class o1, Class o2 )
136 {
137 return o1.getName().compareTo( o2.getName() );
138 }
139 };
140 }
141
142 }