KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > StateTransferTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * 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
23 package org.jboss.test.cluster.test;
24
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import junit.framework.Test;
33
34 import org.apache.commons.httpclient.HttpClient;
35 import org.jboss.cache.Fqn;
36 import org.jboss.cache.buddyreplication.BuddyManager;
37 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
38 import org.jboss.test.JBossClusteredTestCase;
39
40 /**
41  * Tests the use of the TreeCache.activateRegion()/inactivateRegion().
42  *
43  * TODO add a concurrency test.
44  *
45  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
46  * @version $Revision$
47  */

48 public class StateTransferTestCase extends BaseTest
49 {
50
51    private static final Object JavaDoc[] NULL_ARGS = new Object JavaDoc[] {};
52    private static final String JavaDoc[] NULL_TYPES = new String JavaDoc[] {};
53    
54    private static final ObjectName JavaDoc CACHE_OBJECT_NAME;
55    static
56    {
57       try
58       {
59          CACHE_OBJECT_NAME =
60             new ObjectName JavaDoc("jboss.cache:service=TomcatClusteringCache");
61       }
62       catch (MalformedObjectNameException JavaDoc e)
63       {
64          throw new ExceptionInInitializerError JavaDoc(e);
65       }
66    }
67    
68    protected String JavaDoc setUrl_;
69    protected String JavaDoc getUrl_;
70    protected String JavaDoc setUrlBase_;
71    protected String JavaDoc getUrlBase_;
72    
73    private ObjectName JavaDoc warObjectName;
74    private RMIAdaptor adaptor0_;
75    private RMIAdaptor adaptor1_;
76    private String JavaDoc warFqn_;
77    
78    /**
79     * Create a new StateTransferTestCase.
80     *
81     * @param name
82     */

83    public StateTransferTestCase(String JavaDoc name)
84    {
85       super(name);
86       setUrlBase_ = "setSession.jsp";
87       getUrlBase_ = "getAttribute.jsp";
88       concatenate();
89    }
90
91    protected void concatenate()
92    {
93       String JavaDoc contextPath = "/" + getWarName() + "/";
94       setUrl_ = contextPath +setUrlBase_;
95       getUrl_ = contextPath +getUrlBase_;
96    }
97
98    public static Test suite() throws Exception JavaDoc
99    {
100       Test t1 = JBossClusteredTestCase.getDeploySetup(StateTransferTestCase.class,
101             "http-scoped.war");
102       return t1;
103    }
104    
105    protected void setUp() throws Exception JavaDoc
106    {
107       super.setUp();
108       
109       if (warObjectName == null)
110       {
111          String JavaDoc oname = "jboss.web:J2EEApplication=none,J2EEServer=none," +
112                         "j2eeType=WebModule,name=//localhost/" + getWarName();
113          warObjectName = new ObjectName JavaDoc(oname);
114          
115          RMIAdaptor[] adaptors = getAdaptors();
116          adaptor0_ = adaptors[0];
117          adaptor1_ = adaptors[1];
118          
119          Object JavaDoc[] names = {"JSESSION", "localhost", getWarName() };
120          Fqn fqn = new Fqn(names);
121          warFqn_ = fqn.toString();
122       }
123    }
124    
125    protected void tearDown() throws Exception JavaDoc
126    {
127       try
128       {
129          SessionTestUtil.uninstallHelper(adaptor0_);
130       }
131       catch (Exception JavaDoc e) {}
132       try
133       {
134          SessionTestUtil.uninstallHelper(adaptor1_);
135       }
136       catch (Exception JavaDoc e) {}
137    }
138    
139    protected String JavaDoc getWarName()
140    {
141       return "http-scoped";
142    }
143    
144    public void testActivationInactivation() throws Exception JavaDoc
145    {
146       getLog().debug("Enter testActivationInactivation");
147
148       getLog().debug(setUrl_ + ":::::::" + getUrl_);
149       
150       // Stop the war on server1 (but leave the CacheHelper
151
SessionTestUtil.leaveHelperOnServer(adaptor1_);
152       adaptor1_.invoke(warObjectName, "stop" , NULL_ARGS, NULL_TYPES);
153
154       // Confirm the war isn't available on server1
155
HttpClient client0 = new HttpClient();
156       makeGetFailed(client0, baseURL1_ +setUrl_);
157       
158       // Create 3 sessions on server0
159
HttpClient[] clients = new HttpClient[3];
160       String JavaDoc[] attrs = new String JavaDoc[clients.length];
161       for (int i = 0; i < clients.length; i++)
162       {
163          clients[i] = new HttpClient();
164          makeGet(clients[i], baseURL0_ +setUrl_);
165          attrs[i] = makeGet(clients[i], baseURL0_ + getUrl_);
166          // Set cookie domain to server1
167
this.setCookieDomainToThisServer(clients[i], servers_[1]);
168       }
169       
170       getLog().debug("Sessions created");
171       
172       // Confirm there are no sessions in the server1 cache
173
Set JavaDoc sessions = SessionTestUtil.getSessionIds(adaptor1_, warFqn_);
174       
175       assertTrue("server1 has no cached sessions", sessions.size() == 0);
176       
177       getLog().debug("Server1 has no cached sessions");
178       
179       // Start the war on server1
180
adaptor1_.invoke(warObjectName, "start" , NULL_ARGS, NULL_TYPES);
181       
182       getLog().debug("Server1 started");
183       
184       // Confirm the sessions are in the server1 cache
185
sessions = SessionTestUtil.getSessionIds(adaptor1_, warFqn_);
186
187       assertEquals("server1 has cached sessions", clients.length, sessions.size());
188       
189       getLog().debug("Server1 has cached sessions");
190       
191       for (int i = 0; i < clients.length; i++)
192       {
193          String JavaDoc attr = makeGet(clients[i], baseURL1_ + getUrl_);
194          assertEquals("attribute matches for client " + i, attrs[i], attr);
195       }
196       
197       getLog().debug("Attributes match");
198       
199       // Sleep a bit in case the above get triggers replication that takes
200
// a while -- don't want a repl to arrive after the cache is cleared
201
sleep(500);
202       
203       // Stop the war on server0
204
SessionTestUtil.leaveHelperOnServer(adaptor0_);
205       adaptor0_.invoke(warObjectName, "stop" , NULL_ARGS, NULL_TYPES);
206       
207       // Confirm there are no sessions in the server0 cache
208
sessions = SessionTestUtil.getSessionIds(adaptor0_, warFqn_);
209       
210       assertTrue("server0 has no cached sessions", sessions.size() == 0);
211       
212       getLog().debug("Server0 has no cached sessions");
213    }
214 }
215
Popular Tags