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.jaxws.dispatch;
019:
020: import java.net.URL;
021:
022: import javax.xml.bind.JAXBContext;
023: import javax.xml.namespace.QName;
024: import javax.xml.transform.Source;
025: import javax.xml.transform.dom.DOMSource;
026: import javax.xml.ws.Dispatch;
027: import javax.xml.ws.Service;
028: import javax.xml.ws.http.HTTPBinding;
029: import javax.xml.ws.soap.SOAPBinding;
030: import javax.xml.ws.soap.SOAPFaultException;
031:
032: import org.w3c.dom.Document;
033:
034: import org.apache.cxf.helpers.DOMUtils;
035: import org.apache.cxf.jaxws.AbstractJaxWsTest;
036: import org.apache.cxf.jaxws.MessageReplayObserver;
037: import org.apache.cxf.jaxws.ServiceImpl;
038: import org.apache.cxf.service.model.EndpointInfo;
039: import org.apache.cxf.transport.Destination;
040: import org.apache.hello_world_soap_http.SOAPService;
041: import org.apache.hello_world_soap_http.types.SayHi;
042: import org.apache.hello_world_soap_http.types.SayHiResponse;
043: import org.junit.Before;
044: import org.junit.Test;
045:
046: public class DispatchTest extends AbstractJaxWsTest {
047: private final QName serviceName = new QName(
048: "http://apache.org/hello_world_soap_http", "SOAPService");
049:
050: private final QName portName = new QName(
051: "http://apache.org/hello_world_soap_http", "SoapPort");
052:
053: private final String address = "http://localhost:9000/SoapContext/SoapPort";
054:
055: private Destination d;
056:
057: @Before
058: public void setUp() throws Exception {
059: EndpointInfo ei = new EndpointInfo(null,
060: "http://schemas.xmlsoap.org/soap/http");
061: ei.setAddress(address);
062:
063: d = localTransport.getDestination(ei);
064: }
065:
066: @Test
067: public void testJAXB() throws Exception {
068: d.setMessageObserver(new MessageReplayObserver(
069: "/org/apache/cxf/jaxws/sayHiResponse.xml"));
070:
071: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
072: assertNotNull(wsdl);
073:
074: SOAPService service = new SOAPService(wsdl, serviceName);
075: assertNotNull(service);
076:
077: JAXBContext jc = JAXBContext
078: .newInstance("org.apache.hello_world_soap_http.types");
079: Dispatch<Object> disp = service.createDispatch(portName, jc,
080: Service.Mode.PAYLOAD);
081:
082: SayHi s = new SayHi();
083:
084: Object response = disp.invoke(s);
085: assertNotNull(response);
086: assertTrue(response instanceof SayHiResponse);
087: }
088:
089: @Test
090: public void testDOMSource() throws Exception {
091: ServiceImpl service = new ServiceImpl(getBus(), getClass()
092: .getResource("/wsdl/hello_world.wsdl"), serviceName,
093: null);
094:
095: Dispatch<Source> disp = service.createDispatch(portName,
096: Source.class, Service.Mode.MESSAGE);
097: disp.getRequestContext().put(
098: Dispatch.ENDPOINT_ADDRESS_PROPERTY, address);
099:
100: d.setMessageObserver(new MessageReplayObserver(
101: "/org/apache/cxf/jaxws/sayHiResponse.xml"));
102:
103: Document doc = DOMUtils
104: .readXml(getResourceAsStream("/org/apache/cxf/jaxws/sayHi.xml"));
105: DOMSource source = new DOMSource(doc);
106: Source res = disp.invoke(source);
107: assertNotNull(res);
108:
109: }
110:
111: @Test
112: public void testHTTPBinding() throws Exception {
113: ServiceImpl service = new ServiceImpl(getBus(), null,
114: serviceName, null);
115: service.addPort(portName, HTTPBinding.HTTP_BINDING,
116: "local://foobar");
117: Dispatch<Source> disp = service.createDispatch(portName,
118: Source.class, Service.Mode.MESSAGE);
119: assertTrue(disp.getBinding() instanceof HTTPBinding);
120: }
121:
122: @Test
123: public void testSOAPPBinding() throws Exception {
124: ServiceImpl service = new ServiceImpl(getBus(), null,
125: serviceName, null);
126: service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,
127: "local://foobar");
128: Dispatch<Source> disp = service.createDispatch(portName,
129: Source.class, Service.Mode.MESSAGE);
130: assertTrue(disp.getBinding() instanceof SOAPBinding);
131: }
132:
133: @Test
134: public void testSOAPPBindingNullMessage() throws Exception {
135: d.setMessageObserver(new MessageReplayObserver(
136: "/org/apache/cxf/jaxws/sayHiResponse.xml"));
137:
138: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
139: assertNotNull(wsdl);
140:
141: SOAPService service = new SOAPService(wsdl, serviceName);
142: assertNotNull(service);
143:
144: JAXBContext jc = JAXBContext
145: .newInstance("org.apache.hello_world_soap_http.types");
146: Dispatch<Object> disp = service.createDispatch(portName, jc,
147: Service.Mode.PAYLOAD);
148: try {
149: // Send a null message
150: disp.invoke(null);
151: } catch (SOAPFaultException e) {
152: //Passed
153: return;
154: }
155:
156: fail("SOAPFaultException was not thrown");
157: }
158: }
|