KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > 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.remoting.transport.http;
8
9 import junit.framework.TestCase;
10 import org.apache.log4j.Level;
11 import org.jboss.remoting.Client;
12 import org.jboss.remoting.InvokerLocator;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Properties JavaDoc;
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       Properties JavaDoc headerProps = new Properties JavaDoc();
78       headerProps.put("SOAPAction", "");
79       headerProps.put("Content-type", "text/xml; charset=UTF-8");
80
81       metadata.put("HEADER", headerProps);
82
83
84       HTTPInvokerClientTestCase client = new HTTPInvokerClientTestCase();
85
86       String JavaDoc result = client.makeInvocationCall(testURL, xml, metadata);
87       // don't need to comapre full string. (as actual temp value will change each time run)
88
assertEquals(getExpectedWeatherResult().substring(0, 380), result.substring(0, 380));
89
90    }
91
92    public void testCitiesByCountryHTTPInvocation() throws Throwable JavaDoc
93    {
94
95       org.apache.log4j.BasicConfigurator.configure();
96       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
97       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
98       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
99       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
100
101
102       String JavaDoc testURL = "http://www.webserviceX.NET/globalweather.asmx?op=GetCitiesByCountry";
103
104       String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
105                    "<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" +
106                    " <soap:Body>\n" +
107                    " <GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\">\n" +
108                    " <CountryName>Germany</CountryName>\n" +
109                    " </GetCitiesByCountry>\n" +
110                    " </soap:Body>\n" +
111                    "</soap:Envelope>";
112
113       Map JavaDoc metadata = new HashMap JavaDoc();
114       metadata.put(Client.RAW, Boolean.TRUE);
115       metadata.put("TYPE", "POST");
116
117       Properties JavaDoc headerProps = new Properties JavaDoc();
118       headerProps.put("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");
119       headerProps.put("Content-type", "text/xml; charset=UTF-8");
120
121       metadata.put("HEADER", headerProps);
122
123
124       HTTPInvokerClientTestCase client = new HTTPInvokerClientTestCase();
125
126       String JavaDoc result = client.makeInvocationCall(testURL, xml, metadata);
127       // 30 characters will be good enough.
128
assertEquals(getExpectedCityResult().substring(0, 2000), result.substring(0, 2000));
129
130    }
131
132
133    private String JavaDoc getExpectedWeatherResult()
134    {
135       return "<?xml version='1.0' encoding='UTF-8'?>" +
136              "<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\">" +
137              "<SOAP-ENV:Body>" +
138              "<ns1:getTempResponse xmlns:ns1=\"urn:xmethods-Temperature\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
139              "<return xsi:type=\"xsd:float\">60.0</return>" +
140              "</ns1:getTempResponse>" +
141              "\n" +
142              "</SOAP-ENV:Body>\n" +
143              "</SOAP-ENV:Envelope>";
144    }
145
146    private String JavaDoc getExpectedCityResult()
147    {
148       return "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
149              "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
150              "<soap:Body><GetCitiesByCountryResponse xmlns=\"http://www.webserviceX.NET\"><GetCitiesByCountryResult>&lt;NewDataSet&gt; " +
151              "&lt;Table&gt; &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Schoenefeld&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
152              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Dresden-Klotzsche&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
153              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Erfurt-Bindersleben&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
154              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Frankfurt / M-Flughafen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
155              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Muenster / Osnabrueck&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
156              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hamburg-Fuhlsbuettel&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
157              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Tempelhof&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
158              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Koeln / Bonn&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
159              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Duesseldorf&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
160              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Munich / Riem&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
161              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Nuernberg&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
162              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Leipzig-Schkeuditz&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
163              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Saarbruecken / Ensheim&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
164              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Stuttgart-Echterdingen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
165              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Berlin-Tegel&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
166              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hannover&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
167              "&lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Bremen&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt; " +
168              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Hahn&lt;/City&gt; &lt;/Table&gt; &lt;Table&gt;" +
169              " &lt;Country&gt;Germany&lt;/Country&gt; &lt;City&gt;Baden Wurttemberg, Neuostheim&lt;/";
170    }
171
172 // public static void main(String[] args)
173
// {
174
// HTTPInvokerClientTest test = new HTTPInvokerClientTest();
175
// test.testHTTPInvocation();
176
// }
177

178 }
Popular Tags