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 JSPs distributed with the package. It checks that the
016: * cache integration is OK.
017: *
018: * $Id: TestOscacheJsp.java 385 2006-10-07 06:57:10Z larst $
019: * @version $Revision: 385 $
020: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
021: */
022: public final class TestOscacheJsp extends TestCase {
023: // The instance of a webconversation to invoke pages
024: WebConversation wc = null;
025: private final String APPLICATION_SCOPE = "scope=application&";
026:
027: // Constants definition
028: private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL";
029: private final String FIRST_PAGE = "oscacheTest.jsp?";
030: private final String FORCE_CACHE_USE = "forcecacheuse=yes&";
031: private final String FORCE_REFRESH = "refresh=true";
032: //private final String PAGE_SCOPE = "scope=page&";
033: //private final String REQUEST_SCOPE = "scope=request&";
034: private final String SECOND_PAGE = "oscacheTestMultipleTagNoKey.jsp?";
035: private final String SESSION_SCOPE = "scope=session&";
036: private final int CACHE_TAG_EXPIRATION = 2000;
037: private final int HALF_CACHE_TAG_EXPIRATION = CACHE_TAG_EXPIRATION / 2;
038:
039: /**
040: * Constructor required by JUnit
041: * <p>
042: * @param str Test name
043: */
044: public TestOscacheJsp(String str) {
045: super (str);
046: }
047:
048: /**
049: * Returns the test suite for the test class
050: * <p>
051: * @return Test suite for the class
052: */
053: public static Test suite() {
054: return new TestSuite(TestOscacheJsp.class);
055: }
056:
057: /**
058: * Setup method called before each testXXXX of the class
059: */
060: public void setUp() {
061: // Create a web conversation to invoke our JSP
062: if (wc == null) {
063: wc = new WebConversation();
064: }
065: }
066:
067: /**
068: * Test the cache module under load
069: */
070: public void testOscacheBasicForLoad() {
071: String baseUrl = constructURL(FIRST_PAGE);
072:
073: // Connect to the JSP using the application scope
074: String stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
075:
076: // Assert that a page was properly generated.
077: // This does not ensure that the cache is working properly.
078: // Though, it ensures that no exception or other weird problem occured
079: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
080:
081: // Invoke the JSP page containing 2 cache tag
082: baseUrl = constructURL(SECOND_PAGE);
083:
084: // Connect to the JSP using the application scope
085: stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
086:
087: // Assert that a page was properly generated.
088: // This does not ensure that the cache is working properly.
089: // Though, it ensures that no exception or other weird problem occured
090: assertTrue(stringResponse.indexOf("This is some cache content") > 0);
091: }
092:
093: /**
094: * Test the cache module using a JSP
095: */
096: public void testOscacheJsp() {
097: String baseUrl = constructURL(FIRST_PAGE);
098:
099: // Connect to a session scope to allow the JSP compilation
100: compileJSP(baseUrl + SESSION_SCOPE);
101:
102: // Connect to the JSP using the application scope
103: String stringResponse = invokeJSP(baseUrl,
104: HALF_CACHE_TAG_EXPIRATION);
105:
106: // Connect again, we should have the same content since it expires
107: // only each 2 seconds
108: assertTrue(stringResponse.equals(invokeJSP(baseUrl,
109: HALF_CACHE_TAG_EXPIRATION)));
110:
111: // Connect again, the content should be different
112: String newResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION
113: + (CACHE_TAG_EXPIRATION / 4));
114: assertTrue(!stringResponse.equals(newResponse));
115: stringResponse = newResponse;
116:
117: // Connect again, but request the cache content so no refresh should occur
118: assertTrue(stringResponse.equals(invokeJSP(baseUrl,
119: FORCE_CACHE_USE, 0)));
120:
121: // Connect again, the content should have changed
122: newResponse = invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION);
123: assertTrue(!stringResponse.equals(newResponse));
124: stringResponse = newResponse;
125:
126: // Connect for the last time, force the cache
127: // refresh so the content should have changed
128: assertTrue(!stringResponse.equals(invokeJSP(baseUrl,
129: FORCE_REFRESH, 0)));
130:
131: // Invoke the JSP page containing 2 cache tag
132: baseUrl = constructURL(SECOND_PAGE);
133: compileJSP(baseUrl + SESSION_SCOPE);
134: stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
135:
136: // Invoke the same page en check if it's identical
137: assertTrue(stringResponse.equals(invokeJSP(baseUrl,
138: CACHE_TAG_EXPIRATION)));
139: }
140:
141: /**
142: * Compile a JSP page by invoking it. We compile the page first to avoid
143: * the compilation delay when testing since the time is a crucial factor
144: * <p>
145: * @param URL The JSP url to invoke
146: */
147: private void compileJSP(String URL) {
148: try {
149: // Invoke the JSP
150: wc.getResponse(URL);
151: } catch (Exception ex) {
152: ex.printStackTrace();
153: fail("Exception raised!!");
154: }
155: }
156:
157: /**
158: * Reads the base url from the test.web.baseURL system property and
159: * append the given URL.
160: * <p>
161: * @param Url Url to append to the base.
162: * @return Complete URL
163: */
164: private String constructURL(String Url) {
165: String base = System.getProperty(BASE_URL_SYSTEM_PRP);
166: String constructedUrl = null;
167:
168: if (base != null) {
169: if (!base.endsWith("/")) {
170: base = base + "/";
171: }
172:
173: constructedUrl = base + Url;
174: } else {
175: fail("System property test.web.baseURL needs to be set to the proper server to use.");
176: }
177:
178: return constructedUrl;
179: }
180:
181: /**
182: * Utility method to invoke a JSP page and then sleep some time before returning
183: * <p>
184: * @param baseUrl The URL of the JSP to invoke
185: * @param sleepTime THe time to sleep before returning
186: * @return The text value of the reponse (HTML code)
187: */
188: private String invokeJSP(String baseUrl, int sleepTime) {
189: return invokeJSP(baseUrl, "", sleepTime);
190: }
191:
192: /**
193: * Utility method to invoke a JSP page and then sleep some time before returning
194: * <p>
195: * @param baseUrl The URL of the JSP to invoke
196: * @param URLparam The URL parameters of the JSP to invoke
197: * @param sleepTime The time to sleep before returning
198: * @return The text value of the reponse (HTML code)
199: */
200: private String invokeJSP(String baseUrl, String URLparam,
201: int sleepTime) {
202: try {
203: // Invoke the JSP and wait the specified sleepTime
204: WebResponse resp = wc.getResponse(baseUrl
205: + APPLICATION_SCOPE + URLparam);
206: Thread.sleep(sleepTime);
207:
208: return resp.getText();
209: } catch (Exception ex) {
210: ex.printStackTrace();
211: fail("Exception raised!!");
212:
213: return null;
214: }
215: }
216: }
|