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