001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.web;
006:
007: import com.meterware.httpunit.WebConversation;
008: import com.meterware.httpunit.WebResponse;
009:
010: import junit.framework.Test;
011: import junit.framework.TestCase;
012: import junit.framework.TestSuite;
013:
014: /**
015: * Tests the caching filter distributed with the package.
016: *
017: * $Id: TestOscacheFilter.java 435 2007-04-01 10:47:02Z larst $
018: * @version $Revision: 435 $
019: * @author <a href="mailto:chris@swebtec.com">Chris Miller</a>
020: */
021: public final class TestOscacheFilter extends TestCase {
022: // The instance of a webconversation to invoke pages
023: WebConversation wc = null;
024: private final String BASE_PAGE = "filter/filterTest.jsp";
025:
026: // Constants definition
027: private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL";
028: private final String PARAM_1 = "abc=123";
029: private final String PARAM_2 = "xyz=321";
030: private final String SESSION_ID = "jsessionid=12345678";
031: // Constants definition to access OscacheServlet
032: private final String SERVLET_URL = "cacheServlet/?";
033: private final String FORCE_REFRESH = "forceRefresh=true&";
034:
035: /**
036: * Constructor required by JUnit
037: * <p>
038: * @param str Test name
039: */
040: public TestOscacheFilter(String str) {
041: super (str);
042: }
043:
044: /**
045: * Returns the test suite for the test class
046: * <p>
047: * @return Test suite for the class
048: */
049: public static Test suite() {
050: return new TestSuite(TestOscacheFilter.class);
051: }
052:
053: /**
054: * Setup method called before each testXXXX of the class
055: */
056: public void setUp() {
057: // Create a web conversation to invoke our filter
058: if (wc == null) {
059: wc = new WebConversation();
060: }
061: compileJSP(constructURL(BASE_PAGE));
062: }
063:
064: /**
065: * Test the OSCache filter
066: */
067: public void testOscacheFilter() {
068: String baseUrl = constructURL(BASE_PAGE);
069:
070: // Flush the cache to avoid getting refreshed content from previous tests
071: flushCache();
072:
073: // Call the page for the second time
074: String stringResponse = invokeURL(baseUrl, 200);
075:
076: // Connect again, we should have the same content
077: String newResponse = invokeURL(baseUrl, 0);
078: assertTrue("new response " + newResponse
079: + " should be the same to " + stringResponse,
080: stringResponse.equals(newResponse));
081:
082: // Try again with a session ID this time. The session ID should get filtered
083: // out of the cache key so the content should be the same
084: newResponse = invokeURL(baseUrl + "?" + SESSION_ID, 200);
085: assertTrue("new response by a session id request "
086: + newResponse + " should be the same to "
087: + stringResponse, stringResponse.equals(newResponse));
088:
089: // Connect again with extra params, the content should be different
090: newResponse = invokeURL(
091: baseUrl + "?" + PARAM_1 + "&" + PARAM_2, 500);
092: assertFalse("new response " + newResponse
093: + " expected it to be different to last one.",
094: stringResponse.equals(newResponse));
095:
096: stringResponse = newResponse;
097:
098: // Connect again with the parameters in a different order. We should still
099: // get the same content.
100: newResponse = invokeURL(
101: baseUrl + "?" + PARAM_2 + "&" + PARAM_1, 0);
102: assertTrue("order of parameters shouldn't change the response",
103: stringResponse.equals(newResponse));
104:
105: // Connect again with the same parameters, but throw the session ID into
106: // the mix again. The content should remain the same.
107: newResponse = invokeURL(baseUrl + "?" + SESSION_ID + "&"
108: + PARAM_1 + "&" + PARAM_2, 0);
109: assertTrue("a session id shouldn't change the response either",
110: stringResponse.equals(newResponse));
111: }
112:
113: /**
114: * Test the OSCache filter with fast requests
115: */
116: public void testOSCacheFilterFast() {
117: String baseUrl = constructURL(BASE_PAGE);
118:
119: for (int i = 0; i < 10; i++) {
120: // Flush the cache to avoid getting refreshed content from previous tests
121: flushCache();
122: // build the url
123: String url = baseUrl + "?i=" + i;
124: String response = invokeURL(url, 100);
125: for (int j = 0; j < 5; j++) {
126: String newResponse = invokeURL(url, 100);
127: assertTrue("Fast: new response (i=" + i + ",j=" + j
128: + ") " + newResponse
129: + " should be the same to " + response,
130: response.equals(newResponse));
131: }
132: }
133: }
134:
135: /**
136: * Test the cache module using a filter and basic load
137: */
138: public void testOscacheFilterBasicForLoad() {
139: String baseUrl = constructURL(BASE_PAGE);
140:
141: for (int i = 0; i < 5; i++) {
142: String stringResponse = invokeURL(baseUrl, 0);
143:
144: // Check we received something slightly sane
145: assertTrue(stringResponse.indexOf("Current Time") > 0);
146: }
147: }
148:
149: /**
150: * Compile a JSP page by invoking it. We compile the page first to avoid
151: * the compilation delay when testing since the time is a crucial factor
152: *
153: * @param URL The JSP url to invoke
154: */
155: private void compileJSP(String URL) {
156: try {
157: // Invoke the URL
158: wc.getResponse(URL);
159: } catch (Exception ex) {
160: ex.printStackTrace();
161: fail("Exception raised!!");
162: }
163: }
164:
165: /**
166: * Flushes the cache to avoid recieving content from previous tests
167: */
168: private void flushCache() {
169: String flushUrl = constructURL(SERVLET_URL + FORCE_REFRESH);
170:
171: String stringResponse = invokeURL(flushUrl, 0);
172:
173: assertTrue("Flushing the cache failed!", stringResponse
174: .indexOf("This is some cache content") > 0);
175:
176: // avoid that flush time is equal to last update time of a new entry
177: try {
178: Thread.sleep(5);
179: } catch (InterruptedException ignore) {
180: }
181: }
182:
183: /**
184: * Reads the base url from the test.web.baseURL system property and
185: * append the given URL.
186: * <p>
187: * @param Url Url to append to the base.
188: * @return Complete URL
189: */
190: private String constructURL(String url) {
191: String base = System.getProperty(BASE_URL_SYSTEM_PRP);
192: String constructedUrl = null;
193:
194: if (base != null) {
195: if (!base.endsWith("/")) {
196: base = base + "/";
197: }
198:
199: constructedUrl = base + url;
200: } else {
201: fail("System property test.web.baseURL needs to be set to the proper server to use.");
202: }
203:
204: return constructedUrl;
205: }
206:
207: /**
208: * Utility method to request a URL and then sleep some time before returning
209: * <p>
210: * @param url The URL of the page to invoke
211: * @param sleepTime The time to sleep before returning
212: * @return The text value of the reponse (HTML code)
213: */
214: private String invokeURL(String url, int sleepTime) {
215: try {
216: // Invoke the JSP and wait the specified sleepTime
217: WebResponse resp = wc.getResponse(url);
218: Thread.sleep(sleepTime);
219:
220: return resp.getText();
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: fail("Exception raised!!");
224:
225: return null;
226: }
227: }
228:
229: }
|