001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.jaxws;
019:
020: import java.io.InputStream;
021: import java.lang.reflect.UndeclaredThrowableException;
022: import java.net.HttpURLConnection;
023: import java.net.URL;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.xml.namespace.QName;
028: import javax.xml.ws.BindingProvider;
029: import javax.xml.ws.Holder;
030: import javax.xml.ws.Service;
031: import javax.xml.ws.http.HTTPException;
032: import javax.xml.xpath.XPathConstants;
033:
034: import org.w3c.dom.Document;
035:
036: import org.apache.cxf.helpers.XMLUtils;
037: import org.apache.cxf.helpers.XPathUtils;
038: import org.apache.cxf.message.Message;
039: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
040: import org.apache.headers.HeaderTester;
041: import org.apache.headers.XMLHeaderService;
042: import org.apache.headers.types.InHeader;
043: import org.apache.headers.types.InHeaderResponse;
044: import org.apache.headers.types.InoutHeader;
045: import org.apache.headers.types.InoutHeaderResponse;
046: import org.apache.headers.types.OutHeader;
047: import org.apache.headers.types.OutHeaderResponse;
048: import org.apache.headers.types.SOAPHeaderData;
049: import org.apache.hello_world_xml_http.bare.Greeter;
050: import org.apache.hello_world_xml_http.bare.XMLService;
051: import org.apache.hello_world_xml_http.bare.types.MyComplexStructType;
052: import org.apache.hello_world_xml_http.mixed.types.SayHi;
053: import org.apache.hello_world_xml_http.mixed.types.SayHiResponse;
054: import org.apache.hello_world_xml_http.wrapped.GreeterFaultImpl;
055: import org.apache.hello_world_xml_http.wrapped.PingMeFault;
056: import org.junit.BeforeClass;
057: import org.junit.Test;
058:
059: public class ClientServerXMLTest extends
060: AbstractBusClientServerTestBase {
061:
062: private final QName barePortName = new QName(
063: "http://apache.org/hello_world_xml_http/bare", "XMLPort");
064:
065: private final QName wrapServiceName = new QName(
066: "http://apache.org/hello_world_xml_http/wrapped",
067: "XMLService");
068:
069: private final QName mixedServiceName = new QName(
070: "http://apache.org/hello_world_xml_http/mixed",
071: "XMLService");
072:
073: private final QName wrapPortName = new QName(
074: "http://apache.org/hello_world_xml_http/wrapped", "XMLPort");
075:
076: private final QName mixedPortName = new QName(
077: "http://apache.org/hello_world_xml_http/mixed", "XMLPort");
078:
079: private final QName wrapFakePortName = new QName(
080: "http://apache.org/hello_world_xml_http/wrapped",
081: "FakePort");
082:
083: @BeforeClass
084: public static void startServers() throws Exception {
085: assertTrue("server did not launch correctly",
086: launchServer(ServerXMLBinding.class));
087: }
088:
089: @Test
090: public void testBareBasicConnection() throws Exception {
091:
092: XMLService service = new XMLService();
093: assertNotNull(service);
094:
095: String response1 = "Hello ";
096: String response2 = "Bonjour";
097: try {
098: Greeter greeter = service.getPort(barePortName,
099: Greeter.class);
100: String username = System.getProperty("user.name");
101: String reply = greeter.greetMe(username);
102:
103: assertNotNull("no response received from service", reply);
104: assertEquals(response1 + username, reply);
105:
106: reply = greeter.sayHi();
107: assertNotNull("no response received from service", reply);
108: assertEquals(response2, reply);
109:
110: MyComplexStructType argument = new MyComplexStructType();
111: MyComplexStructType retVal = null;
112:
113: String str1 = "this is element 1";
114: String str2 = "this is element 2";
115: int int1 = 42;
116: argument.setElem1(str1);
117: argument.setElem2(str2);
118: argument.setElem3(int1);
119: retVal = greeter.sendReceiveData(argument);
120:
121: assertEquals(str1, retVal.getElem1());
122: assertEquals(str2, retVal.getElem2());
123: assertEquals(int1, retVal.getElem3());
124:
125: } catch (UndeclaredThrowableException ex) {
126: throw (Exception) ex.getCause();
127: }
128: }
129:
130: @Test
131: public void testBareGetGreetMe() throws Exception {
132: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9031/XMLService/XMLPort/greetMe/requestType/cxf");
133: httpConnection.connect();
134:
135: assertEquals(200, httpConnection.getResponseCode());
136:
137: assertEquals("text/xml; charset=utf-8", httpConnection
138: .getContentType());
139: assertEquals("OK", httpConnection.getResponseMessage());
140:
141: InputStream in = httpConnection.getInputStream();
142: assertNotNull(in);
143:
144: Document doc = XMLUtils.parse(in);
145: assertNotNull(doc);
146:
147: Map<String, String> ns = new HashMap<String, String>();
148: ns.put("ns2",
149: "http://apache.org/hello_world_xml_http/bare/types");
150: XPathUtils xu = new XPathUtils(ns);
151: String response = (String) xu
152: .getValue("//ns2:responseType/text()", doc,
153: XPathConstants.STRING);
154: assertEquals("Hello cxf", response);
155: }
156:
157: @Test
158: public void testWrapBasicConnection() throws Exception {
159:
160: org.apache.hello_world_xml_http.wrapped.XMLService service = new org.apache.hello_world_xml_http.wrapped.XMLService(
161: this .getClass().getResource(
162: "/wsdl/hello_world_xml_wrapped.wsdl"),
163: wrapServiceName);
164: assertNotNull(service);
165:
166: String response1 = new String("Hello ");
167: String response2 = new String("Bonjour");
168: try {
169: org.apache.hello_world_xml_http.wrapped.Greeter greeter = service
170: .getPort(
171: wrapPortName,
172: org.apache.hello_world_xml_http.wrapped.Greeter.class);
173: String username = System.getProperty("user.name");
174: String reply = greeter.greetMe(username);
175:
176: assertNotNull("no response received from service", reply);
177: assertEquals(response1 + username, reply);
178:
179: reply = greeter.sayHi();
180: assertNotNull("no response received from service", reply);
181: assertEquals(response2, reply);
182:
183: greeter.greetMeOneWay(System.getProperty("user.name"));
184:
185: } catch (UndeclaredThrowableException ex) {
186: throw (Exception) ex.getCause();
187: }
188: }
189:
190: @Test
191: public void testMixedConnection() throws Exception {
192:
193: org.apache.hello_world_xml_http.mixed.XMLService service = new org.apache.hello_world_xml_http.mixed.XMLService(
194: this .getClass().getResource(
195: "/wsdl/hello_world_xml_mixed.wsdl"),
196: mixedServiceName);
197: assertNotNull(service);
198:
199: String response1 = new String("Hello ");
200: String response2 = new String("Bonjour");
201: try {
202: org.apache.hello_world_xml_http.mixed.Greeter greeter = service
203: .getPort(
204: mixedPortName,
205: org.apache.hello_world_xml_http.mixed.Greeter.class);
206: String username = System.getProperty("user.name");
207: String reply = greeter.greetMe(username);
208:
209: assertNotNull("no response received from service", reply);
210: assertEquals(response1 + username, reply);
211:
212: SayHi request = new SayHi();
213:
214: SayHiResponse response = greeter.sayHi1(request);
215: assertNotNull("no response received from service", response);
216: assertEquals(response2, response.getResponseType());
217:
218: greeter.greetMeOneWay(System.getProperty("user.name"));
219:
220: } catch (UndeclaredThrowableException ex) {
221: throw (Exception) ex.getCause();
222: }
223: }
224:
225: @Test
226: public void testAddPort() throws Exception {
227: URL url = getClass().getResource(
228: "/wsdl/hello_world_xml_wrapped.wsdl");
229:
230: Service service = Service.create(url, wrapServiceName);
231: assertNotNull(service);
232:
233: service.addPort(wrapFakePortName,
234: "http://cxf.apache.org/bindings/xformat",
235: "http://localhost:9032/XMLService/XMLPort");
236:
237: String response1 = new String("Hello ");
238: String response2 = new String("Bonjour");
239:
240: org.apache.hello_world_xml_http.wrapped.Greeter greeter = service
241: .getPort(
242: wrapPortName,
243: org.apache.hello_world_xml_http.wrapped.Greeter.class);
244:
245: try {
246: String username = System.getProperty("user.name");
247: String reply = greeter.greetMe(username);
248:
249: assertNotNull("no response received from service", reply);
250: assertEquals(response1 + username, reply);
251:
252: reply = greeter.sayHi();
253: assertNotNull("no response received from service", reply);
254: assertEquals(response2, reply);
255:
256: BindingProvider bp = (BindingProvider) greeter;
257: Map<String, Object> responseContext = bp
258: .getResponseContext();
259: Integer responseCode = (Integer) responseContext
260: .get(Message.RESPONSE_CODE);
261: assertEquals(200, responseCode.intValue());
262:
263: greeter.greetMeOneWay(System.getProperty("user.name"));
264:
265: } catch (UndeclaredThrowableException ex) {
266: throw (Exception) ex.getCause();
267: }
268:
269: }
270:
271: @Test
272: public void testXMLFault() throws Exception {
273: org.apache.hello_world_xml_http.wrapped.XMLService service = new org.apache.hello_world_xml_http.wrapped.XMLService(
274: this .getClass().getResource(
275: "/wsdl/hello_world_xml_wrapped.wsdl"),
276: wrapServiceName);
277: assertNotNull(service);
278: org.apache.hello_world_xml_http.wrapped.Greeter greeter = service
279: .getPort(
280: wrapPortName,
281: org.apache.hello_world_xml_http.wrapped.Greeter.class);
282: try {
283: greeter.pingMe();
284: fail("did not catch expected PingMeFault exception");
285: } catch (PingMeFault ex) {
286: assertEquals("minor value", 1, (int) ex.getFaultInfo()
287: .getMinor());
288: assertEquals("major value", 2, (int) ex.getFaultInfo()
289: .getMajor());
290:
291: BindingProvider bp = (BindingProvider) greeter;
292: Map<String, Object> responseContext = bp
293: .getResponseContext();
294: String contentType = (String) responseContext
295: .get(Message.CONTENT_TYPE);
296: assertEquals("text/xml; charset=utf-8", contentType);
297: Integer responseCode = (Integer) responseContext
298: .get(Message.RESPONSE_CODE);
299: assertEquals(500, responseCode.intValue());
300: }
301:
302: org.apache.hello_world_xml_http.wrapped.Greeter greeterFault = service
303: .getXMLFaultPort();
304: try {
305: greeterFault.pingMe();
306: fail("did not catch expected runtime exception");
307: } catch (HTTPException ex) {
308: assertTrue(
309: "check expected message of exception",
310: ex.getCause().getMessage().indexOf(
311: GreeterFaultImpl.RUNTIME_EXCEPTION_MESSAGE) >= 0);
312: }
313: }
314:
315: @Test
316: public void testXMLBindingOfSoapHeaderWSDL() throws Exception {
317: XMLHeaderService service = new XMLHeaderService();
318: HeaderTester port = service.getXMLPort9000();
319: try {
320: verifyInHeader(port);
321: verifyInOutHeader(port);
322: verifyOutHeader(port);
323: } catch (UndeclaredThrowableException ex) {
324: throw (Exception) ex.getCause();
325: }
326: }
327:
328: public void verifyInHeader(HeaderTester proxy) throws Exception {
329: InHeader me = new InHeader();
330: me.setRequestType("InHeaderRequest");
331: SOAPHeaderData headerInfo = new SOAPHeaderData();
332: headerInfo.setMessage("message");
333: headerInfo.setOriginator("originator");
334: InHeaderResponse resp = proxy.inHeader(me, headerInfo);
335: assertNotNull(resp);
336: assertEquals("check returned response type",
337: "requestType=InHeaderRequest"
338: + "\nheaderData.message=message"
339: + "\nheaderData.getOriginator=originator", resp
340: .getResponseType());
341: }
342:
343: public void verifyInOutHeader(HeaderTester proxy) throws Exception {
344: InoutHeader me = new InoutHeader();
345: me.setRequestType("InoutHeaderRequest");
346: SOAPHeaderData headerInfo = new SOAPHeaderData();
347: headerInfo.setMessage("inoutMessage");
348: headerInfo.setOriginator("inoutOriginator");
349: Holder<SOAPHeaderData> holder = new Holder<SOAPHeaderData>();
350: holder.value = headerInfo;
351: InoutHeaderResponse resp = proxy.inoutHeader(me, holder);
352: assertNotNull(resp);
353: assertEquals("check return value",
354: "requestType=InoutHeaderRequest", resp
355: .getResponseType());
356:
357: assertEquals("check inout value", "message=inoutMessage",
358: holder.value.getMessage());
359: assertEquals("check inout value", "orginator=inoutOriginator",
360: holder.value.getOriginator());
361: }
362:
363: public void verifyOutHeader(HeaderTester proxy) throws Exception {
364: OutHeader me = new OutHeader();
365: me.setRequestType("OutHeaderRequest");
366:
367: Holder<OutHeaderResponse> outHeaderHolder = new Holder<OutHeaderResponse>();
368: Holder<SOAPHeaderData> soapHeaderHolder = new Holder<SOAPHeaderData>();
369: proxy.outHeader(me, outHeaderHolder, soapHeaderHolder);
370: assertNotNull(outHeaderHolder.value);
371: assertNotNull(soapHeaderHolder.value);
372: assertEquals("check out value", "requestType=OutHeaderRequest",
373: outHeaderHolder.value.getResponseType());
374:
375: assertEquals("check out value", "message=outMessage",
376: soapHeaderHolder.value.getMessage());
377:
378: assertEquals("check out value", "orginator=outOriginator",
379: soapHeaderHolder.value.getOriginator());
380:
381: }
382:
383: }
|