1 | |
package org.apache.maven.surefire.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
import java.util.StringTokenizer; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class RunOrder |
32 | |
{ |
33 | 1 | public static final RunOrder ALPHABETICAL = new RunOrder( "alphabetical" ); |
34 | |
|
35 | 1 | public static final RunOrder FILESYSTEM = new RunOrder( "filesystem" ); |
36 | |
|
37 | 1 | public static final RunOrder HOURLY = new RunOrder( "hourly" ); |
38 | |
|
39 | 1 | public static final RunOrder RANDOM = new RunOrder( "random" ); |
40 | |
|
41 | 1 | public static final RunOrder REVERSE_ALPHABETICAL = new RunOrder( "reversealphabetical" ); |
42 | |
|
43 | 1 | public static final RunOrder BALANCED = new RunOrder( "balanced" ); |
44 | |
|
45 | 1 | public static final RunOrder FAILEDFIRST = new RunOrder( "failedfirst" ); |
46 | |
|
47 | 1 | public static final RunOrder[] DEFAULT = new RunOrder[]{ FILESYSTEM }; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public static RunOrder[] valueOfMulti( String values ) |
56 | |
{ |
57 | 5 | List result = new ArrayList(); |
58 | 5 | if ( values != null ) |
59 | |
{ |
60 | 4 | StringTokenizer stringTokenizer = new StringTokenizer( values, "," ); |
61 | 8 | while ( stringTokenizer.hasMoreTokens() ) |
62 | |
{ |
63 | 5 | result.add( valueOf( stringTokenizer.nextToken() ) ); |
64 | |
} |
65 | |
} |
66 | 4 | return (RunOrder[]) result.toArray( new RunOrder[result.size()] ); |
67 | |
} |
68 | |
|
69 | |
public static RunOrder valueOf( String name ) |
70 | |
{ |
71 | 5 | if ( name == null ) |
72 | |
{ |
73 | 0 | return null; |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 5 | RunOrder[] runOrders = values(); |
78 | 27 | for ( int i = 0; i < runOrders.length; i++ ) |
79 | |
{ |
80 | 26 | if ( runOrders[i].matches( name ) ) |
81 | |
{ |
82 | 4 | return runOrders[i]; |
83 | |
} |
84 | |
} |
85 | |
|
86 | 1 | StringBuffer errorMessage = createMessageForMissingRunOrder( name ); |
87 | 1 | throw new IllegalArgumentException( errorMessage.toString() ); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
private static StringBuffer createMessageForMissingRunOrder( String name ) |
92 | |
{ |
93 | 1 | RunOrder[] runOrders = values(); |
94 | 1 | StringBuffer message = new StringBuffer(); |
95 | 1 | message.append( "There's no RunOrder with the name " ); |
96 | 1 | message.append( name ); |
97 | 1 | message.append( ". Please use one of the following RunOrders: " ); |
98 | 8 | for ( int i = 0; i < runOrders.length; i++ ) |
99 | |
{ |
100 | 7 | if ( i != 0 ) |
101 | |
{ |
102 | 6 | message.append( ", " ); |
103 | |
} |
104 | 7 | message.append( runOrders[i] ); |
105 | |
} |
106 | 1 | message.append( "." ); |
107 | 1 | return message; |
108 | |
} |
109 | |
|
110 | |
private static RunOrder[] values() |
111 | |
{ |
112 | 6 | return new RunOrder[]{ ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST }; |
113 | |
} |
114 | |
|
115 | |
public static String asString( RunOrder[] runOrder ) |
116 | |
{ |
117 | 1 | StringBuffer stringBuffer = new StringBuffer(); |
118 | 3 | for ( int i = 0; i < runOrder.length; i++ ) |
119 | |
{ |
120 | 2 | stringBuffer.append( runOrder[i].name ); |
121 | 2 | if ( i < ( runOrder.length - 1 ) ) |
122 | |
{ |
123 | 1 | stringBuffer.append( "," ); |
124 | |
} |
125 | |
} |
126 | 1 | return stringBuffer.toString(); |
127 | |
|
128 | |
} |
129 | |
|
130 | |
private final String name; |
131 | |
|
132 | |
private RunOrder( String name ) |
133 | 7 | { |
134 | 7 | this.name = name; |
135 | 7 | } |
136 | |
|
137 | |
private boolean matches( String anotherName ) |
138 | |
{ |
139 | 26 | return name.equalsIgnoreCase( anotherName ); |
140 | |
} |
141 | |
|
142 | |
public String name() |
143 | |
{ |
144 | 0 | return name; |
145 | |
} |
146 | |
|
147 | |
public String toString() |
148 | |
{ |
149 | 7 | return name; |
150 | |
} |
151 | |
} |