Coverage Report - org.apache.maven.surefire.util.DefaultRunOrderCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultRunOrderCalculator
0%
0/36
0%
0/20
2.625
DefaultRunOrderCalculator$1
0%
0/2
N/A
2.625
DefaultRunOrderCalculator$2
0%
0/2
N/A
2.625
 
 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  0
     {
 49  0
         this.runOrderParameters = runOrderParameters;
 50  0
         this.threadCount = threadCount;
 51  0
         this.runOrder = runOrderParameters.getRunOrder();
 52  0
         this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
 53  0
     }
 54  
 
 55  
     public TestsToRun orderTestClasses( TestsToRun scannedClasses )
 56  
     {
 57  0
         List result = new ArrayList( Arrays.asList( scannedClasses.getLocatedClasses() ) );
 58  
 
 59  0
         orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
 60  0
         return new TestsToRun( result );
 61  
     }
 62  
 
 63  
     private void orderTestClasses( List testClasses, RunOrder runOrder )
 64  
     {
 65  0
         if ( RunOrder.RANDOM.equals( runOrder ) )
 66  
         {
 67  0
             Collections.shuffle( testClasses );
 68  
         }
 69  0
         else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
 70  
         {
 71  0
             RunEntryStatisticsMap runEntryStatisticsMap =
 72  
                 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
 73  0
             final List prioritized = runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( testClasses );
 74  0
             testClasses.clear();
 75  0
             testClasses.addAll( prioritized );
 76  
 
 77  0
         }
 78  0
         else if ( RunOrder.BALANCED.equals( runOrder ) )
 79  
         {
 80  0
             RunEntryStatisticsMap runEntryStatisticsMap =
 81  
                 RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
 82  0
             final List prioritized = runEntryStatisticsMap.getPrioritizedTestsClassRunTime( testClasses, threadCount );
 83  0
             testClasses.clear();
 84  0
             testClasses.addAll( prioritized );
 85  
 
 86  0
         }
 87  0
         else if ( sortOrder != null )
 88  
         {
 89  0
             Collections.sort( testClasses, sortOrder );
 90  
         }
 91  0
     }
 92  
 
 93  
     private Comparator getSortOrderComparator( RunOrder runOrder )
 94  
     {
 95  0
         if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
 96  
         {
 97  0
             return getAlphabeticalComparator();
 98  
         }
 99  0
         else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
 100  
         {
 101  0
             return getReverseAlphabeticalComparator();
 102  
         }
 103  0
         else if ( RunOrder.HOURLY.equals( runOrder ) )
 104  
         {
 105  0
             final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
 106  0
             return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
 107  
         }
 108  
         else
 109  
         {
 110  0
             return null;
 111  
         }
 112  
     }
 113  
 
 114  
     private Comparator getReverseAlphabeticalComparator()
 115  
     {
 116  0
         return new Comparator()
 117  
         {
 118  0
             public int compare( Object o1, Object o2 )
 119  
             {
 120  0
                 return ( (Class) o2 ).getName().compareTo( ( (Class) o1 ).getName() );
 121  
             }
 122  
         };
 123  
     }
 124  
 
 125  
     private Comparator getAlphabeticalComparator()
 126  
     {
 127  0
         return new Comparator()
 128  
         {
 129  0
             public int compare( Object o1, Object o2 )
 130  
             {
 131  0
                 return ( (Class) o1 ).getName().compareTo( ( (Class) o2 ).getName() );
 132  
             }
 133  
         };
 134  
     }
 135  
 
 136  
 }