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