KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Test test the JSPs distributed with the package. It checks that the
16  * cache integration is OK.
17  *
18  * $Id: TestOscacheJsp.java,v 1.1 2005/06/17 05:06:38 dres Exp $
19  * @version $Revision: 1.1 $
20  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
21  */

22 public final class TestOscacheJsp extends TestCase {
23     // The instance of a webconversation to invoke pages
24
WebConversation wc = null;
25     private final String JavaDoc APPLICATION_SCOPE = "scope=application&";
26
27     // Constants definition
28
private final String JavaDoc BASE_URL_SYSTEM_PRP = "test.web.baseURL";
29     private final String JavaDoc FIRST_PAGE = "oscacheTest.jsp?";
30     private final String JavaDoc FORCE_CACHE_USE = "forcecacheuse=yes&";
31     private final String JavaDoc FORCE_REFRESH = "refresh=true";
32     private final String JavaDoc PAGE_SCOPE = "scope=page&";
33     private final String JavaDoc REQUEST_SCOPE = "scope=request&";
34     private final String JavaDoc SECOND_PAGE = "oscacheTestMultipleTagNoKey.jsp?";
35     private final String JavaDoc SESSION_SCOPE = "scope=session&";
36     private final int CACHE_TAG_EXPIRATION = 2000;
37     private final int HALF_CACHE_TAG_EXPIRATION = CACHE_TAG_EXPIRATION / 2;
38
39     /**
40      * Constructor required by JUnit
41      * <p>
42      * @param str Test name
43      */

44     public TestOscacheJsp(String JavaDoc str) {
45         super(str);
46     }
47
48     /**
49      * Returns the test suite for the test class
50      * <p>
51      * @return Test suite for the class
52      */

53     public static Test suite() {
54         return new TestSuite(TestOscacheJsp.class);
55     }
56
57     /**
58      * Setup method called before each testXXXX of the class
59      */

60     public void setUp() {
61         // Create a web conversation to invoke our JSP
62
if (wc == null) {
63             wc = new WebConversation();
64         }
65     }
66
67     /**
68      * Test the cache module under load
69      */

70     public void testOscacheBasicForLoad() {
71         String JavaDoc baseUrl = constructURL(FIRST_PAGE);
72
73         // Connect to the JSP using the application scope
74
String JavaDoc stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
75
76         // Assert that a page was properly generated.
77
// This does not ensure that the cache is working properly.
78
// Though, it ensures that no exception or other weird problem occured
79
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
80
81         // Invoke the JSP page containing 2 cache tag
82
baseUrl = constructURL(SECOND_PAGE);
83
84         // Connect to the JSP using the application scope
85
stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
86
87         // Assert that a page was properly generated.
88
// This does not ensure that the cache is working properly.
89
// Though, it ensures that no exception or other weird problem occured
90
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
91     }
92
93     /**
94      * Test the cache module using a JSP
95      */

96     public void testOscacheJsp() {
97         String JavaDoc baseUrl = constructURL(FIRST_PAGE);
98
99         // 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 JavaDoc stringResponse = invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION);
104
105         // Connect again, we should have the same content since it expires
106
// only each 2 seconds
107
assertTrue(stringResponse.equals(invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION)));
108
109         // Connect again, the content should be different
110
String JavaDoc newResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION + (CACHE_TAG_EXPIRATION / 4));
111         assertTrue(!stringResponse.equals(newResponse));
112         stringResponse = newResponse;
113
114         // Connect again, but request the cache content so no refresh should occur
115
assertTrue(stringResponse.equals(invokeJSP(baseUrl, FORCE_CACHE_USE, 0)));
116
117         // Connect again, the content should have changed
118
newResponse = invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION);
119         assertTrue(!stringResponse.equals(newResponse));
120         stringResponse = newResponse;
121
122         // Connect for the last time, force the cache
123
// refresh so the content should have changed
124
assertTrue(!stringResponse.equals(invokeJSP(baseUrl, FORCE_REFRESH, 0)));
125
126         // Invoke the JSP page containing 2 cache tag
127
baseUrl = constructURL(SECOND_PAGE);
128         compileJSP(baseUrl + SESSION_SCOPE);
129         stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION);
130
131         // Invoke the same page en check if it's identical
132
assertTrue(stringResponse.equals(invokeJSP(baseUrl, CACHE_TAG_EXPIRATION)));
133     }
134
135     /**
136      * Compile a JSP page by invoking it. We compile the page first to avoid
137      * the compilation delay when testing since the time is a crucial factor
138      * <p>
139      * @param URL The JSP url to invoke
140      */

141     private void compileJSP(String JavaDoc URL) {
142         try {
143             // Invoke the JSP
144
WebResponse resp = wc.getResponse(URL);
145         } catch (Exception JavaDoc ex) {
146             ex.printStackTrace();
147             fail("Exception raised!!");
148         }
149     }
150
151     /**
152      * Reads the base url from the test.web.baseURL system property and
153      * append the given URL.
154      * <p>
155      * @param Url Url to append to the base.
156      * @return Complete URL
157      */

158     private String JavaDoc constructURL(String JavaDoc Url) {
159         String JavaDoc base = System.getProperty(BASE_URL_SYSTEM_PRP);
160         String JavaDoc constructedUrl = null;
161
162         if (base != null) {
163             if (!base.endsWith("/")) {
164                 base = base + "/";
165             }
166
167             constructedUrl = base + Url;
168         } else {
169             fail("System property test.web.baseURL needs to be set to the proper server to use.");
170         }
171
172         return constructedUrl;
173     }
174
175     /**
176      * Utility method to invoke a JSP page and then sleep some time before returning
177      * <p>
178      * @param baseUrl The URL of the JSP to invoke
179      * @param sleepTime THe time to sleep before returning
180      * @return The text value of the reponse (HTML code)
181      */

182     private String JavaDoc invokeJSP(String JavaDoc baseUrl, int sleepTime) {
183         return invokeJSP(baseUrl, "", sleepTime);
184     }
185
186     /**
187      * Utility method to invoke a JSP page and then sleep some time before returning
188      * <p>
189      * @param baseUrl The URL of the JSP to invoke
190      * @param URLparam The URL parameters of the JSP 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 invokeJSP(String JavaDoc baseUrl, String JavaDoc URLparam, int sleepTime) {
195         try {
196             // Invoke the JSP and wait the specified sleepTime
197
WebResponse resp = wc.getResponse(baseUrl + APPLICATION_SCOPE + URLparam);
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