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.List;
23  import java.util.StringTokenizer;
24  
25  /**
26   * A RunOrder specifies the order in which the tests will be run.
27   *
28   * @author Stefan Birkner
29   */
30  public class RunOrder {
31      public static final RunOrder ALPHABETICAL = new RunOrder("alphabetical");
32  
33      public static final RunOrder FILESYSTEM = new RunOrder("filesystem");
34  
35      public static final RunOrder HOURLY = new RunOrder("hourly");
36  
37      public static final RunOrder RANDOM = new RunOrder("random");
38  
39      public static final RunOrder REVERSE_ALPHABETICAL = new RunOrder("reversealphabetical");
40  
41      public static final RunOrder BALANCED = new RunOrder("balanced");
42  
43      public static final RunOrder FAILEDFIRST = new RunOrder("failedfirst");
44  
45      public static final RunOrder[] DEFAULT = new RunOrder[] {FILESYSTEM};
46  
47      /**
48       * Returns the specified RunOrder
49       *
50       * @param values The runorder string value
51       * @return An array of RunOrder objects, never null
52       */
53      public static RunOrder[] valueOfMulti(String values) {
54          List<RunOrder> result = new ArrayList<>();
55          if (values != null) {
56              StringTokenizer stringTokenizer = new StringTokenizer(values, ",");
57              while (stringTokenizer.hasMoreTokens()) {
58                  result.add(valueOf(stringTokenizer.nextToken()));
59              }
60          }
61          return result.toArray(new RunOrder[result.size()]);
62      }
63  
64      public static RunOrder valueOf(String name) {
65          if (name == null) {
66              return null;
67          } else {
68              RunOrder[] runOrders = values();
69              for (RunOrder runOrder : runOrders) {
70                  if (runOrder.matches(name)) {
71                      return runOrder;
72                  }
73              }
74  
75              String errorMessage = createMessageForMissingRunOrder(name);
76              throw new IllegalArgumentException(errorMessage);
77          }
78      }
79  
80      private static String createMessageForMissingRunOrder(String name) {
81          RunOrder[] runOrders = values();
82          StringBuilder message = new StringBuilder("There's no RunOrder with the name ");
83          message.append(name);
84          message.append(". Please use one of the following RunOrders: ");
85          for (int i = 0; i < runOrders.length; i++) {
86              if (i != 0) {
87                  message.append(", ");
88              }
89              message.append(runOrders[i]);
90          }
91          message.append('.');
92          return message.toString();
93      }
94  
95      private static RunOrder[] values() {
96          return new RunOrder[] {ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST};
97      }
98  
99      public static String asString(RunOrder[] runOrder) {
100         StringBuilder stringBuffer = new StringBuilder();
101         for (int i = 0; i < runOrder.length; i++) {
102             stringBuffer.append(runOrder[i].name);
103             if (i < (runOrder.length - 1)) {
104                 stringBuffer.append(",");
105             }
106         }
107         return stringBuffer.toString();
108     }
109 
110     private final String name;
111 
112     private RunOrder(String name) {
113         this.name = name;
114     }
115 
116     private boolean matches(String anotherName) {
117         return name.equalsIgnoreCase(anotherName);
118     }
119 
120     public String name() {
121         return name;
122     }
123 
124     @Override
125     public String toString() {
126         return name;
127     }
128 }