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      @Override
57      @SuppressWarnings( "checkstyle:magicnumber" )
58      public TestsToRun orderTestClasses( TestsToRun scannedClasses )
59      {
60          List<Class<?>> result = new ArrayList<Class<?>>( 512 );
61  
62          for ( Class<?> scannedClass : scannedClasses )
63          {
64              result.add( scannedClass );
65          }
66  
67          orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
68          return new TestsToRun( new LinkedHashSet<Class<?>>( result ) );
69      }
70  
71      private void orderTestClasses( List<Class<?>> testClasses, RunOrder runOrder )
72      {
73          if ( RunOrder.RANDOM.equals( runOrder ) )
74          {
75              Collections.shuffle( testClasses );
76          }
77          else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
78          {
79              RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
80              List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst( testClasses );
81              testClasses.clear();
82              testClasses.addAll( prioritized );
83  
84          }
85          else if ( RunOrder.BALANCED.equals( runOrder ) )
86          {
87              RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
88              List<Class<?>> prioritized = stat.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             @Override
125             public int compare( Class o1, Class o2 )
126             {
127                 return o2.getName().compareTo( o1.getName() );
128             }
129         };
130     }
131 
132     private Comparator<Class> getAlphabeticalComparator()
133     {
134         return new Comparator<Class>()
135         {
136             @Override
137             public int compare( Class o1, Class o2 )
138             {
139                 return o1.getName().compareTo( o2.getName() );
140             }
141         };
142     }
143 }