|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| package com.gargoylesoftware.base.testing; |
|
39 |
| |
|
40 |
| import com.gargoylesoftware.base.util.DetailedIllegalArgumentException; |
|
41 |
| import java.lang.reflect.Constructor; |
|
42 |
| import java.lang.reflect.InvocationTargetException; |
|
43 |
| import java.lang.reflect.Method; |
|
44 |
| import java.util.HashMap; |
|
45 |
| import java.util.Iterator; |
|
46 |
| import java.util.Map; |
|
47 |
| import junit.framework.TestCase; |
|
48 |
| import junit.framework.TestSuite; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| public final class OrderedTestSuite extends TestSuite { |
|
82 |
| private final Class classToTest_; |
|
83 |
| private final String[] orderedMethodNames_; |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
8
| public OrderedTestSuite( final Class clazz, final String[] methodNames ) {
|
|
94 |
8
| if( clazz == null ) {
|
|
95 |
1
| throw new NullPointerException( "clazz" );
|
|
96 |
| } |
|
97 |
7
| if( methodNames == null ) {
|
|
98 |
1
| throw new NullPointerException( "methodNames" );
|
|
99 |
| } |
|
100 |
6
| if( TestCase.class.isAssignableFrom( clazz ) == false ) {
|
|
101 |
1
| throw new DetailedIllegalArgumentException(
|
|
102 |
| "clazz", clazz, "Must be a subclass of TestCase" ); |
|
103 |
| } |
|
104 |
5
| classToTest_ = clazz;
|
|
105 |
5
| orderedMethodNames_ = methodNames;
|
|
106 |
| |
|
107 |
5
| addMethodsInOrder( getMatchingMethods() );
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
5
| private Map getMatchingMethods() {
|
|
112 |
5
| final Map matchingMethods = new HashMap( 89 );
|
|
113 |
| |
|
114 |
5
| final Method allMethods[] = classToTest_.getMethods();
|
|
115 |
| |
|
116 |
5
| int i;
|
|
117 |
5
| for( i = 0; i < allMethods.length; i++ ) {
|
|
118 |
275
| if( isMatchingMethod( allMethods[i] ) ) {
|
|
119 |
16
| matchingMethods.put( allMethods[i].getName(), allMethods[i] );
|
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
5
| return matchingMethods;
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
275
| private boolean isMatchingMethod( final Method method ) {
|
|
128 |
275
| final String methodName = method.getName();
|
|
129 |
| |
|
130 |
275
| if( method.getParameterTypes().length != 0 ) {
|
|
131 |
195
| return false;
|
|
132 |
| } |
|
133 |
| |
|
134 |
80
| if( method.getReturnType() != void.class ) {
|
|
135 |
35
| return false;
|
|
136 |
| } |
|
137 |
| |
|
138 |
45
| if( methodName.startsWith( "test" ) ) {
|
|
139 |
15
| return true;
|
|
140 |
| } |
|
141 |
| |
|
142 |
30
| int i;
|
|
143 |
30
| for( i = 0; i < orderedMethodNames_.length; i++ ) {
|
|
144 |
83
| if( methodName.equals( orderedMethodNames_[i] ) ) {
|
|
145 |
1
| return true;
|
|
146 |
| } |
|
147 |
| } |
|
148 |
29
| return false;
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
15
| private void addTest( final Method method ) {
|
|
153 |
15
| try {
|
|
154 |
15
| final Constructor constructor = classToTest_.getConstructor( new Class[]{String.class} );
|
|
155 |
15
| final TestCase test = (TestCase)constructor.newInstance( new Object[]{method.getName()} );
|
|
156 |
| |
|
157 |
15
| addTest( test );
|
|
158 |
| } |
|
159 |
| catch( final NoSuchMethodException e ) { |
|
160 |
0
| throw new TestInitializationFailedException(
|
|
161 |
| "Unable to find a constructor that takes a string", e ); |
|
162 |
| } |
|
163 |
| catch( final InstantiationException e ) { |
|
164 |
0
| throw new TestInitializationFailedException(
|
|
165 |
| "Unable to instantiate test for method " + method.getName(), e ); |
|
166 |
| } |
|
167 |
| catch( final IllegalAccessException e ) { |
|
168 |
0
| throw new TestInitializationFailedException(
|
|
169 |
| "Unable to instantiate test for method " + method.getName(), e ); |
|
170 |
| } |
|
171 |
| catch( final InvocationTargetException e ) { |
|
172 |
0
| throw new TestInitializationFailedException(
|
|
173 |
| "Unable to instantiate test for method" + method.getName(), e.getTargetException() ); |
|
174 |
| } |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
5
| private void addMethodsInOrder( final Map methodMap ) {
|
|
179 |
5
| int i;
|
|
180 |
5
| String name;
|
|
181 |
5
| Method method;
|
|
182 |
| |
|
183 |
5
| for( i = 0; i < orderedMethodNames_.length; i++ ) {
|
|
184 |
13
| name = orderedMethodNames_[i];
|
|
185 |
| |
|
186 |
13
| method = (Method)methodMap.get( name );
|
|
187 |
13
| methodMap.remove( name );
|
|
188 |
13
| if( method == null ) {
|
|
189 |
1
| throw new TestInitializationFailedException( "method not found [" + name + "]" );
|
|
190 |
| } |
|
191 |
12
| addTest( method );
|
|
192 |
| } |
|
193 |
| |
|
194 |
4
| final Iterator iterator = methodMap.values().iterator();
|
|
195 |
4
| while( iterator.hasNext() ) {
|
|
196 |
3
| method = (Method)iterator.next();
|
|
197 |
3
| addTest( method );
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| } |
|
201 |
| |