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 java.net.HttpURLConnection;
|
7
|
|
import java.net.URLConnection;
|
8
|
|
import org.apache.cactus.client.AbstractHttpClient;
|
9
|
|
import org.apache.cactus.util.ChainedRuntimeException;
|
10
|
|
|
11
|
|
/**
|
12
|
|
* Abstract class for Web Test Cases (i.e. HTTP connection to the server) that
|
13
|
|
* (<code>ServletTestCase</code>, <code>FilterTestCase</code>, ...) must
|
14
|
|
* extend.
|
15
|
|
*
|
16
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
17
|
|
*
|
18
|
|
* @version $Id: AbstractWebTestCase.java,v 1.6 2002/07/21 12:09:16 vmassol Exp $
|
19
|
|
*/
|
20
|
|
public abstract class AbstractWebTestCase extends AbstractTestCase {
|
21
|
|
/**
|
22
|
|
* Constructs a JUnit test case with the given name.
|
23
|
|
*
|
24
|
|
* @param theName the name of the test case
|
25
|
|
*/
|
26
|
366
|
public AbstractWebTestCase(String theName) {
|
27
|
366
|
super(theName);
|
28
|
|
;
|
29
|
|
}
|
30
|
|
/**
|
31
|
|
* Call the test case end method
|
32
|
|
*
|
33
|
|
* @param theRequest the request data that were used to open the
|
34
|
|
* connection.
|
35
|
|
* @param theConnection the <code>HttpURLConnection</code> that was used
|
36
|
|
* to open the connection to the redirection servlet. The response
|
37
|
|
* codes, headers, cookies can be checked using the get methods of
|
38
|
|
* this object.
|
39
|
|
* @exception Throwable any error that occurred when calling the end method
|
40
|
|
* for the current test case.
|
41
|
|
*/
|
42
|
173
|
protected void callEndMethod(WebRequest theRequest,
|
43
|
|
HttpURLConnection theConnection) throws Throwable {
|
44
|
173
|
Method methodToCall = null;
|
45
|
173
|
Object paramObject = null;
|
46
|
173
|
Method[] methods = this.getClass().getMethods();
|
47
|
173
|
for (int i = 0; i < methods.length; i++) {
|
48
|
12404
|
if (methods[i].getName().equals(this.getEndMethodName())) {
|
49
|
66
|
if (!methods[i].getReturnType().getName().equals("void")) {
|
50
|
1
|
AbstractWebTestCase.fail("The end method [" + methods[i].getName() +
|
51
|
|
"] should return void and not [" + methods[i].getReturnType().getName() + "]");
|
52
|
|
}
|
53
|
65
|
if (!Modifier.isPublic(methods[i].getModifiers())) {
|
54
|
0
|
AbstractWebTestCase.fail("Method [" + methods[i].getName() +
|
55
|
|
"] should be declared public");
|
56
|
|
}
|
57
|
65
|
Class[] parameters = methods[i].getParameterTypes();
|
58
|
65
|
if (parameters.length != 1) {
|
59
|
1
|
AbstractWebTestCase.fail("The end method [" + methods[i].getName() +
|
60
|
|
"] must only have a single parameter");
|
61
|
|
}
|
62
|
64
|
if (parameters[0].getName().equals("com.meterware.httpunit.WebResponse")) {
|
63
|
1
|
paramObject = this.createHttpUnitWebResponse(theConnection);
|
64
|
63
|
} else if (parameters[0].getName().equals("org.apache.cactus.WebResponse")) {
|
65
|
61
|
paramObject = new WebResponse(theRequest, theConnection);
|
66
|
2
|
} else if (parameters[0].getName().equals("java.net.HttpURLConnection")) {
|
67
|
1
|
paramObject = theConnection;
|
68
|
|
} else {
|
69
|
1
|
AbstractWebTestCase.fail("The end method [" + methods[i].getName() +
|
70
|
|
"] has a bad parameter of type [" + parameters[0].getName() + "]");
|
71
|
|
}
|
72
|
63
|
if (methodToCall != null) {
|
73
|
0
|
AbstractWebTestCase.fail("There can only be one end method per test case. Test case [" +
|
74
|
|
this.getCurrentTestMethod() + "] has two at least !");
|
75
|
|
}
|
76
|
63
|
methodToCall = methods[i];
|
77
|
|
}
|
78
|
|
}
|
79
|
170
|
if (methodToCall != null) {
|
80
|
63
|
try {
|
81
|
63
|
methodToCall.invoke(this, new java.lang.Object[] {paramObject});
|
82
|
|
} catch (InvocationTargetException e) {
|
83
|
3
|
e.fillInStackTrace();
|
84
|
3
|
throw e.getTargetException();
|
85
|
|
} catch (IllegalAccessException e) {
|
86
|
0
|
e.fillInStackTrace();
|
87
|
0
|
throw e;
|
88
|
|
}
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|
92
|
|
/**
|
93
|
|
* Create a HttpUnit <code>WebResponse</code> object by reflection (so
|
94
|
|
* that we don't need the HttpUnit jar for users who are not using
|
95
|
|
* the HttpUnit endXXX() signature).
|
96
|
|
*
|
97
|
|
* @param theConnection the HTTP connection that was used when connecting
|
98
|
|
* to the server side and which now contains the returned HTTP
|
99
|
|
* response that we will pass to HttpUnit so that it can construt
|
100
|
|
* a <code>com.meterware.httpunit.WebResponse</code> object.
|
101
|
|
* @return a HttpUnit <code>WebResponse</code> object
|
102
|
|
*/
|
103
|
1
|
private Object createHttpUnitWebResponse(HttpURLConnection theConnection) {
|
104
|
1
|
Object webResponse;
|
105
|
1
|
try {
|
106
|
1
|
Class responseClass = Class.forName("com.meterware.httpunit.WebResponse");
|
107
|
1
|
Method method = responseClass.getMethod("newResponse",
|
108
|
|
new java.lang.Class[] {URLConnection.class});
|
109
|
1
|
webResponse = method.invoke(null, new java.lang.Object[] {theConnection});
|
110
|
|
} catch (Exception e) {
|
111
|
0
|
throw new ChainedRuntimeException(
|
112
|
|
"Error calling [public static com.meterware.httpunit.WebResponse com.meterware.httpunit.WebResponse.newResponse(java.net.URLConnection) throws java.io.IOException]",
|
113
|
|
e);
|
114
|
|
}
|
115
|
1
|
return webResponse;
|
116
|
|
}
|
117
|
|
|
118
|
|
/**
|
119
|
|
* Execute the test case begin method, then connect to the server proxy
|
120
|
|
* redirector (where the test case test method is executed) and then
|
121
|
|
* executes the test case end method.
|
122
|
|
*
|
123
|
|
* @param theHttpClient the HTTP client class to use to connect to the
|
124
|
|
* proxy redirector.
|
125
|
|
* @exception Throwable any error that occurred when calling the test method
|
126
|
|
* for the current test case.
|
127
|
|
*/
|
128
|
177
|
protected void runGenericTest(AbstractHttpClient theHttpClient) throws Throwable {
|
129
|
177
|
WebRequest request = new WebRequest();
|
130
|
177
|
this.callBeginMethod(request);
|
131
|
177
|
request.addParameter("Cactus_TestClass", this.getClass().getName(), "GET");
|
132
|
177
|
request.addParameter("Cactus_TestMethod", this.getCurrentTestMethod(), "GET");
|
133
|
177
|
request.addParameter("Cactus_AutomaticSession",
|
134
|
|
request.getAutomaticSession() ? "true" : "false", "GET");
|
135
|
177
|
if (request.getURL() != null) {
|
136
|
27
|
request.getURL().saveToRequest(request);
|
137
|
|
}
|
138
|
177
|
HttpURLConnection connection = theHttpClient.doTest(request);
|
139
|
165
|
this.callEndMethod(request, connection);
|
140
|
165
|
connection.getInputStream().close();
|
141
|
|
}
|
142
|
|
|
143
|
|
}
|