View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Random;
27  
28  import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
29  import org.apache.maven.surefire.api.testset.RunOrderParameters;
30  
31  /**
32   * Applies the final runorder of the tests
33   *
34   * @author Kristian Rosenvold
35   */
36  public class DefaultRunOrderCalculator implements RunOrderCalculator {
37      private final Comparator<Class<?>> sortOrder;
38  
39      private final RunOrder[] runOrder;
40  
41      private final RunOrderParameters runOrderParameters;
42  
43      private final int threadCount;
44  
45      private final Random random;
46  
47      public DefaultRunOrderCalculator(RunOrderParameters runOrderParameters, int threadCount) {
48          this.runOrderParameters = runOrderParameters;
49          this.threadCount = threadCount;
50          this.runOrder = runOrderParameters.getRunOrder();
51          this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator(this.runOrder[0]) : null;
52          Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
53          random = new Random(runOrderRandomSeed == null ? System.nanoTime() : runOrderRandomSeed);
54      }
55  
56      @Override
57      @SuppressWarnings("checkstyle:magicnumber")
58      public TestsToRun orderTestClasses(TestsToRun scannedClasses) {
59          List<Class<?>> result = new ArrayList<>(512);
60  
61          for (Class<?> scannedClass : scannedClasses) {
62              result.add(scannedClass);
63          }
64  
65          orderTestClasses(result, runOrder.length != 0 ? runOrder[0] : null);
66          return new TestsToRun(new LinkedHashSet<>(result));
67      }
68  
69      private void orderTestClasses(List<Class<?>> testClasses, RunOrder runOrder) {
70          if (RunOrder.RANDOM.equals(runOrder)) {
71              Collections.shuffle(testClasses, random);
72          } else if (RunOrder.FAILEDFIRST.equals(runOrder)) {
73              RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile(runOrderParameters.getRunStatisticsFile());
74              List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst(testClasses);
75              testClasses.clear();
76              testClasses.addAll(prioritized);
77  
78          } else if (RunOrder.BALANCED.equals(runOrder)) {
79              RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile(runOrderParameters.getRunStatisticsFile());
80              List<Class<?>> prioritized = stat.getPrioritizedTestsClassRunTime(testClasses, threadCount);
81              testClasses.clear();
82              testClasses.addAll(prioritized);
83  
84          } else if (sortOrder != null) {
85              testClasses.sort(sortOrder);
86          }
87      }
88  
89      private static Comparator<Class<?>> getSortOrderComparator(RunOrder runOrder) {
90          if (RunOrder.ALPHABETICAL.equals(runOrder)) {
91              return getAlphabeticalComparator();
92          } else if (RunOrder.REVERSE_ALPHABETICAL.equals(runOrder)) {
93              return getReverseAlphabeticalComparator();
94          } else {
95              return null;
96          }
97      }
98  
99      private static Comparator<Class<?>> getReverseAlphabeticalComparator() {
100         return (o1, o2) -> o2.getName().compareTo(o1.getName());
101     }
102 
103     private static Comparator<Class<?>> getAlphabeticalComparator() {
104         return Comparator.comparing(Class::getName);
105     }
106 }