KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > web > jk > test > JvmRouteURLRewritingTestCase


1 package org.jboss.test.cluster.web.jk.test;
2
3 import java.io.IOException JavaDoc;
4 import java.net.HttpURLConnection JavaDoc;
5
6 import junit.framework.Test;
7
8 import org.apache.commons.httpclient.Header;
9 import org.apache.commons.httpclient.HttpClient;
10 import org.apache.commons.httpclient.methods.GetMethod;
11 import org.jboss.test.JBossClusteredTestCase;
12 import org.jboss.test.cluster.test.BaseTest;
13
14 public class JvmRouteURLRewritingTestCase extends BaseTest
15 {
16
17    public JvmRouteURLRewritingTestCase(String JavaDoc name)
18    {
19       super(name);
20    }
21
22    public static Test suite() throws Exception JavaDoc
23    {
24       Test t1 = JBossClusteredTestCase.getDeploySetup(JvmRouteURLRewritingTestCase.class,
25             "http-jk.war");
26       return t1;
27    }
28    
29    public void testJkFailoverWithURLRewriting() throws Exception JavaDoc
30    {
31       getLog().debug("Enter testJkFailoverWithURLRewriting()");
32       
33       // Create an instance of HttpClient.
34
HttpClient client = new HttpClient();
35       
36       String JavaDoc sessId0 = makeGetWithRequestCount(client, baseURL0_, null, "1");
37       
38       // Fail over to node1
39
String JavaDoc sessId1 = makeGetWithRequestCount(client, baseURL1_, sessId0, "2");
40       
41       // Fail back over to node0
42
makeGetWithRequestCount(client, baseURL0_, sessId1, "3");
43    }
44    
45    private String JavaDoc makeGetWithRequestCount(HttpClient client,
46                                           String JavaDoc urlBase,
47                                           String JavaDoc sessionId,
48                                           String JavaDoc expectedCount)
49    {
50       String JavaDoc url = urlBase + "/http-jk/accessSession.jsp";
51       if (sessionId != null)
52       {
53          url = url + ";jsessionid=" + sessionId;
54       }
55       
56       GetMethod method = new GetMethod(url);
57       int responseCode = 0;
58       try
59       {
60          responseCode = client.executeMethod(method);
61       }
62       catch (IOException JavaDoc e)
63       {
64          e.printStackTrace();
65          fail("HttpClient executeMethod fails." +e.toString());
66       }
67       assertTrue("Get OK with url: " +url + " responseCode: " +responseCode
68         , responseCode == HttpURLConnection.HTTP_OK);
69       
70       // Validate that the request count is as expected,
71
// proving that state replication happened
72
Header hdr = method.getResponseHeader("X-TestRequestCount");
73       assertNotNull("Got the X-TestRequestCount header", hdr);
74       assertEquals("X-TestRequestCount header is correct", expectedCount, hdr.getValue());
75       
76       // Find out the session id
77
hdr = method.getResponseHeader("X-TestJSessionID");
78       assertNotNull("Got the X-TestJSessionID header", hdr);
79       String JavaDoc id = hdr.getValue();
80       
81       if (sessionId != null)
82       {
83          // Check the real session id is correct
84
assertEquals("Real session id is correct", stripJvmRoute(sessionId),
85                                                     stripJvmRoute(id));
86          // Check the session id has changed
87
assertFalse("Session id has changed", sessionId.equals(id));
88       }
89       
90       // Read the response body and confirm the URL was properly encoded.
91
byte[] responseBody = method.getResponseBody();
92       String JavaDoc body = new String JavaDoc(responseBody);
93       String JavaDoc expectedURL = "accessSession.jsp;jsessionid=" + id;
94       assertTrue("URL encoded properly: " + body, body.indexOf(expectedURL) > -1);
95       
96       return id;
97    }
98
99 }
100
Popular Tags