View Javadoc

1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Applies the final runorder of the tests
33   *
34   * @author Kristian Rosenvold
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      {
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          List result = new ArrayList( Arrays.asList( scannedClasses.getLocatedClasses() ) );
58  
59          orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
60          return new TestsToRun( result );
61      }
62  
63      private void orderTestClasses( List testClasses, RunOrder runOrder )
64      {
65          if ( RunOrder.RANDOM.equals( runOrder ) )
66          {
67              Collections.shuffle( testClasses );
68          }
69          else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
70          {
71              RunEntryStatisticsMap runEntryStatisticsMap =
72                  RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
73              final List prioritized = runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( testClasses );
74              testClasses.clear();
75              testClasses.addAll( prioritized );
76  
77          }
78          else if ( RunOrder.BALANCED.equals( runOrder ) )
79          {
80              RunEntryStatisticsMap runEntryStatisticsMap =
81                  RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
82              final List prioritized = runEntryStatisticsMap.getPrioritizedTestsClassRunTime( testClasses, threadCount );
83              testClasses.clear();
84              testClasses.addAll( prioritized );
85  
86          }
87          else if ( sortOrder != null )
88          {
89              Collections.sort( testClasses, sortOrder );
90          }
91      }
92  
93      private Comparator getSortOrderComparator( RunOrder runOrder )
94      {
95          if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
96          {
97              return getAlphabeticalComparator();
98          }
99          else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
100         {
101             return getReverseAlphabeticalComparator();
102         }
103         else if ( RunOrder.HOURLY.equals( runOrder ) )
104         {
105             final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
106             return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
107         }
108         else
109         {
110             return null;
111         }
112     }
113 
114     private Comparator getReverseAlphabeticalComparator()
115     {
116         return new Comparator()
117         {
118             public int compare( Object o1, Object o2 )
119             {
120                 return ( (Class) o2 ).getName().compareTo( ( (Class) o1 ).getName() );
121             }
122         };
123     }
124 
125     private Comparator getAlphabeticalComparator()
126     {
127         return new Comparator()
128         {
129             public int compare( Object o1, Object o2 )
130             {
131                 return ( (Class) o1 ).getName().compareTo( ( (Class) o2 ).getName() );
132             }
133         };
134     }
135 
136 }