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: }
|