You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2005/01/28 20:08:59 UTC
cvs commit: jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit TestHttpSession.java
vmassol 2005/01/28 11:08:58
Modified: samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit
TestHttpSession.java
Log:
Show that it is possible to share the same session on the server side. Note that this is strongly discouraged. I haven't come across any real use case requiring this.
Revision Changes Path
1.4 +96 -1 jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestHttpSession.java
Index: TestHttpSession.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestHttpSession.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestHttpSession.java 29 Feb 2004 16:36:44 -0000 1.3
+++ TestHttpSession.java 28 Jan 2005 19:08:58 -0000 1.4
@@ -19,6 +19,9 @@
*/
package org.apache.cactus.sample.servlet.unit;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
import org.apache.cactus.HttpSessionCookie;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
@@ -32,6 +35,48 @@
public class TestHttpSession extends ServletTestCase
{
/**
+ * Save the session cookie for the testDependentTestUsingSession tests.
+ * The idea is to share the same session object on the server side.
+ */
+ private HttpSessionCookie sessionCookie;
+
+ /**
+ * Link this test to another test. This is to cleanly be able to pass a
+ * parameter to another test (in our case the session cookie).
+ */
+ private TestHttpSession dependentTest;
+
+ public TestHttpSession(String name)
+ {
+ super(name);
+ }
+
+ public TestHttpSession(String name, TestHttpSession test)
+ {
+ super(name);
+ this.dependentTest = test;
+ }
+
+ /**
+ * We order the tests so that we are sure that testDependentTestUsingSession
+ * is run before testDependentTestUsingSession2. This is because we wish to
+ * verify it is possible to share session data between 2 tests.
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new TestHttpSession("testNoAutomaticSessionCreation"));
+ suite.addTest(new TestHttpSession("testVerifyJsessionid"));
+ suite.addTest(new TestHttpSession("testCreateSessionCookie"));
+
+ TestHttpSession test = new TestHttpSession("testDependentTestUsingSession");
+ suite.addTest(test);
+ suite.addTest(new TestHttpSession("testDependentTestUsingSession2", test));
+
+ return suite;
+ }
+
+ /**
* Verify that it is possible to ask for no automatic session creation in
* the <code>beginXXX()</code> method.
*
@@ -99,4 +144,54 @@
+ "this request", !session.isNew());
}
+ //-------------------------------------------------------------------------
+
+ /**
+ * Verify that it is possible to share the HTTP Session between 2 tests.
+ * Please note that this is *NOT* recommended at all as unit tests must
+ * be independent one from another.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginDependentTestUsingSession(WebRequest theRequest)
+ {
+ this.sessionCookie = theRequest.getSessionCookie();
+ assertNotNull("Session cookie should not be null", sessionCookie);
+ theRequest.addCookie(sessionCookie);
+ }
+
+ /**
+ * Verify that it is possible to share the HTTP Session between 2 tests.
+ * Please note that this is *NOT* recommended at all as unit tests must
+ * be independent one from another.
+ */
+ public void testDependentTestUsingSession()
+ {
+ session.setAttribute("dependentTestId", "dependentTestValue");
+ }
+
+ /**
+ * Verify that it is possible to share the HTTP Session between 2 tests.
+ * Please note that this is *NOT* recommended at all as unit tests must
+ * be independent one from another.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginDependentTestUsingSession2(WebRequest theRequest)
+ {
+ assertNotNull(this.dependentTest.sessionCookie);
+ theRequest.addCookie(this.dependentTest.sessionCookie);
+ }
+
+ /**
+ * Verify that it is possible to share the HTTP Session between 2 tests.
+ * Please note that this is *NOT* recommended at all as unit tests must
+ * be independent one from another.
+ */
+ public void testDependentTestUsingSession2()
+ {
+ assertEquals("dependentTestValue", session.getAttribute("dependentTestId"));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org