1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.constructs.web;
18
19 import com.meterware.httpunit.ClientProperties;
20 import com.meterware.httpunit.HttpUnitOptions;
21 import com.meterware.httpunit.WebConversation;
22 import com.meterware.httpunit.WebResponse;
23 import junit.framework.AssertionFailedError;
24 import junit.framework.TestCase;
25 import net.sf.ehcache.AbstractCacheTest;
26 import net.sf.ehcache.CacheManager;
27 import org.apache.commons.httpclient.HttpMethod;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.dom4j.Document;
31 import org.dom4j.io.DOMReader;
32 import org.xml.sax.SAXException;
33
34 import java.io.IOException;
35 import java.net.HttpURLConnection;
36 import java.util.List;
37
38
39
40
41
42
43
44 public abstract class AbstractWebTest extends TestCase {
45
46
47
48
49
50 public static final String CONTENT_TYPE = "CONTENT-TYPE";
51
52
53
54 public static final String CONTENT_LENGTH = "CONTENT-LENGTH";
55
56
57
58 public static final String CONTENT_ENCODING = "CONTENT-ENCODING";
59
60
61
62 public static final String CONNECTION = "CONNECTION";
63
64
65
66 public static final String SERVER = "SERVER";
67
68
69
70 public static final String DATE = "DATE";
71
72
73
74 public static final String KEEP_ALIVE = "KEEP-ALIVE";
75
76 private static final Log LOG = LogFactory.getLog(AbstractWebTest.class.getName());
77
78
79
80
81
82 private CacheManager instanceManager;
83
84
85
86
87 protected void setUp() throws Exception {
88 instanceManager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache.xml");
89 }
90
91
92
93
94 protected void tearDown() throws Exception {
95 instanceManager.shutdown();
96 }
97
98
99
100
101
102 protected static void assertContains(final String string, final String content) {
103 if (content.indexOf(string) == -1) {
104 throw new AssertionFailedError(content + "' does not contain '" + string + "'");
105 }
106 }
107
108
109
110
111
112
113
114 protected WebResponse getResponseFromAcceptGzipRequest(final String uri) throws IOException, SAXException {
115 final WebConversation conversation = createWebConversation(true);
116 final WebResponse response = conversation.getResponse(buildUrl(uri));
117 return response;
118 }
119
120
121
122
123 protected WebResponse getResponseFromNonAcceptGzipRequest(final String uri) throws IOException, SAXException {
124 final WebConversation conversation = createWebConversation(false);
125 final WebResponse response = conversation.getResponse(buildUrl(uri));
126 return response;
127 }
128
129
130
131
132 protected WebConversation createWebConversation(boolean acceptGzip) {
133 HttpUnitOptions.setExceptionsThrownOnScriptError(false);
134 HttpUnitOptions.setCheckContentLength(true);
135 HttpUnitOptions.setScriptingEnabled(false);
136 ClientProperties.getDefaultProperties().setAcceptGzip(acceptGzip);
137 final WebConversation conversation = new WebConversation();
138 return conversation;
139 }
140
141
142
143
144
145
146
147 protected String buildUrl(String uri) {
148 return "http://localhost:9080" + uri;
149 }
150
151
152
153
154
155
156
157
158
159 protected void assertResponseGood(WebResponse response, boolean fullPage) {
160 assertResponseOk(response);
161 if (fullPage) {
162 assertHeadersSane(response);
163 } else {
164 assertIncludeHeadersSane(response);
165 }
166 assertPageNotBlank(response);
167 assertPropertlyFormed(response);
168 }
169
170
171
172
173
174
175 protected void assertResponseOk(WebResponse response) {
176 assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
177 }
178
179
180
181
182
183
184
185
186
187
188
189 protected void assertHeadersSane(WebResponse response) {
190
191 String contentType = response.getHeaderField(CONTENT_TYPE);
192 assertTrue(contentType.equals("text/html")
193 || contentType.equals("text/html; charset=utf-8")
194 || contentType.equals("text/html;charset=utf-8")
195 || contentType.equals("text/html;charset=ISO-8859-1"));
196
197
198 String headerContentLength = response.getHeaderField(CONTENT_LENGTH);
199 if (headerContentLength != null) {
200 assertEquals("Content length matches header content length",
201 Integer.parseInt(headerContentLength), response.getContentLength());
202 }
203 }
204
205
206
207
208
209
210 protected void assertIncludeHeadersSane(WebResponse response) {
211 String contentType = response.getHeaderField(CONTENT_TYPE);
212
213 assertTrue(contentType == null
214 || contentType.startsWith("text/html")
215 || contentType.startsWith("text/plain"));
216
217 String contentLength = response.getHeaderField(CONTENT_LENGTH);
218
219 assertNull(response.getHeaderField(CONTENT_ENCODING));
220 }
221
222
223
224
225
226
227
228
229
230 protected void checkTimeStamps(WebResponse webResponse1, WebResponse webResponse2,
231 boolean shouldTimestampsBeEqual) throws Exception {
232 LOG.debug("Should timestamps be equal: " + shouldTimestampsBeEqual);
233 String firstGeneratedTimestamp = getTimestamp(webResponse1);
234 LOG.debug("First time stamp: " + firstGeneratedTimestamp);
235 String secondGeneratedTimestamp = getTimestamp(webResponse2);
236 LOG.debug("Second time stamp: " + secondGeneratedTimestamp);
237
238
239
240 if (shouldTimestampsBeEqual) {
241 assertEquals(firstGeneratedTimestamp, secondGeneratedTimestamp);
242 } else {
243 assertFalse(firstGeneratedTimestamp.equals(secondGeneratedTimestamp));
244 }
245 }
246
247
248
249
250
251
252 protected String getTimestamp(final WebResponse response) throws Exception {
253 String generationPrefix = "Generated at ";
254 int index = response.getText().indexOf(generationPrefix);
255 String timestamp = response.getText().substring(index, index + 36);
256 return timestamp;
257 }
258
259
260
261
262
263
264 protected void assertResponseGoodAndCached(String path, boolean fullPage) throws Exception {
265 WebResponse firstResponse = getResponseFromAcceptGzipRequest(path);
266 assertResponseGood(firstResponse, fullPage);
267 WebResponse secondResponse = getResponseFromAcceptGzipRequest(path);
268 assertResponseGood(secondResponse, fullPage);
269 checkTimeStamps(firstResponse, secondResponse, true);
270 }
271
272
273
274
275
276
277 protected void assertResponseGoodAndNotCached(String path, boolean fullPage) throws Exception {
278 WebResponse firstResponse = getResponseFromAcceptGzipRequest(path);
279 assertResponseGood(firstResponse, fullPage);
280 WebResponse secondResponse = getResponseFromAcceptGzipRequest(path);
281 assertResponseGood(secondResponse, fullPage);
282 checkTimeStamps(firstResponse, secondResponse, false);
283 }
284
285
286
287
288 protected void checkMessage(final WebResponse response, final String expected)
289 throws SAXException {
290 final Document document = createDocument(response);
291 final String message = document.valueOf("//*[@id='message']");
292 assertEquals(expected, message);
293 }
294
295
296
297
298
299 protected Document createDocument(final WebResponse response) throws SAXException {
300 response.getDOM();
301 final Document doc = new DOMReader().read(response.getDOM());
302 return doc;
303 }
304
305
306
307
308 protected void runThreads(final List executables) throws Exception {
309 final long endTime = System.currentTimeMillis() + 10000;
310 final Throwable[] errors = new Throwable[1];
311
312
313 final Thread[] threads = new Thread[executables.size()];
314 for (int i = 0; i < threads.length; i++) {
315 final AbstractWebTest.Executable executable = (AbstractWebTest.Executable) executables.get(i);
316 threads[i] = new Thread() {
317 public void run() {
318 try {
319
320 while (System.currentTimeMillis() < endTime) {
321 executable.execute();
322 }
323 } catch (Throwable t) {
324
325 errors[0] = t;
326 }
327 }
328 };
329
330 threads[i].start();
331 }
332 LOG.debug("Started " + threads.length + " threads");
333
334
335 for (int i = 0; i < threads.length; i++) {
336 threads[i].join();
337 }
338
339
340 if (errors[0] != null) {
341 throw new Exception("Test thread failed.", errors[0]);
342 }
343 }
344
345
346
347
348
349 protected interface Executable {
350
351
352
353
354
355
356 void execute() throws Exception;
357 }
358
359
360
361
362
363
364
365
366
367 protected void assertPageNotBlank(WebResponse response) {
368 String body = null;
369 try {
370 body = response.getText();
371 } catch (IOException e) {
372 LOG.fatal(e, e);
373 fail();
374 }
375 assertNotNull(body);
376 assertTrue(!body.equals(""));
377 }
378
379
380
381
382
383
384 protected void assertPropertlyFormed(WebResponse response) {
385
386
387
388
389
390
391 String text = null;
392 try {
393 text = response.getText();
394 } catch (IOException e) {
395 LOG.fatal(e.getMessage(), e);
396 fail();
397 }
398 assertTrue(text.indexOf("<html>") != -1);
399 assertTrue(text.indexOf("</html>") != -1);
400 }
401
402
403
404
405
406
407 protected void checkNullOrZeroContentLength(HttpMethod httpMethod) {
408 boolean nullContentLengthHeader = httpMethod.getResponseHeader("Content-Length") == null;
409 if (!nullContentLengthHeader) {
410 assertEquals("0", httpMethod.getResponseHeader("Content-Length").getValue());
411 }
412 }
413
414 }
415