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(); // guaranteed non-empty
82          StringBuilder message = new StringBuilder("There's no RunOrder with the name ");
83          message.append(name);
84          message.append(". Use one of the following RunOrders: ");
85          message.append(runOrders[0]);
86          for (int i = 1; i < runOrders.length; i++) {
87              message.append(", ");
88              message.append(runOrders[i]);
89          }
90          message.append('.');
91          return message.toString();
92      }
93  
94      private static RunOrder[] values() {
95          return new RunOrder[] {ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST};
96      }
97  
98      public static String asString(RunOrder[] runOrder) {
99          StringBuilder stringBuffer = new StringBuilder();
100         for (int i = 0; i < runOrder.length - 1; i++) {
101             stringBuffer.append(runOrder[i].name);
102             stringBuffer.append(",");
103         }
104         stringBuffer.append(runOrder[runOrder.length - 1].name);
105         return stringBuffer.toString();
106     }
107 
108     private final String name;
109 
110     private RunOrder(String name) {
111         this.name = name;
112     }
113 
114     private boolean matches(String anotherName) {
115         return name.equalsIgnoreCase(anotherName);
116     }
117 
118     public String name() {
119         return name;
120     }
121 
122     @Override
123     public String toString() {
124         return name;
125     }
126 }