KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > web > TestOscacheFilter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.web;
6
7 import com.meterware.httpunit.WebConversation;
8 import com.meterware.httpunit.WebResponse;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13
14 /**
15  * Tests the caching filter distributed with the package.
16  *
17  * $Id: TestOscacheFilter.java,v 1.4 2005/10/20 12:12:35 ltorunski Exp $
18  * @version $Revision: 1.4 $
19  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
20  */

21 public final class TestOscacheFilter extends TestCase {
22     // The instance of a webconversation to invoke pages
23
WebConversation wc = null;
24     private final String JavaDoc BASE_PAGE = "filter/filterTest.jsp";
25
26     // Constants definition
27
private final String JavaDoc BASE_URL_SYSTEM_PRP = "test.web.baseURL";
28     private final String JavaDoc PARAM_1 = "abc=123";
29     private final String JavaDoc PARAM_2 = "xyz=321";
30     private final String JavaDoc SESSION_ID = "jsessionid=12345678";
31     // Constants definition to access OscacheServlet
32
private final String JavaDoc SERVLET_URL = "cacheServlet/?";
33     private final String JavaDoc FORCE_REFRESH = "forceRefresh=true&";
34
35
36     /**
37      * Constructor required by JUnit
38      * <p>
39      * @param str Test name
40      */

41     public TestOscacheFilter(String JavaDoc str) {
42         super(str);
43     }
44
45     /**
46      * Returns the test suite for the test class
47      * <p>
48      * @return Test suite for the class
49      */

50     public static Test suite() {
51         return new TestSuite(TestOscacheFilter.class);
52     }
53
54     /**
55      * Setup method called before each testXXXX of the class
56      */

57     public void setUp() {
58         // Create a web conversation to invoke our filter
59
if (wc == null) {
60             wc = new WebConversation();
61         }
62         compileJSP(constructURL(BASE_PAGE));
63     }
64
65     /**
66      * Test the OSCache filter
67      */

68     public void testOscacheFilter() {
69         String JavaDoc baseUrl = constructURL(BASE_PAGE);
70
71         // Flush the cache to avoid getting refreshed content from previous tests
72
flushCache();
73
74         // Call the page for the second time
75
String JavaDoc stringResponse = invokeURL(baseUrl, 500);
76
77         // Connect again, we should have the same content
78
String JavaDoc newResponse = invokeURL(baseUrl, 0);
79         assertTrue("new response " + newResponse + " should be the same to " + stringResponse, stringResponse.equals(newResponse));
80
81         // Try again with a session ID this time. The session ID should get filtered
82
// out of the cache key so the content should be the same
83
newResponse = invokeURL(baseUrl + "?" + SESSION_ID, 500);
84         assertTrue("new response " + newResponse + " should be the same to " + stringResponse, stringResponse.equals(newResponse));
85
86         // Connect again with extra params, the content should be different
87
newResponse = invokeURL(baseUrl + "?" + PARAM_1 + "&" + PARAM_2, 1000);
88         assertFalse("new response " + newResponse + " expected it to be different to last one.", stringResponse.equals(newResponse));
89
90         stringResponse = newResponse;
91
92         // Connect again with the parameters in a different order. We should still
93
// get the same content.
94
newResponse = invokeURL(baseUrl + "?" + PARAM_2 + "&" + PARAM_1, 0);
95         assertTrue(stringResponse.equals(newResponse));
96
97         // Connect again with the same parameters, but throw the session ID into
98
// the mix again. The content should remain the same.
99
newResponse = invokeURL(baseUrl + "?" + SESSION_ID + "&" + PARAM_1 + "&" + PARAM_2, 0);
100         assertTrue(stringResponse.equals(newResponse));
101     }
102
103     /**
104      * Test the OSCache filter with fast requests
105      */

106     public void testOSCacheFilterFast() {
107         String JavaDoc baseUrl = constructURL(BASE_PAGE);
108
109         for (int i = 0; i < 5; i++) {
110             // Flush the cache to avoid getting refreshed content from previous tests
111
flushCache();
112             // build the url
113
String JavaDoc url = baseUrl + "?i=" + i;
114             String JavaDoc response = invokeURL(url, 500);
115             for (int j = 0; j < 3; j++) {
116                 String JavaDoc newResponse = invokeURL(url, 500);
117                 assertTrue("new response " + newResponse + " should be the same to " + response, response.equals(newResponse));
118             }
119         }
120     }
121
122     /**
123      * Test the cache module using a filter and basic load
124      */

125     public void testOscacheFilterBasicForLoad() {
126         String JavaDoc baseUrl = constructURL(BASE_PAGE);
127
128         for (int i = 0; i < 5; i++) {
129             String JavaDoc stringResponse = invokeURL(baseUrl, 0);
130
131             // Check we received something slightly sane
132
assertTrue(stringResponse.indexOf("Current Time") > 0);
133         }
134     }
135
136     /**
137      * Compile a JSP page by invoking it. We compile the page first to avoid
138      * the compilation delay when testing since the time is a crucial factor
139      *
140      * @param URL The JSP url to invoke
141      */

142     private void compileJSP(String JavaDoc URL) {
143         try {
144             // Invoke the URL
145
wc.getResponse(URL);
146         } catch (Exception JavaDoc ex) {
147             ex.printStackTrace();
148             fail("Exception raised!!");
149         }
150     }
151
152     /**
153      * Flushes the cache to avoid recieving content from previous tests
154      */

155     private void flushCache() {
156         String JavaDoc flushUrl = constructURL(SERVLET_URL + FORCE_REFRESH);
157         
158         String JavaDoc stringResponse = invokeURL(flushUrl, 0);
159         
160         assertTrue("Flushing the cache failed!", stringResponse.indexOf("This is some cache content") > 0);
161     }
162
163     /**
164      * Reads the base url from the test.web.baseURL system property and
165      * append the given URL.
166      * <p>
167      * @param Url Url to append to the base.
168      * @return Complete URL
169      */

170     private String JavaDoc constructURL(String JavaDoc url) {
171         String JavaDoc base = System.getProperty(BASE_URL_SYSTEM_PRP);
172         String JavaDoc constructedUrl = null;
173
174         if (base != null) {
175             if (!base.endsWith("/")) {
176                 base = base + "/";
177             }
178
179             constructedUrl = base + url;
180         } else {
181             fail("System property test.web.baseURL needs to be set to the proper server to use.");
182         }
183
184         return constructedUrl;
185     }
186
187     /**
188      * Utility method to request a URL and then sleep some time before returning
189      * <p>
190      * @param url The URL of the page to invoke
191      * @param sleepTime The time to sleep before returning
192      * @return The text value of the reponse (HTML code)
193      */

194     private String JavaDoc invokeURL(String JavaDoc url, int sleepTime) {
195         try {
196             // Invoke the JSP and wait the specified sleepTime
197
WebResponse resp = wc.getResponse(url);
198             Thread.sleep(sleepTime);
199
200             return resp.getText();
201         } catch (Exception JavaDoc ex) {
202             ex.printStackTrace();
203             fail("Exception raised!!");
204
205             return null;
206         }
207     }
208 }
209
Popular Tags