KickJava   Java API By Example, From Geeks To Geeks.

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


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 osCacheServlet distributed with the package. It checks that the
16  * cache integration is OK.
17  *
18  * $Id: TestOscacheServlet.java,v 1.3 2005/10/16 18:30:25 ltorunski Exp $
19  * @version $Revision: 1.3 $
20  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
21  */

22 public final class TestOscacheServlet extends TestCase {
23
24     // The instance of a webconversation to invoke pages
25
static WebConversation wc = null;
26     private final String JavaDoc APPLICATION_SCOPE = "scope=application&";
27
28     // Constants definition
29
private final String JavaDoc BASE_URL_SYSTEM_PRP = "test.web.baseURL";
30     private final String JavaDoc FORCE_CACHE_USE = "forcecacheuse=yes&";
31     private final String JavaDoc FORCE_REFRESH = "forceRefresh=true&";
32     private final String JavaDoc KEY = "key=ServletKeyItem&";
33     private final String JavaDoc REFRESH_PERIOD = "refreshPeriod=";
34     private final String JavaDoc SERVLET_URL = "/cacheServlet/?";
35     private final int NO_REFRESH_WANTED = 2000;
36     private final int REFRESH_WANTED = 0;
37
38     /**
39      * Constructor required by JUnit
40      * <p>
41      * @param str Test name
42      */

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

52     public static Test suite() {
53         return new TestSuite(TestOscacheServlet.class);
54     }
55
56     /**
57      * This method is invoked before each testXXXX methods of the
58      * class. It set ups the variables required for each tests.
59      */

60     public void setUp() {
61         // Create a web conversation on first run
62
if (wc == null) {
63             wc = new WebConversation();
64         }
65     }
66     
67     /**
68      * Test the cache module using a servlet
69      */

70     public void testOscacheServlet() {
71         // Make a first call just to initialize the servlet
72
String JavaDoc newResponse = invokeServlet(NO_REFRESH_WANTED);
73
74         // Connect to the servlet using the application scope
75
String JavaDoc previousReponse = invokeServlet(NO_REFRESH_WANTED);
76
77         // Call again an verify that the content hasn't changed
78
newResponse = invokeServlet(NO_REFRESH_WANTED);
79         assertTrue("new response " + newResponse + " should be the same to " + previousReponse, previousReponse.equals(newResponse));
80
81         // Call again an verify that the content is updated
82
newResponse = invokeServlet(REFRESH_WANTED);
83         assertFalse("new response " + newResponse + " expected it to be different to last one.", previousReponse.equals(newResponse));
84         previousReponse = newResponse;
85
86         // Call short delay so content should be refresh, but it will not since
87
// we ask to use the item already in cache
88
newResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE);
89         assertTrue("new response " + newResponse + " should be the same to " + previousReponse, previousReponse.equals(newResponse));
90
91         // Call with long delay so the item would not need refresh, but we'll ask
92
// a refresh anyway
93
newResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH);
94         assertFalse("new response " + newResponse + " expected it to be different to last one.", previousReponse.equals(newResponse));
95
96         // Verify that the cache key and the cache entry are present in the output and
97
// that their values are correct
98
assertTrue("response '" + previousReponse + "' does not contain oscache string", previousReponse.indexOf("oscache") != -1);
99
100         assertTrue("response '" + previousReponse + "' does not contain /Test_key string", previousReponse.indexOf("/Test_key") != -1);
101     }
102
103     /**
104      * Test the cache module using a servlet and basic load
105      */

106     public void testOscacheServletBasicForLoad() {
107         // Call Servlet
108
String JavaDoc stringResponse = invokeServlet(NO_REFRESH_WANTED);
109
110         // Assert that a page was properly generated.
111
// This does not ensure that the cache is working properly.
112
// Though, it ensures that no exception or other weird problem occured
113
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
114
115         // Call again
116
stringResponse = invokeServlet(REFRESH_WANTED);
117
118         // idem comment
119
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
120
121         // Call again
122
stringResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE);
123
124         // idem comment
125
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
126
127         // Call again
128
stringResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH);
129
130         // idem comment
131
assertTrue(stringResponse.indexOf("This is some cache content") > 0);
132     }
133
134     /**
135      * Reads the base url from the test.web.baseURL system property and
136      * append the given URL.
137      * <p>
138      * @param Url Url to append to the base.
139      * @return Complete URL
140      */

141     private String JavaDoc constructURL(String JavaDoc Url) {
142         String JavaDoc base = System.getProperty(BASE_URL_SYSTEM_PRP);
143         String JavaDoc constructedUrl = null;
144
145         if (base != null) {
146             if (base.endsWith("/")) {
147                 base = base.substring(0, base.length() - 1);
148             }
149
150             constructedUrl = base + Url;
151         } else {
152             fail("System property test.web.baseURL needs to be set to the proper server to use.");
153         }
154
155         return constructedUrl;
156     }
157
158     /**
159      * Utility method to invoke a servlet
160      * <p>
161      * @param refresh The time interval telling if the item needs refresh
162      * @return The HTML page returned by the servlet
163      */

164     private String JavaDoc invokeServlet(int refresh) {
165         // Invoke the servlet
166
return invokeServlet(refresh, "");
167     }
168
169     /**
170      * Utility method to invoke a servlet
171      * <p>
172      * @param refresh The time interval telling if the item needs refresh
173      * @param URL The URL of the servlet
174      * @return The HTML page returned by the servlet
175      */

176     private String JavaDoc invokeServlet(int refresh, String JavaDoc URL) {
177         // wait 10 millis to change the time, see System.currentTimeMillis() in OscacheServlet
178
try {
179             Thread.sleep(10);
180         } catch (InterruptedException JavaDoc ignore) {
181         }
182
183         // Invoke the servlet
184
try {
185             String JavaDoc request = constructURL(SERVLET_URL) + APPLICATION_SCOPE + KEY + REFRESH_PERIOD + refresh + "&" + URL;
186             WebResponse resp = wc.getResponse(request);
187             return resp.getText();
188         } catch (Exception JavaDoc ex) {
189             ex.printStackTrace();
190             fail("Exception raised! " + ex.getMessage());
191             return "";
192         }
193     }
194 }
195
Popular Tags