Source Code Cross Referenced for TestOscacheServlet.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:         * 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:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.