/jetty/modules/jetty/src/test/java/org/mortbay/jetty/servlet/AbstractSessionTest.java

https://github.com/derickbailey/qedserver · Java · 175 lines · 94 code · 37 blank · 44 comment · 4 complexity · cc68d3dadaaf0e3f8638992edf2c7ae1 MD5 · raw file

  1. // ========================================================================
  2. // Copyright 2008 Mort Bay Consulting Pty. Ltd.
  3. // ------------------------------------------------------------------------
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ========================================================================
  14. package org.mortbay.jetty.servlet;
  15. import junit.framework.TestCase;
  16. public abstract class AbstractSessionTest extends TestCase
  17. {
  18. public static final String __host1 = "localhost";
  19. public static final String __host2 = __host1;
  20. public static final String __port1 = "8010";
  21. public static final String __port2 = "8011";
  22. SessionTestServer _server1;
  23. SessionTestServer _server2;
  24. public abstract SessionTestServer newServer1 ();
  25. public abstract SessionTestServer newServer2();
  26. public void setUp () throws Exception
  27. {
  28. _server1 = newServer1();
  29. _server2 = newServer2();
  30. _server1.start();
  31. _server2.start();
  32. }
  33. public void tearDown () throws Exception
  34. {
  35. if (_server1 != null)
  36. _server1.stop();
  37. if (_server2 != null)
  38. _server2.stop();
  39. _server1=null;
  40. _server2=null;
  41. }
  42. public void testSessions () throws Exception
  43. {
  44. SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
  45. SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
  46. // confirm that user has no session
  47. assertFalse(client1.send("/contextA", null));
  48. String cookie1 = client1.newSession("/contextA");
  49. assertNotNull(cookie1);
  50. System.err.println("cookie1: " + cookie1);
  51. // confirm that client2 has the same session attributes as client1
  52. assertTrue(client1.setAttribute("/contextA", cookie1, "foo", "bar"));
  53. assertTrue(client2.hasAttribute("/contextA", cookie1, "foo", "bar"));
  54. // confirm that /contextA would share same sessionId as /contextB
  55. assertTrue(client1.send("/contextA/dispatch/forward/contextB", cookie1));
  56. assertTrue(client2.send("/contextA/dispatch/forward/contextB", cookie1));
  57. assertTrue(client1.send("/contextB", cookie1));
  58. // verify that session attributes on /contextA is different from /contextB
  59. assertFalse(client1.hasAttribute("/contextB/action", cookie1, "foo", "bar"));
  60. // add new session attributes on /contextB
  61. client1.setAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy");
  62. assertTrue(client1.hasAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy"));
  63. // verify that client2 has same sessionAttributes on /contextB
  64. // client1's newly added attribute "zzzzz" needs to be flushed to the database first
  65. // saveInterval is configured at 10s... to test, uncomment the 2 lines below.
  66. //Thread.sleep(10000);
  67. //assertTrue(client2.hasAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy"));
  68. String cookie2 = client2.newSession("/contextA");
  69. assertNotNull(cookie2);
  70. System.err.println("cookie2: " + cookie2);
  71. // confirm that client1 has same session attributes as client2
  72. assertTrue(client2.setAttribute("/contextA", cookie2, "hello", "world"));
  73. assertTrue(client1.hasAttribute("/contextA", cookie2, "hello", "world"));
  74. // confirm that /contextA would share same sessionId as /contextB
  75. assertTrue(client1.send("/contextA/dispatch/forward/contextB", cookie2));
  76. assertTrue(client2.send("/contextA/dispatch/forward/contextB", cookie2));
  77. assertTrue(client1.send("/contextB", cookie2));
  78. // Session invalidate on contextA
  79. assertTrue(client1.invalidate("/contextA", cookie1));
  80. // confirm that session on contextB has not been invalidated after contextA has been invalidated
  81. assertTrue(client1.send("/contextB", cookie1));
  82. // confirm that session on contextA has been deleted
  83. assertFalse(client1.send("/contextA", cookie1));
  84. // Session invalidate on contextB
  85. assertTrue(client1.invalidate("/contextB/action", cookie1));
  86. // confirm that session on contextB has been deleted
  87. assertFalse(client1.send("/contextB/action", cookie1));
  88. // wait for saveInterval and check if the session invalidation has been reflected to the other node
  89. // to test, uncomment 3 lines below
  90. //Thread.sleep(10000);
  91. //assertFalse(client2.send("/contextA", cookie1));
  92. //assertFalse(client2.send("/contextB/action", cookie1));
  93. }
  94. public void testSessionManagerStop() throws Exception
  95. {
  96. SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
  97. SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
  98. // confirm that user has no session
  99. assertFalse(client1.send("/contextA", null));
  100. String cookie1 = client1.newSession("/contextA");
  101. assertNotNull(cookie1);
  102. System.err.println("cookie1: " + cookie1);
  103. // creates a session for contextB
  104. assertTrue(client1.send("/contextB", cookie1));
  105. // confirm that /contextA and /contextB sessions are available
  106. assertTrue(client1.send("/contextA", cookie1));
  107. assertTrue(client1.send("/contextB/action", cookie1));
  108. assertTrue(client1.setAttribute("/contextA", cookie1, "a", "b"));
  109. assertTrue(client1.setAttribute("/contextB/action", cookie1, "c", "d"));
  110. // confirm that /contextA and /contextB sessions from client2 are available
  111. assertTrue(client2.send("/contextA", cookie1));
  112. assertTrue(client2.send("/contextB/action", cookie1));
  113. assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
  114. assertTrue(client2.hasAttribute("/contextB/action", cookie1, "c", "d"));
  115. // stop sessionManager from node1
  116. _server1._sessionMgr1.stop();
  117. // verify session still exists for contextB
  118. assertTrue(client1.send("/contextB/action", cookie1));
  119. assertTrue(client1.hasAttribute("/contextB/action", cookie1, "c", "d"));
  120. // stop sessionManager from node2
  121. _server2._sessionMgr2.stop();
  122. // verfiy session still exists for contextA
  123. assertTrue(client2.send("/contextA", cookie1));
  124. assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
  125. }
  126. public void testFailover() throws Exception
  127. {
  128. SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
  129. SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
  130. // confirm that user has no session
  131. assertFalse(client1.send("/contextA", null));
  132. String cookie1 = client1.newSession("/contextA");
  133. assertNotNull(cookie1);
  134. System.err.println("cookie1: " + cookie1);
  135. assertTrue(client1.setAttribute("/contextA", cookie1, "a", "b"));
  136. assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
  137. }
  138. }