001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet.unit;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.cactus.HttpSessionCookie;
026: import org.apache.cactus.ServletTestCase;
027: import org.apache.cactus.WebRequest;
028: import org.apache.cactus.WebResponse;
029:
030: /**
031: * Tests that manipulates the HTTP Session.
032: *
033: * @version $Id: TestHttpSession.java 239131 2005-01-29 15:49:49Z vmassol $
034: */
035: public class TestHttpSession extends ServletTestCase {
036: /**
037: * Save the session cookie for the testDependentTestUsingSession tests.
038: * The idea is to share the same session object on the server side.
039: */
040: private HttpSessionCookie sessionCookie;
041:
042: /**
043: * Link this test to another test. This is to cleanly be able to pass a
044: * parameter to another test (in our case the session cookie).
045: */
046: private TestHttpSession dependentTest;
047:
048: /**
049: * @see ServletTestCase#ServletTestCase(String)
050: */
051: public TestHttpSession(String theName) {
052: super (theName);
053: }
054:
055: /**
056: * @param theName test name
057: * @param theDependentest a dependent test to link to this test
058: */
059: public TestHttpSession(String theName,
060: TestHttpSession theDependentest) {
061: super (theName);
062: this .dependentTest = theDependentest;
063: }
064:
065: /**
066: * We order the tests so that we are sure that testDependentTestUsingSession
067: * is run before testDependentTestUsingSession2. This is because we wish to
068: * verify it is possible to share session data between 2 tests.
069: *
070: * @return the ordered suite to execute
071: */
072: public static Test suite() {
073: TestSuite suite = new TestSuite();
074: suite.addTest(new TestHttpSession(
075: "testNoAutomaticSessionCreation"));
076: suite.addTest(new TestHttpSession("testVerifyJsessionid"));
077: suite.addTest(new TestHttpSession("testCreateSessionCookie"));
078:
079: TestHttpSession test = new TestHttpSession(
080: "testDependentTestUsingSession");
081: suite.addTest(test);
082: suite.addTest(new TestHttpSession(
083: "testDependentTestUsingSession2", test));
084:
085: return suite;
086: }
087:
088: /**
089: * Verify that it is possible to ask for no automatic session creation in
090: * the <code>beginXXX()</code> method.
091: *
092: * @param theRequest the request object that serves to initialize the
093: * HTTP connection to the server redirector.
094: */
095: public void beginNoAutomaticSessionCreation(WebRequest theRequest) {
096: theRequest.setAutomaticSession(false);
097: }
098:
099: /**
100: * Verify that it is possible to ask for no automatic session creation in
101: * the <code>beginXXX()</code> method.
102: */
103: public void testNoAutomaticSessionCreation() {
104: assertNull(
105: "A valid session has been found when no session should "
106: + "exist", session);
107: }
108:
109: //-------------------------------------------------------------------------
110:
111: /**
112: * Verify that we can get hold of the jsessionid cookie returned by the
113: * server.
114: */
115: public void testVerifyJsessionid() {
116: // By default, Cactus will create an HTTP session.
117: }
118:
119: /**
120: * Verify that we can get hold of the jsessionid cookie returned by the
121: * server.
122: *
123: * @param theResponse the response from the server side.
124: */
125: public void endVerifyJsessionid(WebResponse theResponse) {
126: assertNotNull(theResponse.getCookieIgnoreCase("jsessionid"));
127: }
128:
129: //-------------------------------------------------------------------------
130:
131: /**
132: * Verify that Cactus can provide us with a real HTTP session cookie.
133: *
134: * @param theRequest the request object that serves to initialize the
135: * HTTP connection to the server redirector.
136: */
137: public void beginCreateSessionCookie(WebRequest theRequest) {
138: HttpSessionCookie sessionCookie = theRequest.getSessionCookie();
139: assertNotNull("Session cookie should not be null",
140: sessionCookie);
141: theRequest.addCookie(sessionCookie);
142: }
143:
144: /**
145: * Verify that Cactus can provide us with a real HTTP session cookie.
146: */
147: public void testCreateSessionCookie() {
148: assertTrue("A session should have been created prior to "
149: + "this request", !session.isNew());
150: }
151:
152: //-------------------------------------------------------------------------
153:
154: /**
155: * Verify that it is possible to share the HTTP Session between 2 tests.
156: * Please note that this is *NOT* recommended at all as unit tests must
157: * be independent one from another.
158: *
159: * @param theRequest the request object that serves to initialize the
160: * HTTP connection to the server redirector.
161: */
162: public void beginDependentTestUsingSession(WebRequest theRequest) {
163: this .sessionCookie = theRequest.getSessionCookie();
164: assertNotNull("Session cookie should not be null",
165: sessionCookie);
166: theRequest.addCookie(sessionCookie);
167: }
168:
169: /**
170: * Verify that it is possible to share the HTTP Session between 2 tests.
171: * Please note that this is *NOT* recommended at all as unit tests must
172: * be independent one from another.
173: */
174: public void testDependentTestUsingSession() {
175: session.setAttribute("dependentTestId", "dependentTestValue");
176: }
177:
178: /**
179: * Verify that it is possible to share the HTTP Session between 2 tests.
180: * Please note that this is *NOT* recommended at all as unit tests must
181: * be independent one from another.
182: *
183: * @param theRequest the request object that serves to initialize the
184: * HTTP connection to the server redirector.
185: */
186: public void beginDependentTestUsingSession2(WebRequest theRequest) {
187: assertNotNull(this .dependentTest.sessionCookie);
188: theRequest.addCookie(this .dependentTest.sessionCookie);
189: }
190:
191: /**
192: * Verify that it is possible to share the HTTP Session between 2 tests.
193: * Please note that this is *NOT* recommended at all as unit tests must
194: * be independent one from another.
195: */
196: public void testDependentTestUsingSession2() {
197: assertEquals("dependentTestValue", session
198: .getAttribute("dependentTestId"));
199: }
200: }
|