KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.test.cluster.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.HttpState;
11 import org.apache.commons.httpclient.methods.GetMethod;
12 import org.jboss.test.JBossClusteredTestCase;
13
14 public class CrossContextCallsTestCase extends ScopedTestCase
15 {
16
17    public CrossContextCallsTestCase(String JavaDoc name)
18    {
19       super(name);
20       warName_ = "/http-cross-ctx-first/";
21       
22       concatenate();
23    }
24
25    public static Test suite() throws Exception JavaDoc
26    {
27       Test t1 = JBossClusteredTestCase.getDeploySetup(CrossContextCallsTestCase.class,
28             "http-cross-ctx.ear");
29       return t1;
30    }
31    
32    
33    /**
34     * Disabled; no-op.
35     */

36    public void testExcludeSecuritySubject() throws Exception JavaDoc
37    {
38       return;
39    }
40
41    /**
42     * Disabled; no-op.
43     */

44    public void testSessionBindingEvent() throws Exception JavaDoc
45    {
46       return;
47    }
48
49    protected String JavaDoc makeGet(HttpClient client, String JavaDoc url)
50    {
51       getLog().debug("makeGet(): trying to get from url " +url);
52
53       GetMethod method = new GetMethod(url);
54       int responseCode = 0;
55       try
56       {
57          responseCode = client.executeMethod(method);
58       } catch (IOException JavaDoc e)
59       {
60          e.printStackTrace();
61          fail("HttpClient executeMethod fails." +e.toString());
62       }
63       assertTrue("Get OK with url: " +url + " responseCode: " +responseCode
64         , responseCode == HttpURLConnection.HTTP_OK);
65       
66       String JavaDoc response = extractResponse(method);
67    
68       // Release the connection.
69
// method.releaseConnection();
70

71       return response;
72    }
73    
74    protected String JavaDoc getWarName()
75    {
76       return "http-cross-ctx-first";
77    }
78    
79    /**
80     * Makes a http call to the jsp that retrieves the attribute stored on the
81     * session. When the attribute values mathes with the one retrieved earlier,
82     * we have HttpSessionReplication.
83     * Makes use of commons-httpclient library of Apache
84     *
85     * @param client
86     * @param url
87     * @return session attribute
88     */

89    protected String JavaDoc makeGetWithState(HttpClient client, String JavaDoc url)
90    {
91       getLog().debug("makeGetWithState(): trying to get from url " +url);
92       GetMethod method = new GetMethod(url);
93       int responseCode = 0;
94       try
95       {
96          HttpState state = client.getState();
97          responseCode = client.executeMethod(method.getHostConfiguration(),
98             method, state);
99       } catch (IOException JavaDoc e)
100       {
101          e.printStackTrace();
102          fail("HttpClient executeMethod fails." +e.toString());
103       }
104       assertTrue("Get OK with url: " +url + " responseCode: " +responseCode,
105                  responseCode == HttpURLConnection.HTTP_OK);
106       
107       String JavaDoc response = extractResponse(method);
108    
109       // Release the connection.
110
// method.releaseConnection();
111

112       return response;
113    }
114    
115    private String JavaDoc extractResponse(GetMethod method)
116    {
117       Header header = method.getResponseHeader("FIRST");
118    
119       assertNotNull("Received FIRST header", header);
120       
121       String JavaDoc result = header.getValue();
122       
123       assertNotNull("FIRST header not null", result);
124       
125       header = method.getResponseHeader("SECOND");
126       
127       assertNotNull("Received SECOND header", header);
128       
129       String JavaDoc second = header.getValue();
130       
131       assertNotNull("FIRST header not null", second);
132       
133       result.concat(second);
134       
135       // Read the response body.
136
byte[] responseBody = method.getResponseBody();
137       // Use caution: ensure correct character encoding and is not binary data
138
result.concat(new String JavaDoc(responseBody));
139    
140       // Release the connection.
141
// method.releaseConnection();
142

143       return result;
144       
145    }
146
147 }
148
Popular Tags