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.support;
019:
020: import java.lang.reflect.Method;
021:
022: import java.util.Iterator;
023:
024: import javax.jws.WebMethod;
025: import javax.jws.WebService;
026: import javax.jws.soap.SOAPBinding;
027: import javax.wsdl.Definition;
028: import javax.wsdl.Service;
029: import javax.wsdl.factory.WSDLFactory;
030: import javax.wsdl.xml.WSDLReader;
031: import javax.xml.namespace.QName;
032:
033: import org.apache.cxf.Bus;
034: import org.apache.cxf.binding.BindingFactoryManager;
035: import org.apache.cxf.service.model.MessageInfo;
036: import org.apache.cxf.service.model.OperationInfo;
037: import org.apache.cxf.service.model.ServiceInfo;
038: import org.apache.cxf.transport.DestinationFactoryManager;
039: import org.apache.cxf.wsdl11.WSDLLocatorImpl;
040: import org.apache.cxf.wsdl11.WSDLServiceBuilder;
041: import org.easymock.classextension.EasyMock;
042: import org.easymock.classextension.IMocksControl;
043: import org.junit.Assert;
044: import org.junit.Before;
045: import org.junit.Test;
046:
047: public class JaxWsServiceConfigurationTest extends Assert {
048:
049: @Before
050: public void setUp() throws Exception {
051:
052: }
053:
054: @Test
055: public void testGetInPartName() throws Exception {
056: QName opName = new QName("http://cxf.com/", "sayHello");
057: Method sayHelloMethod = Hello.class.getMethod("sayHello",
058: new Class[] { String.class, String.class });
059: ServiceInfo si = getMockedServiceModel("/wsdl/default_partname_test.wsdl");
060: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
061: bean.setServiceClass(Hello.class);
062: JaxWsServiceConfiguration jwsc = (JaxWsServiceConfiguration) bean
063: .getServiceConfigurations().get(0);
064: jwsc.setServiceFactory(bean);
065:
066: OperationInfo op = si.getInterface().getOperation(opName);
067: op.setInput("input", new MessageInfo(op, new QName(
068: "http://cxf.com/", "input")));
069: op.setOutput("output", new MessageInfo(op, new QName(
070: "http://cxf.com/", "output")));
071:
072: QName partName = jwsc.getInPartName(op, sayHelloMethod, 0);
073: assertEquals("get wrong in partName for first param",
074: new QName("http://cxf.com/", "arg0"), partName);
075:
076: op.getInput().addMessagePart(new QName("arg0"));
077:
078: partName = jwsc.getInPartName(op, sayHelloMethod, 1);
079: assertEquals("get wrong in partName for first param",
080: new QName("http://cxf.com/", "arg1"), partName);
081: }
082:
083: @Test
084: public void testDefaultStyle() throws Exception {
085: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
086: bean.setServiceClass(HelloDefault.class);
087: JaxWsServiceConfiguration jwsc = (JaxWsServiceConfiguration) bean
088: .getServiceConfigurations().get(0);
089: jwsc.setServiceFactory(bean);
090:
091: assertEquals("document", jwsc.getStyle());
092: assertNull(jwsc.isWrapped());
093: }
094:
095: @Test
096: public void testRPCStyle() throws Exception {
097: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
098: bean.setServiceClass(HelloRPC.class);
099: JaxWsServiceConfiguration jwsc = (JaxWsServiceConfiguration) bean
100: .getServiceConfigurations().get(0);
101: jwsc.setServiceFactory(bean);
102:
103: assertEquals("rpc", jwsc.getStyle());
104: assertFalse(jwsc.isWrapped());
105: }
106:
107: @Test
108: public void testDocumentWrappedStyle() throws Exception {
109: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
110: bean.setServiceClass(HelloWrapped.class);
111: JaxWsServiceConfiguration jwsc = (JaxWsServiceConfiguration) bean
112: .getServiceConfigurations().get(0);
113: jwsc.setServiceFactory(bean);
114:
115: assertEquals("document", jwsc.getStyle());
116: assertTrue(jwsc.isWrapped());
117: }
118:
119: @Test
120: public void testDocumentBareStyle() throws Exception {
121: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
122: bean.setServiceClass(HelloBare.class);
123: JaxWsServiceConfiguration jwsc = (JaxWsServiceConfiguration) bean
124: .getServiceConfigurations().get(0);
125: jwsc.setServiceFactory(bean);
126:
127: assertEquals("document", jwsc.getStyle());
128: assertFalse(jwsc.isWrapped());
129: }
130:
131: @Test
132: public void testGetOutPartName() throws Exception {
133: QName opName = new QName("http://cxf.com/", "sayHi");
134: Method sayHiMethod = Hello.class.getMethod("sayHi",
135: new Class[] {});
136: ServiceInfo si = getMockedServiceModel("/wsdl/default_partname_test.wsdl");
137: JaxWsServiceConfiguration jwsc = new JaxWsServiceConfiguration();
138: JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
139: bean.setServiceClass(Hello.class);
140: jwsc.setServiceFactory(bean);
141:
142: // clear the output
143: OperationInfo op = si.getInterface().getOperation(opName);
144: op.setOutput("output", new MessageInfo(op, new QName(
145: "http://cxf.com/", "output")));
146:
147: QName partName = jwsc.getOutPartName(op, sayHiMethod, -1);
148: assertEquals("get wrong return partName", new QName(
149: "http://cxf.com/", "return"), partName);
150: }
151:
152: private ServiceInfo getMockedServiceModel(String wsdlUrl)
153: throws Exception {
154: WSDLReader wsdlReader = WSDLFactory.newInstance()
155: .newWSDLReader();
156: wsdlReader.setFeature("javax.wsdl.verbose", false);
157: Definition def = wsdlReader.readWSDL(new WSDLLocatorImpl(
158: wsdlUrl));
159:
160: IMocksControl control = EasyMock.createNiceControl();
161: Bus bus = control.createMock(Bus.class);
162: BindingFactoryManager bindingFactoryManager = control
163: .createMock(BindingFactoryManager.class);
164: DestinationFactoryManager dfm = control
165: .createMock(DestinationFactoryManager.class);
166: WSDLServiceBuilder wsdlServiceBuilder = new WSDLServiceBuilder(
167: bus);
168:
169: Service service = null;
170: for (Iterator<?> it = def.getServices().values().iterator(); it
171: .hasNext();) {
172: Object obj = it.next();
173: if (obj instanceof Service) {
174: service = (Service) obj;
175: break;
176: }
177: }
178:
179: EasyMock.expect(bus.getExtension(BindingFactoryManager.class))
180: .andReturn(bindingFactoryManager);
181: EasyMock.expect(
182: bus.getExtension(DestinationFactoryManager.class))
183: .andStubReturn(dfm);
184: control.replay();
185:
186: ServiceInfo serviceInfo = wsdlServiceBuilder.buildServices(def,
187: service).get(0);
188: serviceInfo.setProperty(WSDLServiceBuilder.WSDL_DEFINITION,
189: null);
190: serviceInfo.setProperty(WSDLServiceBuilder.WSDL_SERVICE, null);
191: return serviceInfo;
192: }
193:
194: @WebService(name="Hello",targetNamespace="http://cxf.com/")
195: @SOAPBinding(parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.BARE,style=javax.jws.soap.SOAPBinding.Style.RPC,use=javax.jws.soap.SOAPBinding.Use.LITERAL)
196: public interface Hello {
197: @WebMethod(operationName="sayHi",exclude=false)
198: String sayHi();
199:
200: @WebMethod(operationName="sayHello",exclude=false)
201: String sayHello(String asdf1, String asdf2);
202: }
203:
204: @WebService(name="Hello",targetNamespace="http://cxf.com/")
205: public interface HelloDefault {
206: @WebMethod(operationName="sayHi",exclude=false)
207: String sayHi();
208:
209: @WebMethod(operationName="sayHello",exclude=false)
210: String sayHello(String asdf1, String asdf2);
211: }
212:
213: @SOAPBinding(style=javax.jws.soap.SOAPBinding.Style.RPC)
214: public interface HelloRPC {
215: String sayHi();
216: }
217:
218: @SOAPBinding(style=javax.jws.soap.SOAPBinding.Style.DOCUMENT)
219: public interface HelloWrapped {
220: String sayHi();
221: }
222:
223: @SOAPBinding(parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.BARE,style=javax.jws.soap.SOAPBinding.Style.DOCUMENT)
224: public interface HelloBare {
225: String sayHi();
226: }
227: }
|