001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.test;
023:
024: import java.io.IOException;
025: import java.net.HttpURLConnection;
026:
027: import junit.framework.Test;
028:
029: import org.apache.commons.httpclient.Header;
030: import org.apache.commons.httpclient.HttpClient;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.jboss.test.JBossClusteredTestCase;
033:
034: /**
035: * Clustering test case of get/set under scoped class loader.
036: *
037: * @author Ben Wang
038: * @version $Revision: 1.0
039: */
040: public class ScopedTestCase extends BaseTest {
041: protected String setUrl;
042: protected String getUrl;
043: protected String modifyUrl;
044: protected String modifyNoSetUrl;
045: protected String removeUrl;
046: protected String invalidateUrl;
047: protected String attrListUrl;
048: protected String bindUrl_;
049: protected String setSecuritySubjectUrl_;
050: protected String getSecuritySubjectUrl_;
051: protected String warName_;
052: protected String setUrlBase_;
053: protected String getUrlBase_;
054: protected String modifyUrlBase_;
055: protected String modifyNoSetUrlBase_;
056: protected String removeUrlBase_;
057: protected String invalidateUrlBase_;
058: protected String bindUrlBase_;
059: protected String attrListUrlBase_;
060: protected String setSecuritySubjectUrlBase_;
061: protected String getSecuritySubjectUrlBase_;
062:
063: public ScopedTestCase(String name) {
064: super (name);
065: warName_ = "/http-scoped/";
066: setUrlBase_ = "setSession.jsp";
067: getUrlBase_ = "getAttribute.jsp";
068: modifyUrlBase_ = "modifyAttribute.jsp";
069: modifyNoSetUrlBase_ = "modifyAttributeNoSet.jsp";
070: removeUrlBase_ = "removeAttribute.jsp";
071: invalidateUrlBase_ = "invalidateSession.jsp";
072: bindUrlBase_ = "bindSession.jsp?Binding=";
073: attrListUrlBase_ = "attributeNames.jsp";
074: setSecuritySubjectUrlBase_ = "setSecuritySubject.jsp";
075: getSecuritySubjectUrlBase_ = "getSecuritySubject.jsp";
076:
077: concatenate();
078: }
079:
080: protected void concatenate() {
081: setUrl = warName_ + setUrlBase_;
082: getUrl = warName_ + getUrlBase_;
083: modifyUrl = warName_ + modifyUrlBase_;
084: modifyNoSetUrl = warName_ + modifyNoSetUrlBase_;
085: removeUrl = warName_ + removeUrlBase_;
086: invalidateUrl = warName_ + invalidateUrlBase_;
087: bindUrl_ = warName_ + bindUrlBase_;
088: attrListUrl = warName_ + attrListUrlBase_;
089: setSecuritySubjectUrl_ = warName_ + setSecuritySubjectUrlBase_;
090: getSecuritySubjectUrl_ = warName_ + getSecuritySubjectUrlBase_;
091: }
092:
093: public static Test suite() throws Exception {
094: Test t1 = JBossClusteredTestCase.getDeploySetup(
095: ScopedTestCase.class, "http-scoped.war");
096: return t1;
097: }
098:
099: protected void setUp() throws Exception {
100: super .setUp();
101: }
102:
103: protected void tearDown() throws Exception {
104: super .tearDown();
105: }
106:
107: /**
108: * Main method that deals with the Http Session Replication Test
109: *
110: * @throws Exception
111: */
112: public void testNonPrimitiveGet() throws Exception {
113: String attr = "";
114: getLog().info("Enter testNonPrimitiveGet");
115:
116: getLog().debug(setUrl + ":::::::" + getUrl);
117:
118: // Create an instance of HttpClient.
119: HttpClient client = new HttpClient();
120:
121: // Set the session attribute first
122: makeGet(client, baseURL0_ + setUrl);
123:
124: // Create a method instance.
125: // Get the Attribute set
126: attr = makeGetWithState(client, baseURL0_ + getUrl);
127:
128: sleepThread(DEFAULT_SLEEP);
129:
130: // Make connection to server 1 and get
131: setCookieDomainToThisServer(client, servers_[1]);
132: String attr2 = makeGetWithState(client, baseURL1_ + getUrl);
133:
134: assertEquals("Get attribute should be but is ", attr, attr2);
135: getLog().debug("Exit testNonPrimitiveGet");
136: }
137:
138: /**
139: * Test session modify with non-primitive get/modify.
140: *
141: * @throws Exception
142: */
143: public void testNonPrimitiveModify() throws Exception {
144: String attr = "";
145: getLog().info("Enter testNonPrimitiveModify");
146:
147: getLog().debug(setUrl + ":::::::" + getUrl);
148:
149: // Create an instance of HttpClient.
150: HttpClient client = new HttpClient();
151:
152: // Set the session attribute first
153: makeGet(client, baseURL0_ + setUrl);
154:
155: // Get the Attribute set
156: String attrOld = makeGetWithState(client, baseURL0_ + getUrl);
157:
158: // Modify a method instance.
159: makeGet(client, baseURL0_ + modifyUrl);
160:
161: // Get the Attribute set
162: attr = makeGetWithState(client, baseURL0_ + getUrl);
163:
164: sleepThread(DEFAULT_SLEEP);
165:
166: // Make connection to server 1 and get
167: setCookieDomainToThisServer(client, servers_[1]);
168: String attr2 = makeGetWithState(client, baseURL1_ + getUrl);
169:
170: // Check the result
171: assertNotSame(
172: "Old attribute should be different from new one.",
173: attrOld, attr);
174: assertEquals("Attributes should be the same", attr, attr2);
175: getLog().debug("Exit testNonPrimitiveModify");
176: }
177:
178: /**
179: * Test session modify with non-primitive get/modify.
180: *
181: * @throws Exception
182: */
183: public void testNonPrimitiveRepeatedModify() throws Exception {
184: String attr = "";
185: getLog().info("Enter testNonPrimitiveRepeatedModify");
186:
187: getLog().debug(setUrl + ":::::::" + getUrl);
188:
189: // Create an instance of HttpClient.
190: HttpClient client = new HttpClient();
191:
192: // Set the session attribute first
193: makeGet(client, baseURL0_ + setUrl);
194:
195: // Get the Attribute set
196: String attrOld = makeGetWithState(client, baseURL0_ + getUrl);
197:
198: // Modify a method instance.
199: makeGet(client, baseURL0_ + modifyUrl);
200:
201: // Get the Attribute set
202: attr = makeGetWithState(client, baseURL0_ + getUrl);
203:
204: sleepThread(DEFAULT_SLEEP);
205:
206: // Make connection to server 1 and get
207: setCookieDomainToThisServer(client, servers_[1]);
208: String attr2 = makeGetWithState(client, baseURL1_ + getUrl);
209:
210: // Check the result
211: assertNotSame(
212: "Old attribute should be different from new one.",
213: attrOld, attr);
214: assertEquals("Attributes should be the same", attr, attr2);
215:
216: // Modify a method instance.
217: makeGet(client, baseURL1_ + modifyUrl);
218:
219: // Get the Attribute set
220: attr = makeGetWithState(client, baseURL1_ + getUrl);
221:
222: sleepThread(DEFAULT_SLEEP);
223:
224: // Make connection to server 1 and get
225: setCookieDomainToThisServer(client, servers_[0]);
226: attr2 = makeGetWithState(client, baseURL0_ + getUrl);
227:
228: // Check the result
229: assertEquals(
230: "Attributes should be the same after second modify",
231: attr, attr2);
232: getLog().debug("Exit testNonPrimitiveModify");
233: }
234:
235: /**
236: * Test session modify with non-primitive remove.
237: *
238: * @throws Exception
239: */
240: public void testNonPrimitiveRemove() throws Exception {
241: getLog().info("Enter testNonPrimitiveRemove");
242:
243: getLog().debug(setUrl + ":::::::" + getUrl);
244:
245: // Create an instance of HttpClient.
246: HttpClient client = new HttpClient();
247:
248: // Set the session attribute first
249: makeGet(client, baseURL0_ + setUrl);
250:
251: // Modify a method instance.
252: makeGet(client, baseURL0_ + modifyUrl);
253:
254: // Get the Attribute set
255: makeGetWithState(client, baseURL0_ + getUrl);
256:
257: // Get the list of attributes
258: String attrList = makeGet(client, baseURL0_ + attrListUrl);
259:
260: assertTrue("TEST_PERSON is an attribute", attrList
261: .indexOf("TEST_PERSON") > -1);
262:
263: sleepThread(DEFAULT_SLEEP);
264:
265: // Make connection to server 1 and get
266: setCookieDomainToThisServer(client, servers_[1]);
267: // Get the Attribute set
268: makeGetWithState(client, baseURL1_ + getUrl);
269: // Get the list of attributes
270: String attrList1 = makeGet(client, baseURL1_ + attrListUrl);
271:
272: assertTrue("TEST_PERSON should be an attribute on server1",
273: attrList1.indexOf("TEST_PERSON") > -1);
274:
275: // Remove the attribute
276: makeGet(client, baseURL1_ + removeUrl);
277: // Attribute is now null. Should have not OK response.
278: makeGetFailed(client, baseURL1_ + getUrl);
279:
280: // Confirm the attribute is gone from the list
281: attrList1 = makeGet(client, baseURL1_ + attrListUrl);
282:
283: assertTrue("TEST_PERSON should not be an attribute", attrList1
284: .indexOf("TEST_PERSON") == -1);
285:
286: sleepThread(DEFAULT_SLEEP);
287: // Make connection to server 0 and get
288: setCookieDomainToThisServer(client, servers_[0]);
289: // Attribute is now null. Should have not OK response.
290: makeGetFailed(client, baseURL0_ + getUrl);
291:
292: // Confirm the attribute is gone from the list
293: attrList = makeGet(client, baseURL0_ + attrListUrl);
294:
295: assertTrue("TEST_PERSON should not be an attribute on server0",
296: attrList.indexOf("TEST_PERSON") == -1);
297:
298: getLog().debug("Exit testNonPrimitiveRemove");
299: }
300:
301: /**
302: * Test session modify with non-primitive get/modify from node2 and see if it replicates correctly
303: * on node1 or not.
304: *
305: * @throws Exception
306: */
307: public void testNonPrimitiveModifyFromAlternativeNode()
308: throws Exception {
309: String attr = "";
310: getLog()
311: .info("Enter testNonPrimitiveModifyFromAlternativeNode");
312:
313: getLog().debug(setUrl + ":::::::" + getUrl);
314:
315: // Create an instance of HttpClient.
316: HttpClient client = new HttpClient();
317:
318: // Set the session attribute first
319: makeGet(client, baseURL0_ + setUrl);
320:
321: // Get the Attribute set
322: String attrOld = makeGetWithState(client, baseURL0_ + getUrl);
323: sleepThread(DEFAULT_SLEEP);
324: // Get the Attribute set
325: setCookieDomainToThisServer(client, servers_[1]);
326: String attrOld1 = makeGetWithState(client, baseURL1_ + getUrl);
327: // Check the result
328: assertEquals("Attributes should be the same", attrOld, attrOld1);
329:
330: // Modify a method instance.
331: makeGet(client, baseURL1_ + modifyUrl);
332: // Make connection to server 1 and get
333: String attr2 = makeGetWithState(client, baseURL1_ + getUrl);
334:
335: sleepThread(400);
336: // Get the Attribute set
337: setCookieDomainToThisServer(client, servers_[0]);
338: attr = makeGetWithState(client, baseURL0_ + getUrl);
339:
340: assertEquals("Attributes should be the same", attr, attr2);
341: getLog().debug(
342: "Exit testNonPrimitiveModifyModifyFromAlternativeNode");
343: }
344:
345: /**
346: * Test invalidate session
347: *
348: * @throws Exception
349: */
350: public void testInvalidate() throws Exception {
351: getLog().debug("Enter testInvalidate");
352:
353: getLog().debug(setUrl + ":::::::" + getUrl);
354:
355: invalidate();
356:
357: getLog().debug("Exit testInvalidate");
358: }
359:
360: public void testSessionBindingEvent() throws Exception {
361: String attr = "";
362: getLog().info("Enter testSessionBindingEvent");
363:
364: // Create an instance of HttpClient.
365: HttpClient client = new HttpClient();
366:
367: // Bind a new HttpSessionListener to the session
368: // and check that there is a valueBound() event
369: attr = makeGet(client, baseURL0_ + bindUrl_ + "new");
370: assertTrue("Got OK when binding a new listener",
371: (attr != null && attr.indexOf("OK") >= 0));
372:
373: // Rebind the same HttpSessionListener to the session
374: // and check that there is no valueUnbound()
375: attr = makeGet(client, baseURL0_ + bindUrl_ + "rebind");
376: assertTrue("Got OK when rebinding an existing listener",
377: (attr != null && attr.indexOf("OK") >= 0));
378:
379: // Replace the HttpSessionListener with another one
380: // and check that there is a valueUnbound()
381: attr = makeGet(client, baseURL0_ + bindUrl_ + "replace");
382: assertTrue("Got OK when replacing a listener",
383: (attr != null && attr.indexOf("OK") >= 0));
384:
385: // Remove the same HttpSessionListener
386: // and check that there is a valueUnbound()
387: attr = makeGet(client, baseURL0_ + bindUrl_ + "remove");
388: assertTrue("Got OK when removing a listener",
389: (attr != null && attr.indexOf("OK") >= 0));
390: }
391:
392: public void testExcludeSecuritySubject() throws Exception {
393: getLog().debug("Enter testExcludeSecuritySubject");
394:
395: getLog().debug(
396: setSecuritySubjectUrl_ + ":::::::"
397: + getSecuritySubjectUrl_);
398:
399: // Create an instance of HttpClient.
400: HttpClient client = new HttpClient();
401:
402: // Set the session attribute first
403: makeGet(client, baseURL0_ + setSecuritySubjectUrl_);
404:
405: // Confirm the attribute is available from the server where it was set
406: String attrOrig = makeGet(client, baseURL0_
407: + getSecuritySubjectUrl_);
408: assertTrue("javax.security.auth.subject available locally",
409: attrOrig.indexOf("javax.security.auth.Subject") > -1);
410:
411: sleepThread(DEFAULT_SLEEP);
412:
413: // Check if the attribute replicated
414: setCookieDomainToThisServer(client, servers_[1]);
415: String attrRepl = makeGet(client, baseURL1_
416: + getSecuritySubjectUrl_);
417: assertTrue("javax.security.Subject did not replicate", attrRepl
418: .indexOf("java.lang.String") > -1);
419: }
420:
421: /**
422: * Test for JBAS-3528 (http://jira.jboss.com/jira/browse/JBAS-3528).
423: *
424: * @throws Exception
425: */
426: public void testIsNew() throws Exception {
427: getLog().debug("Enter testIsNew");
428:
429: getLog().debug(setUrl + ":::::::" + getUrl);
430:
431: // Create an instance of HttpClient.
432: HttpClient client = new HttpClient();
433:
434: // Set the session attribute first
435: makeGet(client, baseURL0_ + setUrl);
436:
437: sleepThread(DEFAULT_SLEEP);
438:
439: // Let's switch to server 2 to retrieve the session attribute.
440: setCookieDomainToThisServer(client, servers_[1]);
441: assertFalse("Session is not new", checkNew(client, baseURL1_
442: + getUrl));
443: }
444:
445: /**
446: * Makes a http call to the given url and confirms that a non-null
447: * header X-SessionIsNew is returned. Converts the value
448: * of the header to a boolean and returns it.
449: *
450: * @param client
451: * @param url
452: */
453: protected boolean checkNew(HttpClient client, String url) {
454: getLog().info("checkNew(): trying to get from url " + url);
455:
456: GetMethod method = new GetMethod(url);
457: int responseCode = 0;
458: try {
459: responseCode = client.executeMethod(method);
460: } catch (IOException e) {
461: e.printStackTrace();
462: fail("HttpClient executeMethod fails." + e.toString());
463: }
464: assertTrue("Get OK with url: " + url + " responseCode: "
465: + responseCode,
466: responseCode == HttpURLConnection.HTTP_OK);
467:
468: Header hdr = method.getResponseHeader("X-SessionIsNew");
469: assertNotNull("Got X-SessionIsNew header", hdr);
470: String value = hdr.getValue();
471: assertNotNull("Got non-nullX-SessionIsNew header", value);
472:
473: return Boolean.valueOf(value).booleanValue();
474: }
475:
476: protected void invalidate() throws Exception {
477: // Create an instance of HttpClient.
478: HttpClient client = new HttpClient();
479:
480: // Set the session attribute first
481: makeGet(client, baseURL0_ + setUrl);
482:
483: // Get the Attribute set
484: String attr0 = makeGet(client, baseURL0_ + getUrl);
485:
486: sleepThread(DEFAULT_SLEEP);
487:
488: // Make connection to server 1 and get
489: setCookieDomainToThisServer(client, servers_[1]);
490: String attr1 = makeGet(client, baseURL1_ + getUrl);
491:
492: assertEquals("attributes match", attr0, attr1);
493:
494: // Invalidate the session
495: makeGet(client, baseURL1_ + invalidateUrl);
496:
497: sleepThread(DEFAULT_SLEEP + 200); // wait a bit longer to propagate
498:
499: // Make connection to server 1 and get
500: setCookieDomainToThisServer(client, servers_[0]);
501: // Session is invalidated. Should have not OK response.
502: makeGetFailed(client, baseURL0_ + getUrl);
503: }
504:
505: }
|