Source Code Cross Referenced for SessionTest.java in  » Sevlet-Container » jetty-extras » org » mortbay » jetty » 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
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » jetty extras » org.mortbay.jetty 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // ========================================================================
002:        // Copyright 2006 Mort Bay Consulting Pty. Ltd.
003:        // ------------------------------------------------------------------------
004:        // Licensed under the Apache License, Version 2.0 (the "License");
005:        // you may not use this file except in compliance with the License.
006:        // You may obtain a copy of the License at 
007:        // http://www.apache.org/licenses/LICENSE-2.0
008:        // Unless required by applicable law or agreed to in writing, software
009:        // distributed under the License is distributed on an "AS IS" BASIS,
010:        // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011:        // See the License for the specific language governing permissions and
012:        // limitations under the License.
013:        // ========================================================================
014:
015:        package org.mortbay.jetty;
016:
017:        import java.io.InputStream;
018:        import java.net.URL;
019:
020:        import org.mortbay.jetty.Handler;
021:        import org.mortbay.jetty.Server;
022:        import org.mortbay.jetty.SessionIdManager;
023:        import org.mortbay.jetty.SessionManager;
024:        import org.mortbay.jetty.handler.ContextHandlerCollection;
025:        import org.mortbay.jetty.handler.DefaultHandler;
026:        import org.mortbay.jetty.handler.HandlerCollection;
027:        import org.mortbay.jetty.handler.RequestLogHandler;
028:        import org.mortbay.jetty.nio.SelectChannelConnector;
029:        import org.mortbay.jetty.security.HashUserRealm;
030:        import org.mortbay.jetty.security.UserRealm;
031:        import org.mortbay.jetty.webapp.WebAppContext;
032:        import org.mortbay.thread.BoundedThreadPool;
033:        import org.mortbay.util.IO;
034:
035:        import junit.framework.TestCase;
036:
037:        /**
038:         * @version $Revision$
039:         */
040:        public class SessionTest extends TestCase {
041:            Server server;
042:            WebAppContext test0;
043:            WebAppContext test1;
044:            SessionManager session0;
045:            SessionManager session1;
046:            SessionIdManager ids;
047:            String url;
048:
049:            protected void setUp() throws Exception {
050:                server = new Server(0);
051:
052:                HandlerCollection handlers = new HandlerCollection();
053:                ContextHandlerCollection contexts = new ContextHandlerCollection();
054:                RequestLogHandler requestLogHandler = new RequestLogHandler();
055:                handlers.setHandlers(new Handler[] { contexts,
056:                        new DefaultHandler(), requestLogHandler });
057:                server.setHandler(handlers);
058:
059:                test0 = new WebAppContext(contexts, "../../webapps/test",
060:                        "/test0");
061:                test1 = new WebAppContext(contexts, "../../webapps/test",
062:                        "/test1");
063:
064:                HashUserRealm userRealm = new HashUserRealm();
065:                userRealm.setName("Test Realm");
066:                userRealm.setConfig("../../etc/realm.properties");
067:                server.setUserRealms(new UserRealm[] { userRealm });
068:
069:                server.start();
070:
071:                url = "http://127.0.0.1:"
072:                        + server.getConnectors()[0].getLocalPort();
073:
074:                session0 = test0.getSessionHandler().getSessionManager();
075:                session1 = test1.getSessionHandler().getSessionManager();
076:                ids = test0.getSessionHandler().getSessionManager()
077:                        .getMetaManager();
078:                assertEquals(ids, test1.getSessionHandler().getSessionManager()
079:                        .getMetaManager());
080:            }
081:
082:            protected void tearDown() throws Exception {
083:                server.stop();
084:            }
085:
086:            public void testSession() throws Exception {
087:                // no sessions to start with.
088:                testContains("/test0/session", "No Session");
089:                testContains("/test1/session", "No Session");
090:
091:                // create context in context 0
092:                String id0 = getID("/test0/session?Action=New+Session");
093:                assertTrue(id0 != null);
094:                assertEquals(id0, getID("/test0/session;jsessionid=" + id0));
095:                testContains("/test1/session;jsessionid=" + id0, "No Session");
096:
097:                // test setting value
098:                testContains("/test0/session;jsessionid=" + id0
099:                        + "?Action=Set&Name=name0&Value=value0",
100:                        "<b>name0:</b> value0<br/>");
101:                testContains("/test0/session;jsessionid=" + id0,
102:                        "<b>name0:</b> value0<br/>");
103:                testContains("/test1/session;jsessionid=" + id0, "No Session");
104:
105:                // Direct request to context 1 without session ID
106:                String id1 = getID("/test1/session;jsessionid=unknown?Action=New+Session");
107:                assertTrue(id1 != null);
108:                assertFalse(id0.equals(id1));
109:                assertEquals(id0, getID("/test0/session;jsessionid=" + id0));
110:                assertEquals(id1, getID("/test1/session;jsessionid=" + id1));
111:                testContains("/test0/session;jsessionid=" + id1, "No Session");
112:                testContains("/test1/session;jsessionid=" + id0, "No Session");
113:
114:                // test setting value
115:                testContains("/test1/session;jsessionid=" + id1
116:                        + "?Action=Set&Name=name1&Value=value1",
117:                        "<b>name1:</b> value1<br/>");
118:                testContains("/test0/session;jsessionid=" + id0,
119:                        "<b>name0:</b> value0<br/>");
120:                testContains("/test1/session;jsessionid=" + id1,
121:                        "<b>name1:</b> value1<br/>");
122:                testNotContains("/test0/session;jsessionid=" + id0,
123:                        "<b>name1:</b> value1<br/>");
124:                testNotContains("/test1/session;jsessionid=" + id1,
125:                        "<b>name0:</b> value0<br/>");
126:
127:                // Invalidate context 1
128:                testContains("/test1/session;jsessionid=" + id1
129:                        + "?Action=Invalidate", "No Session");
130:
131:                // Direct request to context 1 with session ID
132:                id1 = getID("/test1/session;jsessionid=" + id0
133:                        + "?Action=New+Session");
134:                assertTrue(id1 != null);
135:                assertTrue(id0.equals(id1));
136:                assertEquals(id0, getID("/test0/session;jsessionid=" + id0));
137:                assertEquals(id1, getID("/test1/session;jsessionid=" + id1));
138:
139:                // test setting value
140:                testContains("/test1/session;jsessionid=" + id1
141:                        + "?Action=Set&Name=name1&Value=value1",
142:                        "<b>name1:</b> value1<br/>");
143:                testContains("/test0/session;jsessionid=" + id0,
144:                        "<b>name0:</b> value0<br/>");
145:                testContains("/test1/session;jsessionid=" + id1,
146:                        "<b>name1:</b> value1<br/>");
147:                testNotContains("/test0/session;jsessionid=" + id0,
148:                        "<b>name1:</b> value1<br/>");
149:                testNotContains("/test1/session;jsessionid=" + id1,
150:                        "<b>name0:</b> value0<br/>");
151:
152:                // test dispatch get
153:                assertEquals(
154:                        id0,
155:                        getID("/test0/dispatch/forwardC/test1/session;jsessionid="
156:                                + id0));
157:                testContains(
158:                        "/test0/dispatch/forwardC/test1/session;jsessionid="
159:                                + id0, "<b>name1:</b> value1<br/>");
160:
161:                // invalidate all via dispatch
162:                testContains(
163:                        "/test0/dispatch/forwardC/test1/session;jsessionid="
164:                                + id1 + "?Action=Invalidate", "No Session");
165:                testContains("/test0/session", "No Session");
166:                testContains("/test1/session", "No Session");
167:
168:                // new sessions via dispatch
169:                id0 = getID("/test0/session?Action=New+Session");
170:                assertFalse(id0.equals(id1));
171:                id1 = getID("/test0/dispatch/forwardC/test1/session;jsessionid="
172:                        + id0 + "?Action=New+Session");
173:                assertTrue(id1 != null);
174:                assertTrue(id0.equals(id1));
175:
176:                // test values again
177:                testContains("/test0/session;jsessionid=" + id0
178:                        + "?Action=Set&Name=name0&Value=value0",
179:                        "<b>name0:</b> value0<br/>");
180:                testContains("/test1/session;jsessionid=" + id1
181:                        + "?Action=Set&Name=name1&Value=value1",
182:                        "<b>name1:</b> value1<br/>");
183:                testContains("/test0/session;jsessionid=" + id0,
184:                        "<b>name0:</b> value0<br/>");
185:                testContains("/test1/session;jsessionid=" + id1,
186:                        "<b>name1:</b> value1<br/>");
187:                testNotContains("/test0/session;jsessionid=" + id0,
188:                        "<b>name1:</b> value1<br/>");
189:                testNotContains("/test1/session;jsessionid=" + id1,
190:                        "<b>name0:</b> value0<br/>");
191:                testContains(
192:                        "/test0/dispatch/forwardC/test1/session;jsessionid="
193:                                + id0, "<b>name1:</b> value1<br/>");
194:                testContains(
195:                        "/test1/dispatch/forwardC/test0/session;jsessionid="
196:                                + id0, "<b>name0:</b> value0<br/>");
197:
198:                // direct invalidate
199:                testContains("/test1/session;jsessionid=" + id1
200:                        + "?Action=Invalidate", "No Session");
201:                testContains("/test0/session", "No Session");
202:                testContains("/test1/session", "No Session");
203:
204:            }
205:
206:            protected void testContains(String uri, String string)
207:                    throws Exception {
208:                String result = IO.toString((InputStream) new URL(url + uri)
209:                        .getContent());
210:                // System.err.println(uri+" ==> "+result); 
211:                assertTrue(result != null && result.contains(string));
212:            }
213:
214:            protected void testNotContains(String uri, String string)
215:                    throws Exception {
216:                String result = IO.toString((InputStream) new URL(url + uri)
217:                        .getContent());
218:                assertTrue(result != null && !result.contains(string));
219:            }
220:
221:            protected String getID(String uri) throws Exception {
222:                String result = IO.toString((InputStream) new URL(url + uri)
223:                        .getContent());
224:                assertTrue(result != null && result.contains("ID:</b> "));
225:                int i0 = result.indexOf("ID:</b> ");
226:                int i1 = result.indexOf("<br/>", i0);
227:                String id = result.substring(i0 + 8, i1);
228:                return id;
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.