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: * Test test the osCacheServlet distributed with the package. It checks that the
016: * cache integration is OK.
017: *
018: * $Id: TestOscacheServlet.java 314 2005-10-16 18:30:25Z ltorunski $
019: * @version $Revision: 314 $
020: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
021: */
022: public final class TestOscacheServlet extends TestCase {
023:
024: // The instance of a webconversation to invoke pages
025: static WebConversation wc = null;
026: private final String APPLICATION_SCOPE = "scope=application&";
027:
028: // Constants definition
029: private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL";
030: private final String FORCE_CACHE_USE = "forcecacheuse=yes&";
031: private final String FORCE_REFRESH = "forceRefresh=true&";
032: private final String KEY = "key=ServletKeyItem&";
033: private final String REFRESH_PERIOD = "refreshPeriod=";
034: private final String SERVLET_URL = "/cacheServlet/?";
035: private final int NO_REFRESH_WANTED = 2000;
036: private final int REFRESH_WANTED = 0;
037:
038: /**
039: * Constructor required by JUnit
040: * <p>
041: * @param str Test name
042: */
043: public TestOscacheServlet(String str) {
044: super (str);
045: }
046:
047: /**
048: * Returns the test suite for the test class
049: * <p>
050: * @return Test suite for the class
051: */
052: public static Test suite() {
053: return new TestSuite(TestOscacheServlet.class);
054: }
055:
056: /**
057: * This method is invoked before each testXXXX methods of the
058: * class. It set ups the variables required for each tests.
059: */
060: public void setUp() {
061: // Create a web conversation on first run
062: if (wc == null) {
063: wc = new WebConversation();
064: }
065: }
066:
067: /**
068: * Test the cache module using a servlet
069: */
070: public void testOscacheServlet() {
071: // Make a first call just to initialize the servlet
072: String newResponse = invokeServlet(NO_REFRESH_WANTED);
073:
074: // Connect to the servlet using the application scope
075: String previousReponse = invokeServlet(NO_REFRESH_WANTED);
076:
077: // Call again an verify that the content hasn't changed
078: newResponse = invokeServlet(NO_REFRESH_WANTED);
079: assertTrue("new response " + newResponse
080: + " should be the same to " + previousReponse,
081: previousReponse.equals(newResponse));
082:
083: // Call again an verify that the content is updated
084: newResponse = invokeServlet(REFRESH_WANTED);
085: assertFalse("new response " + newResponse
086: + " expected it to be different to last one.",
087: previousReponse.equals(newResponse));
088: previousReponse = newResponse;
089:
090: // Call short delay so content should be refresh, but it will not since
091: // we ask to use the item already in cache
092: newResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE);
093: assertTrue("new response " + newResponse
094: + " should be the same to " + previousReponse,
095: previousReponse.equals(newResponse));
096:
097: // Call with long delay so the item would not need refresh, but we'll ask
098: // a refresh anyway
099: newResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH);
100: assertFalse("new response " + newResponse
101: + " expected it to be different to last one.",
102: previousReponse.equals(newResponse));
103:
104: // Verify that the cache key and the cache entry are present in the output and
105: // that their values are correct
106: assertTrue("response '" + previousReponse
107: + "' does not contain oscache string", previousReponse
108: .indexOf("oscache") != -1);
109:
110: assertTrue("response '" + previousReponse
111: + "' does not contain /Test_key string",
112: previousReponse.indexOf("/Test_key") != -1);
113: }
114:
115: /**
116: * Test the cache module using a servlet and basic load
117: */
118: public void testOscacheServletBasicForLoad() {
119: // Call Servlet
120: String stringResponse = invokeServlet(NO_REFRESH_WANTED);
121:
122: // Assert that a page was properly generated.
123: // This does not ensure that the cache is working properly.
124: // Though, it ensures that no exception or other weird problem occured
125: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
126:
127: // Call again
128: stringResponse = invokeServlet(REFRESH_WANTED);
129:
130: // idem comment
131: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
132:
133: // Call again
134: stringResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE);
135:
136: // idem comment
137: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
138:
139: // Call again
140: stringResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH);
141:
142: // idem comment
143: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
144: }
145:
146: /**
147: * Reads the base url from the test.web.baseURL system property and
148: * append the given URL.
149: * <p>
150: * @param Url Url to append to the base.
151: * @return Complete URL
152: */
153: private String constructURL(String Url) {
154: String base = System.getProperty(BASE_URL_SYSTEM_PRP);
155: String constructedUrl = null;
156:
157: if (base != null) {
158: if (base.endsWith("/")) {
159: base = base.substring(0, base.length() - 1);
160: }
161:
162: constructedUrl = base + Url;
163: } else {
164: fail("System property test.web.baseURL needs to be set to the proper server to use.");
165: }
166:
167: return constructedUrl;
168: }
169:
170: /**
171: * Utility method to invoke a servlet
172: * <p>
173: * @param refresh The time interval telling if the item needs refresh
174: * @return The HTML page returned by the servlet
175: */
176: private String invokeServlet(int refresh) {
177: // Invoke the servlet
178: return invokeServlet(refresh, "");
179: }
180:
181: /**
182: * Utility method to invoke a servlet
183: * <p>
184: * @param refresh The time interval telling if the item needs refresh
185: * @param URL The URL of the servlet
186: * @return The HTML page returned by the servlet
187: */
188: private String invokeServlet(int refresh, String URL) {
189: // wait 10 millis to change the time, see System.currentTimeMillis() in OscacheServlet
190: try {
191: Thread.sleep(10);
192: } catch (InterruptedException ignore) {
193: }
194:
195: // Invoke the servlet
196: try {
197: String request = constructURL(SERVLET_URL)
198: + APPLICATION_SCOPE + KEY + REFRESH_PERIOD
199: + refresh + "&" + URL;
200: WebResponse resp = wc.getResponse(request);
201: return resp.getText();
202: } catch (Exception ex) {
203: ex.printStackTrace();
204: fail("Exception raised! " + ex.getMessage());
205: return "";
206: }
207: }
208: }
|