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              StringBuffer errorMessage = createMessageForMissingRunOrder( name );
87              throw new IllegalArgumentException( errorMessage.toString() );
88          }
89      }
90  
91      private static StringBuffer createMessageForMissingRunOrder( String name )
92      {
93          RunOrder[] runOrders = values();
94          StringBuffer message = new StringBuffer();
95          message.append( "There's no RunOrder with the name " );
96          message.append( name );
97          message.append( ". Please use one of the following RunOrders: " );
98          for ( int i = 0; i < runOrders.length; i++ )
99          {
100             if ( i != 0 )
101             {
102                 message.append( ", " );
103             }
104             message.append( runOrders[i] );
105         }
106         message.append( "." );
107         return message;
108     }
109 
110     private static RunOrder[] values()
111     {
112         return new RunOrder[]{ ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST };
113     }
114 
115     public static String asString( RunOrder[] runOrder )
116     {
117         StringBuilder stringBuffer = new StringBuilder();
118         for ( int i = 0; i < runOrder.length; i++ )
119         {
120             stringBuffer.append( runOrder[i].name );
121             if ( i < ( runOrder.length - 1 ) )
122             {
123                 stringBuffer.append( "," );
124             }
125         }
126         return stringBuffer.toString();
127 
128     }
129 
130     private final String name;
131 
132     private RunOrder( String name )
133     {
134         this.name = name;
135     }
136 
137     private boolean matches( String anotherName )
138     {
139         return name.equalsIgnoreCase( anotherName );
140     }
141 
142     public String name()
143     {
144         return name;
145     }
146 
147     public String toString()
148     {
149         return name;
150     }
151 }