Source Code Cross Referenced for TestOscacheFilter.java in  » Cache » OSCache » com » opensymphony » oscache » web » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » OSCache » com.opensymphony.oscache.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">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:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.