PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/jboss-as-7.1.1.Final/web/src/test/java/org/jboss/as/web/session/MultipleWarSingleRedeployTestCase.java

#
Java | 200 lines | 129 code | 41 blank | 30 comment | 11 complexity | 3d0acd9f739665bc7478562a28269386 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.web.session;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertFalse;
  25. import static org.junit.Assert.assertNull;
  26. import static org.junit.Assert.assertTrue;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import org.infinispan.manager.EmbeddedCacheManager;
  31. import org.jboss.as.web.session.mocks.BasicRequestHandler;
  32. import org.jboss.as.web.session.mocks.SetAttributesRequestHandler;
  33. import org.jboss.logging.Logger;
  34. import org.jboss.metadata.web.jboss.JBossWebMetaData;
  35. import org.jboss.metadata.web.jboss.ReplicationGranularity;
  36. import org.jboss.metadata.web.jboss.ReplicationTrigger;
  37. import org.junit.After;
  38. import org.junit.Before;
  39. import org.junit.Test;
  40. /**
  41. * Tests for JBAS-7205. Deploy two wars on each node, sharing a cache between them. Confirm that redeploying one war results in
  42. * state being received.
  43. *
  44. * @author Brian Stansberry
  45. */
  46. public class MultipleWarSingleRedeployTestCase extends InfinispanCacheContainerTest {
  47. protected static long testId = System.currentTimeMillis();
  48. protected Logger log = Logger.getLogger(getClass());
  49. protected DistributableSessionManager<?>[] managersA = new DistributableSessionManager[cacheContainers.length];
  50. protected DistributableSessionManager<?>[] managersB = new DistributableSessionManager[cacheContainers.length];
  51. protected Map<String, Object> allAttributes;
  52. public MultipleWarSingleRedeployTestCase() {
  53. super(2, false, false, null);
  54. }
  55. @Before
  56. @Override
  57. public void start() throws Exception {
  58. super.start();
  59. allAttributes = new HashMap<String, Object>();
  60. allAttributes.put("key", "value");
  61. allAttributes = Collections.unmodifiableMap(allAttributes);
  62. }
  63. @After
  64. @Override
  65. public void stop() throws Exception {
  66. for (DistributableSessionManager<?> manager : managersA) {
  67. if (manager != null) {
  68. try {
  69. manager.stop();
  70. } catch (Exception e) {
  71. log.warn(e.getMessage(), e);
  72. }
  73. }
  74. }
  75. for (DistributableSessionManager<?> manager : managersB) {
  76. if (manager != null) {
  77. try {
  78. manager.stop();
  79. } catch (Exception e) {
  80. log.warn(e.getMessage(), e);
  81. }
  82. }
  83. }
  84. super.stop();
  85. }
  86. protected ReplicationGranularity getReplicationGranularity() {
  87. return ReplicationGranularity.SESSION;
  88. }
  89. protected ReplicationTrigger getReplicationTrigger() {
  90. return ReplicationTrigger.SET_AND_NON_PRIMITIVE_GET;
  91. }
  92. @Test
  93. @org.junit.Ignore
  94. public void testMultipleWarSingleRedeploy() throws Exception {
  95. String warnameA = "A" + String.valueOf(++testId);
  96. this.startManagers(warnameA, managersA);
  97. String warnameB = "B" + String.valueOf(testId);
  98. this.startManagers(warnameB, managersB);
  99. // Establish session.
  100. SetAttributesRequestHandler setHandler = new SetAttributesRequestHandler(allAttributes, false);
  101. SessionTestUtil.invokeRequest(managersA[0], setHandler, null);
  102. validateNewSession(setHandler);
  103. String idA = setHandler.getSessionId();
  104. setHandler = new SetAttributesRequestHandler(allAttributes, false);
  105. SessionTestUtil.invokeRequest(managersB[0], setHandler, null);
  106. validateNewSession(setHandler);
  107. String idB = setHandler.getSessionId();
  108. BasicRequestHandler getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
  109. SessionTestUtil.invokeRequest(managersA[1], getHandler, idA);
  110. validateExpectedAttributes(allAttributes, getHandler);
  111. getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
  112. SessionTestUtil.invokeRequest(managersB[1], getHandler, idB);
  113. validateExpectedAttributes(allAttributes, getHandler);
  114. // Undeploy one webapp on node 1
  115. managersB[1].stop();
  116. log.info("jbcmB1 stopped");
  117. // Deploy again
  118. managersB[1] = this.startManager(warnameB, cacheContainers[1]);
  119. log.info("jbcmB1 started");
  120. getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
  121. SessionTestUtil.invokeRequest(managersA[1], getHandler, idA);
  122. validateExpectedAttributes(allAttributes, getHandler);
  123. getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
  124. SessionTestUtil.invokeRequest(managersB[1], getHandler, idB);
  125. validateExpectedAttributes(allAttributes, getHandler);
  126. }
  127. protected void startManagers(String warname, DistributableSessionManager<?>[] managers) throws Exception {
  128. for (int i = 0; i < cacheContainers.length; i++) {
  129. managers[i] = this.startManager(warname, cacheContainers[i]);
  130. }
  131. }
  132. protected DistributableSessionManager<?> startManager(String warname, EmbeddedCacheManager cacheContainer) throws Exception {
  133. JBossWebMetaData metadata = SessionTestUtil.createWebMetaData(getReplicationGranularity(), getReplicationTrigger(), true, 30);
  134. DistributableSessionManager<?> manager = SessionTestUtil.createManager(metadata, warname, 100, cacheContainer, null);
  135. manager.start();
  136. return manager;
  137. }
  138. protected void validateExpectedAttributes(Map<String, Object> expected, BasicRequestHandler handler) {
  139. assertFalse(handler.isNewSession());
  140. if (handler.isCheckAttributeNames()) {
  141. assertEquals(expected.size(), handler.getAttributeNames().size());
  142. }
  143. Map<String, Object> checked = handler.getCheckedAttributes();
  144. assertEquals(expected.size(), checked.size());
  145. for (Map.Entry<String, Object> entry : checked.entrySet()) {
  146. assertEquals(entry.getKey(), expected.get(entry.getKey()), entry.getValue());
  147. }
  148. }
  149. protected void validateNewSession(BasicRequestHandler handler) {
  150. assertTrue(handler.isNewSession());
  151. assertEquals(handler.getCreationTime(), handler.getLastAccessedTime());
  152. if (handler.isCheckAttributeNames()) {
  153. assertEquals(0, handler.getAttributeNames().size());
  154. }
  155. Map<String, Object> checked = handler.getCheckedAttributes();
  156. for (Map.Entry<String, Object> entry : checked.entrySet())
  157. assertNull(entry.getKey(), entry.getValue());
  158. }
  159. }