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.dispatch;
019:
020: import java.io.InputStream;
021: import java.net.URL;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.namespace.QName;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamSource;
028: import javax.xml.ws.Dispatch;
029: import javax.xml.ws.Service;
030: import javax.xml.ws.WebServiceException;
031:
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Node;
034:
035: import org.apache.cxf.helpers.XMLUtils;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.apache.hello_world_xml_http.wrapped.XMLService;
038: import org.apache.hello_world_xml_http.wrapped.types.GreetMe;
039: import org.apache.hello_world_xml_http.wrapped.types.GreetMeResponse;
040: import org.apache.hello_world_xml_http.wrapped.types.ObjectFactory;
041: import org.junit.BeforeClass;
042: import org.junit.Test;
043:
044: public class DispatchXMLClientServerTest extends
045: AbstractBusClientServerTestBase {
046: private final QName serviceName = new QName(
047: "http://apache.org/hello_world_xml_http/wrapped",
048: "XMLService");
049: private final QName portName = new QName(
050: "http://apache.org/hello_world_xml_http/wrapped",
051: "XMLDispatchPort");
052:
053: @BeforeClass
054: public static void startServers() throws Exception {
055: assertTrue("server did not launch correctly",
056: launchServer(Server.class));
057: }
058:
059: @Test
060: public void testJAXBMESSAGE() throws Exception {
061: Service service = Service.create(serviceName);
062: assertNotNull(service);
063: service.addPort(portName,
064: "http://cxf.apache.org/bindings/xformat",
065: "http://localhost:9007/XMLService/XMLDispatchPort");
066:
067: GreetMe gm = new GreetMe();
068: gm.setRequestType("CXF");
069: JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
070: Dispatch<Object> disp = service.createDispatch(portName, ctx,
071: Service.Mode.MESSAGE);
072: GreetMeResponse resp = (GreetMeResponse) disp.invoke(gm);
073: assertNotNull(resp);
074: assertEquals("Hello CXF", resp.getResponseType());
075:
076: try {
077: disp.invoke(null);
078: fail("Should have thrown a fault");
079: } catch (WebServiceException ex) {
080: //expected
081: }
082: }
083:
084: @Test
085: public void testStreamSourceMESSAGE() throws Exception {
086: /*URL wsdl = getClass().getResource("/wsdl/hello_world_xml_wrapped.wsdl");
087: assertNotNull(wsdl);
088:
089: XMLService service = new XMLService(wsdl, serviceName);
090: assertNotNull(service);*/
091: Service service = Service.create(serviceName);
092: assertNotNull(service);
093: service.addPort(portName,
094: "http://cxf.apache.org/bindings/xformat",
095: "http://localhost:9007/XMLService/XMLDispatchPort");
096:
097: InputStream is = getClass().getResourceAsStream(
098: "/messages/XML_GreetMeDocLiteralReq.xml");
099: StreamSource reqMsg = new StreamSource(is);
100: assertNotNull(reqMsg);
101:
102: Dispatch<Source> disp = service.createDispatch(portName,
103: Source.class, Service.Mode.MESSAGE);
104: Source source = disp.invoke(reqMsg);
105: assertNotNull(source);
106:
107: String streamString = XMLUtils.toString(source);
108: Document doc = XMLUtils.parse(streamString);
109: assertEquals("greetMeResponse", doc.getFirstChild()
110: .getLocalName());
111: assertEquals("Hello tli", doc.getFirstChild().getTextContent());
112: }
113:
114: @Test
115: public void testDOMSourcePAYLOAD() throws Exception {
116: URL wsdl = getClass().getResource(
117: "/wsdl/hello_world_xml_wrapped.wsdl");
118: assertNotNull(wsdl);
119:
120: XMLService service = new XMLService(wsdl, serviceName);
121: assertNotNull(service);
122:
123: InputStream is = getClass().getResourceAsStream(
124: "/messages/XML_GreetMeDocLiteralReq.xml");
125: Document doc = XMLUtils.parse(is);
126: DOMSource reqMsg = new DOMSource(doc);
127: assertNotNull(reqMsg);
128:
129: Dispatch<DOMSource> disp = service.createDispatch(portName,
130: DOMSource.class, Service.Mode.PAYLOAD);
131: DOMSource result = disp.invoke(reqMsg);
132: assertNotNull(result);
133:
134: Node respDoc = result.getNode();
135: assertEquals("greetMeResponse", respDoc.getFirstChild()
136: .getLocalName());
137: assertEquals("Hello tli", respDoc.getFirstChild()
138: .getTextContent());
139: }
140: }
|