1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus;
|
3
|
|
import java.lang.reflect.InvocationTargetException;
|
4
|
|
import java.lang.reflect.Method;
|
5
|
|
import java.lang.reflect.Modifier;
|
6
|
|
import junit.framework.TestCase;
|
7
|
|
import org.apache.cactus.util.JUnitVersionHelper;
|
8
|
|
import org.apache.commons.logging.Log;
|
9
|
|
import org.apache.commons.logging.LogFactory;
|
10
|
|
|
11
|
|
/**
|
12
|
|
* Abstract class that is a thin layer on top of JUnit and that knows about
|
13
|
|
* test cases that are executed on the server side. This class is independent
|
14
|
|
* of the protocol used to communicate from the Cactus client side and the
|
15
|
|
* Cactus server side; it can be HTTP, JMS, etc. Subclasses will define
|
16
|
|
* additional behaviour that depends on the protocol.
|
17
|
|
*
|
18
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
19
|
|
*
|
20
|
|
* @version $Id: AbstractTestCase.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $
|
21
|
|
*/
|
22
|
|
public abstract class AbstractTestCase extends TestCase {
|
23
|
|
/**
|
24
|
|
* The prefix of a test method.
|
25
|
|
*/
|
26
|
|
protected static final String TEST_METHOD_PREFIX = "test";
|
27
|
|
/**
|
28
|
|
* The prefix of a begin test method.
|
29
|
|
*/
|
30
|
|
protected static final String BEGIN_METHOD_PREFIX = "begin";
|
31
|
|
/**
|
32
|
|
* The prefix of an end test method.
|
33
|
|
*/
|
34
|
|
protected static final String END_METHOD_PREFIX = "end";
|
35
|
|
/**
|
36
|
|
* Name of properties file to initialize logging subsystem
|
37
|
|
*/
|
38
|
|
public static final String LOG_CLIENT_CONFIG = "log_client.properties";
|
39
|
|
/**
|
40
|
|
* The name of the current test method being executed. This name is valid
|
41
|
|
* both on the client side and on the server side, meaning you can call it
|
42
|
|
* from a <code>testXXX()</code>, <code>setUp()</code> or
|
43
|
|
* <code>tearDown()</code> method, as well as from <code>beginXXX()</code>
|
44
|
|
* and <code>endXXX()</code> methods.
|
45
|
|
*/
|
46
|
|
private String currentTestMethod;
|
47
|
|
/**
|
48
|
|
* The logger (only used on the client side).
|
49
|
|
*/
|
50
|
|
private Log logger;
|
51
|
|
/**
|
52
|
|
* Constructs a JUnit test case with the given name.
|
53
|
|
*
|
54
|
|
* @param theName the name of the test case
|
55
|
|
*/
|
56
|
672
|
public AbstractTestCase(String theName) {
|
57
|
672
|
super(theName);
|
58
|
|
;
|
59
|
672
|
this.setCurrentTestMethod(JUnitVersionHelper.getTestCaseName(this));
|
60
|
|
}
|
61
|
|
/**
|
62
|
|
* @return The logger used by the <code>TestCase</code> class and
|
63
|
|
* subclasses to perform logging.
|
64
|
|
*/
|
65
|
660
|
protected final Log getLogger() {
|
66
|
660
|
return this.logger;
|
67
|
|
}
|
68
|
|
|
69
|
|
/**
|
70
|
|
* @return the name of the test method to call without the
|
71
|
|
* TEST_METHOD_PREFIX prefix
|
72
|
|
*/
|
73
|
44956
|
private String getBaseMethodName() {
|
74
|
44956
|
if (!this.getCurrentTestMethod().startsWith("test")) {
|
75
|
0
|
throw new RuntimeException("bad name [" + this.getCurrentTestMethod() +
|
76
|
|
"]. It should start with [" + "test" + "].");
|
77
|
|
}
|
78
|
44956
|
return this.getCurrentTestMethod().substring("test".length());
|
79
|
|
}
|
80
|
|
|
81
|
|
/**
|
82
|
|
* @return the name of the test begin method to call that initialize the
|
83
|
|
* test by initializing the <code>WebRequest</code> object
|
84
|
|
* for the test case.
|
85
|
|
*/
|
86
|
22365
|
protected String getBeginMethodName() {
|
87
|
22365
|
return "begin" + this.getBaseMethodName();
|
88
|
|
}
|
89
|
|
|
90
|
|
/**
|
91
|
|
* @return the name of the test end method to call when the test has been
|
92
|
|
* run on the server. It can be used to verify returned headers,
|
93
|
|
* cookies, ...
|
94
|
|
*/
|
95
|
22591
|
protected String getEndMethodName() {
|
96
|
22591
|
return "end" + this.getBaseMethodName();
|
97
|
|
}
|
98
|
|
|
99
|
|
/**
|
100
|
|
* Runs the bare test sequence. This method is overridden from the
|
101
|
|
* JUnit <code>TestCase</code> class in order to prevent the latter
|
102
|
|
* to call the <code>setUp()</code> and <code>tearDown()</code> methods
|
103
|
|
* which, in our case, need to be ran in the servlet engine by the
|
104
|
|
* servlet redirector class.
|
105
|
|
*
|
106
|
|
* @exception Throwable if any exception is thrown during the test. Any
|
107
|
|
* exception will be displayed by the JUnit Test Runner
|
108
|
|
*/
|
109
|
330
|
public void runBare() throws Throwable {
|
110
|
330
|
this.logger = LogFactory.getLog(this.getClass());
|
111
|
330
|
this.getLogger().debug("------------- Test: " + this.getCurrentTestMethod());
|
112
|
330
|
try {
|
113
|
330
|
this.runTest();
|
114
|
|
} catch (Throwable t) {
|
115
|
0
|
this.logger.debug("Exception in test", t);
|
116
|
0
|
throw t;
|
117
|
|
}
|
118
|
|
}
|
119
|
|
|
120
|
|
/**
|
121
|
|
* Runs a test case. This method is overriden from the JUnit
|
122
|
|
* <code>TestCase</code> class in order to seamlessly call the
|
123
|
|
* Cactus redirection servlet.
|
124
|
|
*
|
125
|
|
* @exception Throwable any error that occurred when calling the test method
|
126
|
|
* for the current test case.
|
127
|
|
*/
|
128
|
|
protected abstract void runTest() throws Throwable;
|
129
|
|
|
130
|
|
/**
|
131
|
|
* Run the test that was specified in the constructor on the server side,
|
132
|
|
* calling <code>setUp()</code> and <code>tearDown()</code>.
|
133
|
|
*
|
134
|
|
* @exception Throwable any error that occurred when calling the test method
|
135
|
|
* for the current test case, on the server side.
|
136
|
|
*/
|
137
|
330
|
public void runBareServerTest() throws Throwable {
|
138
|
330
|
if (this.getLogger() == null) {
|
139
|
330
|
this.logger = LogFactory.getLog(this.getClass());
|
140
|
|
}
|
141
|
330
|
this.setUp();
|
142
|
330
|
try {
|
143
|
330
|
this.runServerTest();
|
144
|
|
} finally {
|
145
|
330
|
this.tearDown();
|
146
|
|
}
|
147
|
|
}
|
148
|
|
|
149
|
|
/**
|
150
|
|
* Call the test case begin method.
|
151
|
|
*
|
152
|
|
* @param theRequest the request object to pass to the begin method.
|
153
|
|
* @exception Throwable any error that occurred when calling the begin
|
154
|
|
* method for the current test case.
|
155
|
|
*/
|
156
|
342
|
protected void callBeginMethod(Request theRequest) throws Throwable {
|
157
|
342
|
Method[] methods = this.getClass().getMethods();
|
158
|
342
|
for (int i = 0; i < methods.length; i++) {
|
159
|
22365
|
if (methods[i].getName().equals(this.getBeginMethodName())) {
|
160
|
129
|
if (!methods[i].getReturnType().getName().equals("void")) {
|
161
|
1
|
AbstractTestCase.fail("The begin method [" + methods[i].getName() +
|
162
|
|
"] should return void and not [" + methods[i].getReturnType().getName() + "]");
|
163
|
|
}
|
164
|
128
|
if (!Modifier.isPublic(methods[i].getModifiers())) {
|
165
|
0
|
AbstractTestCase.fail("Method [" + methods[i].getName() + "] should be declared public");
|
166
|
|
}
|
167
|
128
|
Class[] parameters = methods[i].getParameterTypes();
|
168
|
128
|
if (parameters.length != 1) {
|
169
|
1
|
AbstractTestCase.fail("The begin method [" + methods[i].getName() +
|
170
|
|
"] must accept a single parameter derived from " + "class [" +
|
171
|
|
WebRequest.class.getName() + "], " + "but " + parameters.length +
|
172
|
|
" parameters were found");
|
173
|
127
|
} else if (!theRequest.getClass().isAssignableFrom(parameters[0])) {
|
174
|
1
|
AbstractTestCase.fail("The begin method [" + methods[i].getName() +
|
175
|
|
"] must accept a single parameter derived from " + "class [" + theRequest.getClass(
|
176
|
|
).getName() + "], " + "but found a [" + parameters[0].getName() + "] " +
|
177
|
|
"parameter instead");
|
178
|
|
}
|
179
|
126
|
try {
|
180
|
126
|
methods[i].invoke(this, new java.lang.Object[] {theRequest});
|
181
|
125
|
break;
|
182
|
|
} catch (InvocationTargetException e) {
|
183
|
1
|
e.fillInStackTrace();
|
184
|
1
|
throw e.getTargetException();
|
185
|
|
} catch (IllegalAccessException e) {
|
186
|
0
|
e.fillInStackTrace();
|
187
|
0
|
throw e;
|
188
|
|
}
|
189
|
|
}
|
190
|
|
}
|
191
|
|
}
|
192
|
|
|
193
|
|
/**
|
194
|
|
* Run the test that was specified in the constructor on the server side.
|
195
|
|
*
|
196
|
|
* @exception Throwable any error that occurred when calling the test method
|
197
|
|
* for the current test case, on the server side.
|
198
|
|
*/
|
199
|
330
|
protected void runServerTest() throws Throwable {
|
200
|
330
|
Method runMethod = null;
|
201
|
330
|
try {
|
202
|
330
|
runMethod = this.getClass().getMethod(this.getCurrentTestMethod(), new java.lang.Class[0]);
|
203
|
|
} catch (NoSuchMethodException e) {
|
204
|
0
|
AbstractTestCase.fail("Method [" + this.getCurrentTestMethod() +
|
205
|
|
"()] does not exist for class [" + this.getClass().getName() + "].");
|
206
|
|
}
|
207
|
330
|
if (runMethod != null && !Modifier.isPublic(runMethod.getModifiers())) {
|
208
|
0
|
AbstractTestCase.fail("Method [" + this.getCurrentTestMethod() + "()] should be public");
|
209
|
|
}
|
210
|
330
|
try {
|
211
|
330
|
runMethod.invoke(this, new java.lang.Class[0]);
|
212
|
|
} catch (InvocationTargetException e) {
|
213
|
20
|
e.fillInStackTrace();
|
214
|
20
|
throw e.getTargetException();
|
215
|
|
} catch (IllegalAccessException e) {
|
216
|
0
|
e.fillInStackTrace();
|
217
|
0
|
throw e;
|
218
|
|
}
|
219
|
|
}
|
220
|
|
|
221
|
|
/**
|
222
|
|
* @return the name of the current test case being executed (it corresponds
|
223
|
|
* to the name of the test method with the "test" prefix removed.
|
224
|
|
* For example, for "testSomeTestOk" would return "someTestOk".
|
225
|
|
*/
|
226
|
90991
|
protected String getCurrentTestMethod() {
|
227
|
90991
|
return this.currentTestMethod;
|
228
|
|
}
|
229
|
|
|
230
|
|
/**
|
231
|
|
* @param theCurrentTestMethod the name of the current test case.
|
232
|
|
*/
|
233
|
672
|
private void setCurrentTestMethod(String theCurrentTestMethod) {
|
234
|
672
|
this.currentTestMethod = theCurrentTestMethod;
|
235
|
|
}
|
236
|
|
|
237
|
|
}
|