PageRenderTime 67ms CodeModel.GetById 16ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 23package org.jboss.as.web.session;
 24
 25import static org.junit.Assert.assertEquals;
 26import static org.junit.Assert.assertFalse;
 27import static org.junit.Assert.assertNull;
 28import static org.junit.Assert.assertTrue;
 29
 30import java.util.Collections;
 31import java.util.HashMap;
 32import java.util.Map;
 33
 34import org.infinispan.manager.EmbeddedCacheManager;
 35import org.jboss.as.web.session.mocks.BasicRequestHandler;
 36import org.jboss.as.web.session.mocks.SetAttributesRequestHandler;
 37import org.jboss.logging.Logger;
 38import org.jboss.metadata.web.jboss.JBossWebMetaData;
 39import org.jboss.metadata.web.jboss.ReplicationGranularity;
 40import org.jboss.metadata.web.jboss.ReplicationTrigger;
 41import org.junit.After;
 42import org.junit.Before;
 43import org.junit.Test;
 44
 45/**
 46 * Tests for JBAS-7205. Deploy two wars on each node, sharing a cache between them. Confirm that redeploying one war results in
 47 * state being received.
 48 *
 49 * @author Brian Stansberry
 50 */
 51public class MultipleWarSingleRedeployTestCase extends InfinispanCacheContainerTest {
 52    protected static long testId = System.currentTimeMillis();
 53
 54    protected Logger log = Logger.getLogger(getClass());
 55
 56    protected DistributableSessionManager<?>[] managersA = new DistributableSessionManager[cacheContainers.length];
 57    protected DistributableSessionManager<?>[] managersB = new DistributableSessionManager[cacheContainers.length];
 58
 59    protected Map<String, Object> allAttributes;
 60
 61    public MultipleWarSingleRedeployTestCase() {
 62        super(2, false, false, null);
 63    }
 64
 65    @Before
 66    @Override
 67    public void start() throws Exception {
 68        super.start();
 69
 70        allAttributes = new HashMap<String, Object>();
 71
 72        allAttributes.put("key", "value");
 73
 74        allAttributes = Collections.unmodifiableMap(allAttributes);
 75    }
 76
 77    @After
 78    @Override
 79    public void stop() throws Exception {
 80        for (DistributableSessionManager<?> manager : managersA) {
 81            if (manager != null) {
 82                try {
 83                    manager.stop();
 84                } catch (Exception e) {
 85                    log.warn(e.getMessage(), e);
 86                }
 87            }
 88        }
 89
 90        for (DistributableSessionManager<?> manager : managersB) {
 91            if (manager != null) {
 92                try {
 93                    manager.stop();
 94                } catch (Exception e) {
 95                    log.warn(e.getMessage(), e);
 96                }
 97            }
 98        }
 99
100        super.stop();
101    }
102
103    protected ReplicationGranularity getReplicationGranularity() {
104        return ReplicationGranularity.SESSION;
105    }
106
107    protected ReplicationTrigger getReplicationTrigger() {
108        return ReplicationTrigger.SET_AND_NON_PRIMITIVE_GET;
109    }
110
111    @Test
112    @org.junit.Ignore
113    public void testMultipleWarSingleRedeploy() throws Exception {
114        String warnameA = "A" + String.valueOf(++testId);
115        this.startManagers(warnameA, managersA);
116
117        String warnameB = "B" + String.valueOf(testId);
118        this.startManagers(warnameB, managersB);
119
120        // Establish session.
121        SetAttributesRequestHandler setHandler = new SetAttributesRequestHandler(allAttributes, false);
122        SessionTestUtil.invokeRequest(managersA[0], setHandler, null);
123        validateNewSession(setHandler);
124        String idA = setHandler.getSessionId();
125
126        setHandler = new SetAttributesRequestHandler(allAttributes, false);
127        SessionTestUtil.invokeRequest(managersB[0], setHandler, null);
128        validateNewSession(setHandler);
129        String idB = setHandler.getSessionId();
130
131        BasicRequestHandler getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
132        SessionTestUtil.invokeRequest(managersA[1], getHandler, idA);
133
134        validateExpectedAttributes(allAttributes, getHandler);
135
136        getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
137        SessionTestUtil.invokeRequest(managersB[1], getHandler, idB);
138
139        validateExpectedAttributes(allAttributes, getHandler);
140
141        // Undeploy one webapp on node 1
142        managersB[1].stop();
143        log.info("jbcmB1 stopped");
144
145        // Deploy again
146        managersB[1] = this.startManager(warnameB, cacheContainers[1]);
147
148        log.info("jbcmB1 started");
149
150        getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
151        SessionTestUtil.invokeRequest(managersA[1], getHandler, idA);
152
153        validateExpectedAttributes(allAttributes, getHandler);
154
155        getHandler = new BasicRequestHandler(allAttributes.keySet(), false);
156        SessionTestUtil.invokeRequest(managersB[1], getHandler, idB);
157
158        validateExpectedAttributes(allAttributes, getHandler);
159    }
160
161    protected void startManagers(String warname, DistributableSessionManager<?>[] managers) throws Exception {
162        for (int i = 0; i < cacheContainers.length; i++) {
163            managers[i] = this.startManager(warname, cacheContainers[i]);
164        }
165    }
166
167    protected DistributableSessionManager<?> startManager(String warname, EmbeddedCacheManager cacheContainer) throws Exception {
168        JBossWebMetaData metadata = SessionTestUtil.createWebMetaData(getReplicationGranularity(), getReplicationTrigger(), true, 30);
169        DistributableSessionManager<?> manager = SessionTestUtil.createManager(metadata, warname, 100, cacheContainer, null);
170        manager.start();
171
172        return manager;
173    }
174
175    protected void validateExpectedAttributes(Map<String, Object> expected, BasicRequestHandler handler) {
176        assertFalse(handler.isNewSession());
177
178        if (handler.isCheckAttributeNames()) {
179            assertEquals(expected.size(), handler.getAttributeNames().size());
180        }
181        Map<String, Object> checked = handler.getCheckedAttributes();
182        assertEquals(expected.size(), checked.size());
183        for (Map.Entry<String, Object> entry : checked.entrySet()) {
184            assertEquals(entry.getKey(), expected.get(entry.getKey()), entry.getValue());
185        }
186
187    }
188
189    protected void validateNewSession(BasicRequestHandler handler) {
190        assertTrue(handler.isNewSession());
191        assertEquals(handler.getCreationTime(), handler.getLastAccessedTime());
192        if (handler.isCheckAttributeNames()) {
193            assertEquals(0, handler.getAttributeNames().size());
194        }
195        Map<String, Object> checked = handler.getCheckedAttributes();
196        for (Map.Entry<String, Object> entry : checked.entrySet())
197            assertNull(entry.getKey(), entry.getValue());
198    }
199
200}