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;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022: import java.net.URL;
023: import java.util.Map;
024: import java.util.ResourceBundle;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.ws.BindingProvider;
028: import javax.xml.ws.WebServiceException;
029:
030: import org.apache.cxf.endpoint.ClientImpl;
031: import org.apache.cxf.interceptor.Fault;
032: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
033: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
034: import org.apache.cxf.message.Message;
035: import org.apache.cxf.phase.AbstractPhaseInterceptor;
036: import org.apache.cxf.phase.Phase;
037: import org.apache.cxf.service.Service;
038: import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
039: import org.apache.cxf.service.invoker.BeanInvoker;
040: import org.apache.cxf.service.model.BindingOperationInfo;
041: import org.apache.cxf.service.model.EndpointInfo;
042: import org.apache.cxf.service.model.MessagePartInfo;
043: import org.apache.cxf.transport.Destination;
044: import org.apache.hello_world_soap_http.BadRecordLitFault;
045: import org.apache.hello_world_soap_http.Greeter;
046: import org.apache.hello_world_soap_http.GreeterImpl;
047: import org.junit.Before;
048: import org.junit.Test;
049:
050: public class JaxWsClientTest extends AbstractJaxWsTest {
051:
052: private final QName serviceName = new QName(
053: "http://apache.org/hello_world_soap_http", "SOAPService");
054: private final QName portName = new QName(
055: "http://apache.org/hello_world_soap_http", "SoapPort");
056: private final String address = "http://localhost:9000/SoapContext/SoapPort";
057: private Destination d;
058:
059: @Before
060: public void setUp() throws Exception {
061: super .setUpBus();
062:
063: EndpointInfo ei = new EndpointInfo(null,
064: "http://schemas.xmlsoap.org/soap/http");
065: ei.setAddress(address);
066:
067: d = localTransport.getDestination(ei);
068: }
069:
070: @Test
071: public void testCreate() throws Exception {
072: javax.xml.ws.Service s = javax.xml.ws.Service.create(new QName(
073: "http://apache.org/hello_world_soap_http", "SoapPort"));
074: assertNotNull(s);
075:
076: try {
077: s = javax.xml.ws.Service.create(new URL(
078: "file:/does/not/exist.wsdl"), new QName(
079: "http://apache.org/hello_world_soap_http",
080: "SoapPort"));
081: fail("did not throw exception");
082: } catch (WebServiceException sce) {
083: // ignore, this is expected
084: }
085: }
086:
087: @Test
088: public void testRequestContext() throws Exception {
089: URL url = getClass().getResource("/wsdl/hello_world.wsdl");
090: javax.xml.ws.Service s = javax.xml.ws.Service.create(url,
091: serviceName);
092: Greeter greeter = s.getPort(portName, Greeter.class);
093: InvocationHandler handler = Proxy.getInvocationHandler(greeter);
094: BindingProvider bp = null;
095:
096: if (handler instanceof BindingProvider) {
097: bp = (BindingProvider) handler;
098: //System.out.println(bp.toString());
099: Map<String, Object> requestContext = bp.getRequestContext();
100: String reqAddr = (String) requestContext
101: .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
102: assertEquals(
103: "the address get from requestContext is not equal",
104: address, reqAddr);
105: } else {
106: fail("can't get the requset context");
107: }
108: }
109:
110: @Test
111: public void testEndpoint() throws Exception {
112: ReflectionServiceFactoryBean bean = new JaxWsServiceFactoryBean();
113: URL resource = getClass().getResource("/wsdl/hello_world.wsdl");
114: assertNotNull(resource);
115: bean.setWsdlURL(resource.toString());
116: bean.setBus(getBus());
117: bean.setServiceClass(GreeterImpl.class);
118: GreeterImpl greeter = new GreeterImpl();
119: BeanInvoker invoker = new BeanInvoker(greeter);
120: bean.setInvoker(invoker);
121:
122: Service service = bean.create();
123:
124: String namespace = "http://apache.org/hello_world_soap_http";
125: EndpointInfo ei = service.getServiceInfos().get(0).getEndpoint(
126: new QName(namespace, "SoapPort"));
127: JaxWsEndpointImpl endpoint = new JaxWsEndpointImpl(getBus(),
128: service, ei);
129:
130: ClientImpl client = new ClientImpl(getBus(), endpoint);
131:
132: BindingOperationInfo bop = ei.getBinding().getOperation(
133: new QName(namespace, "sayHi"));
134: assertNotNull(bop);
135: bop = bop.getUnwrappedOperation();
136: assertNotNull(bop);
137:
138: MessagePartInfo part = bop.getOutput().getMessageParts().get(0);
139: assertEquals(0, part.getIndex());
140:
141: d.setMessageObserver(new MessageReplayObserver(
142: "sayHiResponse.xml"));
143: Object ret[] = client.invoke(bop, new Object[] { "hi" }, null);
144: assertNotNull(ret);
145: assertEquals("Wrong number of return objects", 1, ret.length);
146:
147: // test fault handling
148: bop = ei.getBinding().getOperation(
149: new QName(namespace, "testDocLitFault"));
150: bop = bop.getUnwrappedOperation();
151: d.setMessageObserver(new MessageReplayObserver(
152: "testDocLitFault.xml"));
153: try {
154: client.invoke(bop, new Object[] { "BadRecordLitFault" },
155: null);
156: fail("Should have returned a fault!");
157: } catch (BadRecordLitFault fault) {
158: assertEquals("foo", fault.getFaultInfo().trim());
159: assertEquals("Hadrian did it.", fault.getMessage());
160: }
161:
162: try {
163: client.getEndpoint().getOutInterceptors().add(
164: new NestedFaultThrower());
165: client.getEndpoint().getOutInterceptors().add(
166: new FaultThrower());
167: client.invoke(bop, new Object[] { "BadRecordLitFault" },
168: null);
169: fail("Should have returned a fault!");
170: } catch (Fault fault) {
171: assertEquals(true, fault.getMessage().indexOf("Foo") >= 0);
172: }
173:
174: }
175:
176: public static class NestedFaultThrower extends
177: AbstractPhaseInterceptor<Message> {
178:
179: public NestedFaultThrower() {
180: super (Phase.PRE_LOGICAL);
181: addBefore(FaultThrower.class.getName());
182: }
183:
184: public void handleMessage(Message message) throws Fault {
185: boolean result = message.getInterceptorChain().doIntercept(
186: message);
187: assertEquals("doIntercept not return false", result, false);
188: assertNotNull(message.getContent(Exception.class));
189: throw new Fault(message.getContent(Exception.class));
190: }
191:
192: }
193:
194: public static class FaultThrower extends
195: AbstractPhaseInterceptor<Message> {
196:
197: public FaultThrower() {
198: super (Phase.PRE_LOGICAL);
199: }
200:
201: public void handleMessage(Message message) throws Fault {
202: throw new Fault(new org.apache.cxf.common.i18n.Message(
203: "Foo", (ResourceBundle) null));
204: }
205:
206: }
207:
208: }
|