KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > http > HTTPInvokerClientTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.http;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Properties JavaDoc;
12 import org.apache.log4j.Level;
13 import org.jboss.remoting.Client;
14 import org.jboss.remoting.InvokerLocator;
15
16 import junit.framework.TestCase;
17
18 /**
19  * Test case that uses the HTTPInvoker client to call on two different public SOAP services (one based
20  * on Axis and the other based on .NET implementations).
21  *
22  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
23  */

24 public class HTTPInvokerClientTestCase extends TestCase
25 {
26    private Client client;
27
28    public void init(String JavaDoc httpTargetURL)
29    {
30       try
31       {
32          InvokerLocator locator = new InvokerLocator(httpTargetURL);
33          client = new Client(locator, null);
34          client.connect();
35       }
36       catch(Exception JavaDoc e)
37       {
38          e.printStackTrace();
39       }
40    }
41
42    public String JavaDoc makeInvocationCall(String JavaDoc httpTargetURL, String JavaDoc payload, Map JavaDoc metadata) throws Throwable JavaDoc
43    {
44       init(httpTargetURL);
45       Object JavaDoc obj = client.invoke(payload, metadata);
46
47       System.out.println("invoke returned" + obj);
48
49       return (String JavaDoc) obj;
50    }
51
52    public void testWeatherHTTPInvocation() throws Throwable JavaDoc
53    {
54
55       org.apache.log4j.BasicConfigurator.configure();
56       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
57       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
58       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
59       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
60
61
62       String JavaDoc testURL = "http://services.xmethods.net:80/soap/servlet/rpcrouter";
63
64       String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
65                    "<soap:Envelope xmlns:mrns0=\"urn:xmethods-Temperature\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
66                    " <soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
67                    " <mrns0:getTemp>\n" +
68                    " <zipcode xsi:type=\"xs:string\">30106</zipcode>\n" +
69                    " </mrns0:getTemp>\n" +
70                    " </soap:Body>\n" +
71                    "</soap:Envelope>";
72
73       Map JavaDoc metadata = new HashMap JavaDoc();
74       metadata.put(Client.RAW, Boolean.TRUE);
75       metadata.put("TYPE", "POST");
76
77       // proxy info
78
//metadata.put("http.proxyHost", "ginger");
79
//metadata.put("http.proxyPort", "80");
80
//metadata.put("http.proxy.username", "tom");
81
//metadata.put("http.proxy.password", "foobar");
82

83       Properties JavaDoc headerProps = new Properties JavaDoc();
84       headerProps.put("SOAPAction", "");
85       headerProps.put("Content-type", "text/xml; charset=UTF-8");
86
87       metadata.put("HEADER", headerProps);
88
89
90       HTTPInvokerClientTestCase client = new HTTPInvokerClientTestCase();
91
92       String JavaDoc result = client.makeInvocationCall(testURL, xml, metadata);
93       // don't need to comapre full string. (as actual temp value will change each time run)
94
assertEquals(getExpectedWeatherResult().substring(0, 380), result.substring(0, 380));
95
96    }
97
98    public void testCitiesByCountryHTTPInvocation() throws Throwable JavaDoc
99    {
100
101       org.apache.log4j.BasicConfigurator.configure();
102       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
103       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
104       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
105       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
106
107
108       String JavaDoc testURL = "http://www.webserviceX.NET/globalweather.asmx?op=GetCitiesByCountry";
109
110       String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
111                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
112                    " <soap:Body>\n" +
113                    " <GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\">\n" +
114                    " <CountryName>Germany</CountryName>\n" +
115                    " </GetCitiesByCountry>\n" +
116                    " </soap:Body>\n" +
117                    "</soap:Envelope>";
118
119       Map JavaDoc metadata = new HashMap JavaDoc();
120       metadata.put(Client.RAW, Boolean.TRUE);
121       metadata.put("TYPE", "POST");
122
123       // proxy info
124
//metadata.put("http.proxyHost", "ginger");
125
//metadata.put("http.proxyPort", "80");
126
//metadata.put("http.proxy.username", "tom");
127
//metadata.put("http.proxy.password", "foobar");
128

129       Properties JavaDoc headerProps = new Properties JavaDoc();
130       headerProps.put("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");
131       headerProps.put("Content-type", "text/xml; charset=UTF-8");
132
133       metadata.put("HEADER", headerProps);
134
135
136       HTTPInvokerClientTestCase client = new HTTPInvokerClientTestCase();
137
138       String JavaDoc result = client.makeInvocationCall(testURL, xml, metadata);
139       // 30 characters will be good enough.
140
assertEquals(getExpectedCityResult().substring(0, 2000), result.substring(0, 2000));
141
142    }
143
144
145    private String JavaDoc getExpectedWeatherResult()
146    {
147       return "<?xml version='1.0' encoding='UTF-8'?>" +
148              "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
149              "<SOAP-ENV:Body>" +
150              "<ns1:getTempResponse xmlns:ns1=\"urn:xmethods-Temperature\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
151              "<return xsi:type=\"xsd:float\">60.0</return>" +
152              "</ns1:getTempResponse>" +
153              "\n" +
154              "</SOAP-ENV:Body>\n" +
155              "</SOAP-ENV:Envelope>";
156    }
157
158    private String JavaDoc getExpectedCityResult()
159    {
160       return "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
161              "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
162              "<soap:Body><GetCitiesByCountryResponse xmlns=\"http://www.webserviceX.NET\"><GetCitiesByCountryResult>&lt;NewDataSet&gt; " +
163              "&lt;Table&gt; &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Schoenefeld&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
164              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Dresden-Klotzsche&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
165              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Erfurt-Bindersleben&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
166              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Frankfurt / M-Flughafen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
167              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Muenster / Osnabrueck&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
168              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hamburg-Fuhlsbuettel&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
169              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Tempelhof&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
170              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Koeln / Bonn&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
171              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Duesseldorf&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
172              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Munich / Riem&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
173              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Nuernberg&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
174              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Leipzig-Schkeuditz&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
175              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Saarbruecken / Ensheim&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
176              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Stuttgart-Echterdingen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
177              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Tegel&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
178              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hannover&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
179              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Bremen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
180              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hahn&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt;" +
181              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Baden Wurttemberg, Neuostheim&lt;/";
182    }
183
184 // public static void main(String[] args)
185
// {
186
// HTTPInvokerClientTest test = new HTTPInvokerClientTest();
187
// test.testHTTPInvocation();
188
// }
189

190 }
Popular Tags