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 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.LinkedHashSet;
30  import java.util.List;
31  
32  /**
33   * Applies the final runorder of the tests
34   *
35   * @author Kristian Rosenvold
36   */
37  public class DefaultRunOrderCalculator
38      implements RunOrderCalculator
39  {
40      private final Comparator<Class> sortOrder;
41  
42      private final RunOrder[] runOrder;
43  
44      private final RunOrderParameters runOrderParameters;
45  
46      private final int threadCount;
47  
48      public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount )
49      {
50          this.runOrderParameters = runOrderParameters;
51          this.threadCount = threadCount;
52          this.runOrder = runOrderParameters.getRunOrder();
53          this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
54      }
55  
56      @SuppressWarnings( "checkstyle:magicnumber" )
57      public TestsToRun orderTestClasses( TestsToRun scannedClasses )
58      {
59          List<Class<?>> result = new ArrayList<Class<?>>( 512 );
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( new LinkedHashSet<Class<?>>( 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 stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
79              List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst( testClasses );
80              testClasses.clear();
81              testClasses.addAll( prioritized );
82  
83          }
84          else if ( RunOrder.BALANCED.equals( runOrder ) )
85          {
86              RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
87              List<Class<?>> prioritized = stat.getPrioritizedTestsClassRunTime( testClasses, threadCount );
88              testClasses.clear();
89              testClasses.addAll( prioritized );
90  
91          }
92          else if ( sortOrder != null )
93          {
94              Collections.sort( testClasses, sortOrder );
95          }
96      }
97  
98      private Comparator<Class> getSortOrderComparator( RunOrder runOrder )
99      {
100         if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
101         {
102             return getAlphabeticalComparator();
103         }
104         else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
105         {
106             return getReverseAlphabeticalComparator();
107         }
108         else if ( RunOrder.HOURLY.equals( runOrder ) )
109         {
110             final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
111             return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
112         }
113         else
114         {
115             return null;
116         }
117     }
118 
119     private Comparator<Class> getReverseAlphabeticalComparator()
120     {
121         return new Comparator<Class>()
122         {
123             public int compare( Class o1, Class o2 )
124             {
125                 return o2.getName().compareTo( o1.getName() );
126             }
127         };
128     }
129 
130     private Comparator<Class> getAlphabeticalComparator()
131     {
132         return new Comparator<Class>()
133         {
134             public int compare( Class o1, Class o2 )
135             {
136                 return o1.getName().compareTo( o2.getName() );
137             }
138         };
139     }
140 }