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