1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.server;
|
3
|
|
import java.io.IOException;
|
4
|
|
import java.io.Writer;
|
5
|
|
import java.lang.reflect.Constructor;
|
6
|
|
import javax.servlet.ServletException;
|
7
|
|
import org.apache.cactus.AbstractTestCase;
|
8
|
|
import org.apache.cactus.HttpServiceDefinition;
|
9
|
|
import org.apache.cactus.WebTestResult;
|
10
|
|
import org.apache.cactus.util.ClassLoaderUtils;
|
11
|
|
import org.apache.commons.logging.Log;
|
12
|
|
import org.apache.commons.logging.LogFactory;
|
13
|
|
|
14
|
|
/**
|
15
|
|
* Responsible for instanciating the <code>TestCase</code> class on the server
|
16
|
|
* side, set up the implicit objects and call the test method. This class
|
17
|
|
* provides a common abstraction for all test web requests.
|
18
|
|
*
|
19
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
20
|
|
* @author <a href="mailto:ndlesiecki@apache.org">Nicholas Lesiecki</a>
|
21
|
|
*
|
22
|
|
* @version $Id: AbstractWebTestCaller.java,v 1.7 2002/07/22 12:26:04 vmassol Exp $
|
23
|
|
*/
|
24
|
|
public abstract class AbstractWebTestCaller {
|
25
|
|
/**
|
26
|
|
* Name of the attribute in the <code>application</code> scope that will
|
27
|
|
* hold the results of the test.
|
28
|
|
*/
|
29
|
|
protected static final String TEST_RESULTS = "ServletTestRedirector_TestResults";
|
30
|
|
/**
|
31
|
|
* The logger.
|
32
|
|
*/
|
33
|
|
private static Log LOGGER;
|
34
|
|
/**
|
35
|
|
* The implicit objects (which will be used to set the test case fields
|
36
|
|
* in the <code>setTesCaseFields</code> method.
|
37
|
|
*/
|
38
|
|
protected WebImplicitObjects webImplicitObjects;
|
39
|
|
/**
|
40
|
|
* @param theObjects the implicit objects coming from the redirector
|
41
|
|
*/
|
42
|
361
|
public AbstractWebTestCaller(WebImplicitObjects theObjects) {
|
43
|
361
|
super();
|
44
|
361
|
this.webImplicitObjects = theObjects;
|
45
|
|
}
|
46
|
|
/**
|
47
|
|
* Sets the implicit object in the test case class
|
48
|
|
*
|
49
|
|
* @param theTestCase the instance of the test case class on which the
|
50
|
|
* class variable (implicit objects) should be set
|
51
|
|
* @exception Exception if an errors occurs when setting the implicit
|
52
|
|
* objects
|
53
|
|
*/
|
54
|
|
protected abstract void setTestCaseFields(AbstractTestCase theTestCase) throws Exception;
|
55
|
|
|
56
|
|
/**
|
57
|
|
* @return a <code>Writer</code> object that will be used to return the
|
58
|
|
* test result to the client side.
|
59
|
|
* @exception IOException if an error occurs when retrieving the writer
|
60
|
|
*/
|
61
|
|
protected abstract Writer getResponseWriter() throws IOException;
|
62
|
|
|
63
|
|
/**
|
64
|
|
* Calls a test method. The parameters needed to call this method are found
|
65
|
|
* in the HTTP request. Save the results in the <code>application</code>
|
66
|
|
* scope so that the Get Test Result service can find them.
|
67
|
|
*
|
68
|
|
* @exception ServletException if an unexpected error occurred
|
69
|
|
*/
|
70
|
177
|
public void doTest() throws ServletException {
|
71
|
177
|
WebTestResult result = null;
|
72
|
177
|
try {
|
73
|
177
|
AbstractTestCase testInstance = this.getTestClassInstance(this.getTestClassName(),
|
74
|
|
this.getTestMethodName());
|
75
|
177
|
this.setTestCaseFields(testInstance);
|
76
|
177
|
testInstance.runBareServerTest();
|
77
|
165
|
result = new WebTestResult();
|
78
|
|
} catch (Throwable e) {
|
79
|
12
|
result = new WebTestResult(e);
|
80
|
|
}
|
81
|
177
|
AbstractWebTestCaller.LOGGER.debug("Test result : [" + result + "]");
|
82
|
177
|
this.webImplicitObjects.getServletContext().setAttribute("ServletTestRedirector_TestResults",
|
83
|
|
result);
|
84
|
177
|
AbstractWebTestCaller.LOGGER.debug("Result saved in context scope");
|
85
|
|
}
|
86
|
|
|
87
|
|
/**
|
88
|
|
* Return the last test results in the HTTP response.
|
89
|
|
*
|
90
|
|
* @exception ServletException if an unexpected error occurred
|
91
|
|
*/
|
92
|
177
|
public void doGetResults() throws ServletException {
|
93
|
177
|
WebTestResult result = (WebTestResult)(this.webImplicitObjects.getServletContext(
|
94
|
|
).getAttribute("ServletTestRedirector_TestResults"));
|
95
|
177
|
AbstractWebTestCaller.LOGGER.debug("Test Result = [" + result + "]");
|
96
|
177
|
try {
|
97
|
177
|
Writer writer = this.getResponseWriter();
|
98
|
177
|
writer.write(result.toXml());
|
99
|
177
|
writer.close();
|
100
|
|
} catch (IOException e) {
|
101
|
0
|
String message = "Error writing WebTestResult instance to output stream";
|
102
|
0
|
AbstractWebTestCaller.LOGGER.error(message, e);
|
103
|
0
|
throw new ServletException(message, e);
|
104
|
|
}
|
105
|
|
}
|
106
|
|
|
107
|
|
/**
|
108
|
|
* Run the connection test between client and server. This is just to
|
109
|
|
* ensure that configuration is set up correctly.
|
110
|
|
*
|
111
|
|
* @exception ServletException if an unexpected error occurred
|
112
|
|
*/
|
113
|
7
|
public void doRunTest() throws ServletException {
|
114
|
|
}
|
115
|
|
|
116
|
|
/**
|
117
|
|
* @return the class to test class name, extracted from the HTTP request
|
118
|
|
* @exception ServletException if the class name of the test case is missing
|
119
|
|
* from the HTTP request
|
120
|
|
*/
|
121
|
177
|
protected String getTestClassName() throws ServletException {
|
122
|
177
|
String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
|
123
|
177
|
String className = ServletUtil.getQueryStringParameter(queryString, "Cactus_TestClass");
|
124
|
177
|
if (className == null) {
|
125
|
0
|
String message = "Missing class name parameter [Cactus_TestClass] in HTTP request.";
|
126
|
0
|
AbstractWebTestCaller.LOGGER.error(message);
|
127
|
0
|
throw new ServletException(message);
|
128
|
|
}
|
129
|
177
|
AbstractWebTestCaller.LOGGER.debug("Class to call = " + className);
|
130
|
177
|
return className;
|
131
|
|
}
|
132
|
|
|
133
|
|
/**
|
134
|
|
* @return the class method to call for the current test case, extracted
|
135
|
|
* from the HTTP request
|
136
|
|
* @exception ServletException if the method name of the test case is
|
137
|
|
* missing from the HTTP request
|
138
|
|
*/
|
139
|
177
|
protected String getTestMethodName() throws ServletException {
|
140
|
177
|
String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
|
141
|
177
|
String methodName = ServletUtil.getQueryStringParameter(queryString, "Cactus_TestMethod");
|
142
|
177
|
if (methodName == null) {
|
143
|
0
|
String message = "Missing method name parameter [Cactus_TestMethod] in HTTP request.";
|
144
|
0
|
AbstractWebTestCaller.LOGGER.error(message);
|
145
|
0
|
throw new ServletException(message);
|
146
|
|
}
|
147
|
177
|
AbstractWebTestCaller.LOGGER.debug("Method to call = " + methodName);
|
148
|
177
|
return methodName;
|
149
|
|
}
|
150
|
|
|
151
|
|
/**
|
152
|
|
* @return true if the auto session flag for the Session can be found in
|
153
|
|
* the HTTP request
|
154
|
|
*/
|
155
|
177
|
protected boolean isAutoSession() {
|
156
|
177
|
String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
|
157
|
177
|
String autoSession = ServletUtil.getQueryStringParameter(queryString,
|
158
|
|
"Cactus_AutomaticSession");
|
159
|
177
|
boolean isAutomaticSession = Boolean.valueOf(autoSession).booleanValue();
|
160
|
177
|
AbstractWebTestCaller.LOGGER.debug("Auto session is " + isAutomaticSession);
|
161
|
177
|
return isAutomaticSession;
|
162
|
|
}
|
163
|
|
|
164
|
|
/**
|
165
|
|
* @param theClassName the name of the test class
|
166
|
|
* @param theTestCaseName the name of the current test case
|
167
|
|
* @return an instance of the test class to call
|
168
|
|
* @exception ServletException if the test case instance for the current
|
169
|
|
* test fails to be instanciated (for example if some
|
170
|
|
* information is missing from the HTTP request)
|
171
|
|
*/
|
172
|
177
|
protected AbstractTestCase getTestClassInstance(String theClassName,
|
173
|
|
String theTestCaseName) throws ServletException {
|
174
|
177
|
Class testClass = this.getTestClassClass(theClassName);
|
175
|
177
|
AbstractTestCase testInstance = null;
|
176
|
177
|
try {
|
177
|
177
|
Constructor constructor = testClass.getConstructor(new java.lang.Class[] {String.class});
|
178
|
177
|
testInstance = (AbstractTestCase)constructor.newInstance(new java.lang.Object[] {
|
179
|
|
theTestCaseName});
|
180
|
|
} catch (Exception e) {
|
181
|
0
|
String message = "Error instantiating class [" + theClassName + "(" + theTestCaseName + ")]";
|
182
|
0
|
AbstractWebTestCaller.LOGGER.error(message, e);
|
183
|
0
|
throw new ServletException(message, e);
|
184
|
|
}
|
185
|
177
|
return testInstance;
|
186
|
|
}
|
187
|
|
|
188
|
|
/**
|
189
|
|
* @param theClassName the name of the test class
|
190
|
|
* @return the class object the test class to call
|
191
|
|
* @exception ServletException if the class of the current test case
|
192
|
|
* cannot be loaded in memory (i.e. it is not in the
|
193
|
|
* classpath)
|
194
|
|
*/
|
195
|
177
|
protected Class getTestClassClass(String theClassName) throws ServletException {
|
196
|
177
|
Class testClass = null;
|
197
|
177
|
try {
|
198
|
177
|
testClass = ClassLoaderUtils.loadClass(theClassName, this.getClass());
|
199
|
|
} catch (Exception e) {
|
200
|
0
|
String message = "Error finding class [" + theClassName +
|
201
|
|
"] using both the Context classloader and the webapp " +
|
202
|
|
"classloader. Possible causes include:\r\n";
|
203
|
0
|
message += "\t- Your webapp does not include your test classes,\r\n";
|
204
|
0
|
message +=
|
205
|
|
"\t- The cactus.jar is not located in your WEB-INF/lib directory and your Container has not set the Context classloader to point to the webapp one";
|
206
|
0
|
AbstractWebTestCaller.LOGGER.error(message, e);
|
207
|
0
|
throw new ServletException(message, e);
|
208
|
|
}
|
209
|
177
|
return testClass;
|
210
|
|
}
|
211
|
|
|
212
|
|
/**
|
213
|
|
* Responsible for instanciating the <code>TestCase</code> class on the server
|
214
|
|
* side, set up the implicit objects and call the test method. This class
|
215
|
|
* provides a common abstraction for all test web requests.
|
216
|
|
*
|
217
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
218
|
|
* @author <a href="mailto:ndlesiecki@apache.org">Nicholas Lesiecki</a>
|
219
|
|
*
|
220
|
|
* @version $Id: AbstractWebTestCaller.java,v 1.7 2002/07/22 12:26:04 vmassol Exp $
|
221
|
|
*/
|
222
|
|
static {
|
223
|
|
/**
|
224
|
|
* The logger.
|
225
|
|
*/
|
226
|
3
|
AbstractWebTestCaller.LOGGER = LogFactory.getLog(AbstractWebTestCaller.class);
|
227
|
|
}
|
228
|
|
|
229
|
|
}
|