KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jboss.test.cluster.test;
23
24 import junit.framework.Test;
25
26 import org.apache.commons.httpclient.HttpClient;
27
28 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
29 import org.jboss.test.JBossClusteredTestCase;
30
31 /**
32  * Clustering test case of get/set under non-scoped class loader.
33  * Replication granularity is attribute-based.
34  *
35  * @author Ben Wang
36  * @author Brian Stansberry
37  * @version $Id: FieldBasedTestCase.java 56126 2006-08-21 16:49:34Z hmesha $
38  */

39 public class FieldBasedTestCase
40       extends ScopedTestCase
41 {
42    
43    protected String JavaDoc modifySubjectUrl;
44    protected String JavaDoc modifySubjectUrlBase_;
45    
46    public FieldBasedTestCase(String JavaDoc name)
47    {
48       super(name);
49       warName_ = "/http-field/";
50       modifySubjectUrlBase_ = "modifySubject.jsp";
51       concatenate();
52    }
53
54    public static Test suite() throws Exception JavaDoc
55    {
56       Test t1 = JBossClusteredTestCase.getDeploySetup(FieldBasedTestCase.class,
57             "http-field.war");
58       return t1;
59    }
60
61    protected void concatenate()
62    {
63       super.concatenate();
64       modifySubjectUrl = warName_ +modifySubjectUrlBase_;
65    }
66    
67    protected String JavaDoc getWarName()
68    {
69       return "http-field";
70    }
71    
72    public void testSubjectObserver() throws Exception JavaDoc
73    {
74       getLog().debug("Enter testSubjectObserver");
75
76       getLog().debug(setUrl + ":::::::" + modifySubjectUrl);
77
78       // Create an instance of HttpClient.
79
HttpClient client = new HttpClient();
80
81       // Set the session attribute first
82
makeGet(client, baseURL0_ +setUrl);
83       
84       // Find out the session id and use it to build an FQN
85
String JavaDoc sessionID = getSessionID(client, servers_[0]);
86       // Strip off the jvmRoute, if there is one
87
sessionID = stripJvmRoute(sessionID);
88       String JavaDoc sessionFqn = "/JSESSION/localhost" + warName_ + sessionID;
89       
90       RMIAdaptor[] adaptors = getAdaptors();
91       
92       // Get the session from the cache
93
Object JavaDoc origVersion = SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn);
94       
95       // Modify the POJO stored in the session, but don't touch the
96
// session attributes (a reference to the POJO is stored in the
97
// servlet context)
98
makeGet(client, baseURL0_ +modifySubjectUrl);
99       
100       // Get the session from the cache
101
Object JavaDoc newVersion = SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn);
102       
103       // The byte[] should have been updated because the POJO was
104
assertFalse("Session body has been updated",
105                   origVersion.equals(newVersion));
106       
107       this.sleepThread(DEFAULT_SLEEP);
108       
109       // Get the session from the server1 cache
110
Object JavaDoc replVersion = SessionTestUtil.getSessionVersion(adaptors[1], sessionFqn);
111       
112       if (replVersion == null)
113       {
114          // Since we haven't accessed the session on server 1,
115
// see if it is in a buddy subtree
116
replVersion = SessionTestUtil.getBuddySessionVersion(adaptors[1], sessionFqn);
117       }
118       
119       // Should match the one on server0
120
assertTrue("Session body was replicated",
121                  newVersion.equals(replVersion));
122    }
123    
124    public void testObserverRemoval() throws Exception JavaDoc
125    {
126       getLog().debug("Enter testObserverRemoval");
127
128       getLog().debug(setUrl + ":::::::" + modifySubjectUrl);
129
130       // Create an instance of HttpClient.
131
HttpClient client = new HttpClient();
132
133       // Set the session attribute first
134
makeGet(client, baseURL0_ +setUrl);
135       
136       // Find out the session id and use it to build an FQN
137
String JavaDoc sessionID = getSessionID(client, servers_[0]);
138       // Strip off the jvmRoute, if there is one
139
sessionID = stripJvmRoute(sessionID);
140       String JavaDoc sessionFqn = "/JSESSION/localhost" + warName_ + sessionID;
141       
142       this.sleepThread(DEFAULT_SLEEP);
143       
144       // Switch over to server1 and get the POJO, which will also
145
// cause it to be stored in the servlet context on that server
146
setCookieDomainToThisServer(client, servers_[1]);
147       makeGet(client, baseURL1_ + getUrl);
148       
149       this.sleepThread(DEFAULT_SLEEP);
150       
151       // Now switch back to server0 and remove the POJO from the session
152
setCookieDomainToThisServer(client, servers_[0]);
153       makeGet(client, baseURL0_ + removeUrl);
154       
155       RMIAdaptor[] adaptors = getAdaptors();
156       
157       // Get the session from the cache
158
Object JavaDoc origVersion = SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn);
159       
160       // Modify the POJO that was originally stored in the session
161
// (a reference to the POJO is stored in the servlet context)
162
// Use a new client with no session cookie so we don't touch the session
163
HttpClient client2 = new HttpClient();
164       makeGet(client2, baseURL0_ +modifySubjectUrl);
165       
166       // Get the session from the cache
167
Object JavaDoc newVersion = SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn);
168       
169       // The version should not have been updated
170
assertEquals("Session version has not been updated",
171                    origVersion, newVersion);
172       
173       this.sleepThread(DEFAULT_SLEEP);
174       
175       // Get the session from the server1 cache
176
Object JavaDoc replVersion = SessionTestUtil.getSessionVersion(adaptors[1], sessionFqn);
177       
178       if (replVersion == null)
179       {
180          // Since we haven't accessed the session on server 1,
181
// see if it is in a buddy subtree
182
replVersion = SessionTestUtil.getBuddySessionVersion(adaptors[1], sessionFqn);
183       }
184       
185       // Should match the one on server0
186
assertEquals("Session body was replicated",
187                    newVersion, replVersion);
188       
189       // Make connection to server 1 and again modify the POJO via servlet ctx
190
// Again use a fresh client so we don't touch the session
191
HttpClient client3 = new HttpClient();
192       makeGet(client3, baseURL1_ + modifySubjectUrl);
193       
194       // Again get the session from the server1 cache
195
Object JavaDoc newReplVersion = SessionTestUtil.getSessionVersion(adaptors[1], sessionFqn);
196       
197       if (newReplVersion == null)
198       {
199          // Since we haven't accessed the session on server 1,
200
// see if it is in a buddy subtree
201
newReplVersion = SessionTestUtil.getBuddySessionVersion(adaptors[1], sessionFqn);
202       }
203       
204       // The byte[] should not have been updated
205
assertEquals("Session body has not been updated on server1",
206                    replVersion, newReplVersion);
207       
208    }
209
210 }
211
Popular Tags