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.wsdl11;
019:
020: import java.util.Collection;
021: import java.util.logging.Logger;
022:
023: import javax.wsdl.Binding;
024: import javax.wsdl.Definition;
025: import javax.wsdl.Fault;
026: import javax.wsdl.Input;
027: import javax.wsdl.Message;
028: import javax.wsdl.Operation;
029: import javax.wsdl.Output;
030: import javax.wsdl.PortType;
031: import javax.wsdl.Service;
032: import javax.wsdl.Types;
033: import javax.wsdl.extensions.ExtensibilityElement;
034: import javax.wsdl.extensions.schema.Schema;
035: import javax.wsdl.factory.WSDLFactory;
036: import javax.wsdl.xml.WSDLReader;
037: import javax.xml.namespace.QName;
038:
039: import org.w3c.dom.Element;
040:
041: import org.apache.cxf.Bus;
042: import org.apache.cxf.binding.BindingFactoryManager;
043: import org.apache.cxf.helpers.CastUtils;
044: import org.apache.cxf.service.model.ServiceInfo;
045: import org.apache.cxf.transport.DestinationFactory;
046: import org.apache.cxf.transport.DestinationFactoryManager;
047: import org.apache.cxf.wsdl.WSDLManager;
048: import org.apache.ws.commons.schema.XmlSchemaCollection;
049: import org.easymock.classextension.EasyMock;
050: import org.easymock.classextension.IMocksControl;
051: import org.junit.After;
052: import org.junit.Assert;
053: import org.junit.Before;
054: import org.junit.Test;
055:
056: public class ServiceWSDLBuilderTest extends Assert {
057:
058: private static final Logger LOG = Logger
059: .getLogger(ServiceWSDLBuilderTest.class.getName());
060: private static final String WSDL_PATH = "hello_world.wsdl";
061:
062: private Definition def;
063: private Definition newDef;
064: private Service service;
065:
066: private WSDLServiceBuilder wsdlServiceBuilder;
067: private ServiceInfo serviceInfo;
068:
069: private IMocksControl control;
070: private Bus bus;
071: private BindingFactoryManager bindingFactoryManager;
072: private DestinationFactoryManager destinationFactoryManager;
073: private DestinationFactory destinationFactory;
074:
075: @Before
076: public void setUp() throws Exception {
077:
078: String wsdlUrl = getClass().getResource(WSDL_PATH).toString();
079: LOG.info("the path of wsdl file is " + wsdlUrl);
080: WSDLFactory wsdlFactory = WSDLFactory.newInstance();
081: WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
082: wsdlReader.setFeature("javax.wsdl.verbose", false);
083: def = wsdlReader.readWSDL(wsdlUrl);
084:
085: control = EasyMock.createNiceControl();
086: bus = control.createMock(Bus.class);
087: bindingFactoryManager = control
088: .createMock(BindingFactoryManager.class);
089: destinationFactoryManager = control
090: .createMock(DestinationFactoryManager.class);
091: destinationFactory = control
092: .createMock(DestinationFactory.class);
093: wsdlServiceBuilder = new WSDLServiceBuilder(bus);
094:
095: for (Service serv : CastUtils.cast(def.getServices().values(),
096: Service.class)) {
097: if (serv != null) {
098: service = serv;
099: break;
100: }
101: }
102: EasyMock.expect(bus.getExtension(WSDLManager.class)).andReturn(
103: new WSDLManagerImpl()).anyTimes();
104:
105: EasyMock.expect(bus.getExtension(BindingFactoryManager.class))
106: .andReturn(bindingFactoryManager);
107: EasyMock.expect(
108: bus.getExtension(DestinationFactoryManager.class))
109: .andReturn(destinationFactoryManager);
110:
111: EasyMock
112: .expect(
113: destinationFactoryManager
114: .getDestinationFactory("http://schemas.xmlsoap.org/wsdl/soap/"))
115: .andReturn(destinationFactory);
116:
117: control.replay();
118:
119: serviceInfo = wsdlServiceBuilder.buildServices(def, service)
120: .get(0);
121: serviceInfo.setProperty(WSDLServiceBuilder.WSDL_DEFINITION,
122: null);
123: serviceInfo.setProperty(WSDLServiceBuilder.WSDL_SERVICE, null);
124: newDef = new ServiceWSDLBuilder(bus, serviceInfo).build();
125:
126: }
127:
128: @After
129: public void tearDown() throws Exception {
130: control.verify();
131: newDef = null;
132: }
133:
134: @Test
135: public void testDefinition() throws Exception {
136: assertEquals("http://apache.org/hello_world_soap_http", newDef
137: .getTargetNamespace());
138: Service serv = newDef.getService(new QName(
139: "http://apache.org/hello_world_soap_http",
140: "SOAPService"));
141: assertNotNull(serv);
142: assertNotNull(serv.getPort("SoapPort"));
143: }
144:
145: @Test
146: public void testPortType() throws Exception {
147: assertEquals(newDef.getPortTypes().size(), 1);
148: PortType portType = (PortType) newDef.getPortTypes().values()
149: .iterator().next();
150: assertNotNull(portType);
151: assertTrue(portType.getQName().equals(
152: new QName(newDef.getTargetNamespace(), "Greeter")));
153:
154: }
155:
156: @Test
157: public void testSayHiOperation() throws Exception {
158: PortType portType = newDef.getPortType(new QName(newDef
159: .getTargetNamespace(), "Greeter"));
160: Collection<Operation> operations = CastUtils.cast(portType
161: .getOperations(), Operation.class);
162:
163: assertEquals(operations.size(), 4);
164: Operation sayHi = portType.getOperation("sayHi",
165: "sayHiRequest", "sayHiResponse");
166: assertNotNull(sayHi);
167: assertEquals("sayHi", sayHi.getName());
168: Input input = sayHi.getInput();
169: assertNotNull(input);
170: assertEquals(input.getName(), "sayHiRequest");
171: Message message = input.getMessage();
172: assertNotNull(message);
173: assertEquals(message.getQName().getLocalPart(), "sayHiRequest");
174: assertEquals(message.getQName().getNamespaceURI(), newDef
175: .getTargetNamespace());
176: assertEquals(message.getParts().size(), 1);
177: assertEquals(message.getPart("in").getName(), "in");
178: Output output = sayHi.getOutput();
179: assertNotNull(output);
180: assertEquals(output.getName(), "sayHiResponse");
181: message = output.getMessage();
182: assertNotNull(message);
183: assertEquals(message.getQName().getLocalPart(), "sayHiResponse");
184: assertEquals(message.getQName().getNamespaceURI(), newDef
185: .getTargetNamespace());
186: assertEquals(message.getParts().size(), 1);
187: assertEquals(message.getPart("out").getName(), "out");
188: assertTrue(sayHi.getFaults().size() == 0);
189:
190: }
191:
192: @Test
193: public void testGreetMeOperation() throws Exception {
194: PortType portType = newDef.getPortType(new QName(newDef
195: .getTargetNamespace(), "Greeter"));
196: Operation greetMe = portType.getOperation("greetMe",
197: "greetMeRequest", "greetMeResponse");
198: assertNotNull(greetMe);
199: assertEquals("greetMe", greetMe.getName());
200: Input input = greetMe.getInput();
201: assertNotNull(input);
202: assertEquals(input.getName(), "greetMeRequest");
203: Message message = input.getMessage();
204: assertNotNull(message);
205: assertEquals(message.getQName().getLocalPart(),
206: "greetMeRequest");
207: assertEquals(message.getQName().getNamespaceURI(), newDef
208: .getTargetNamespace());
209: assertEquals(message.getParts().size(), 1);
210: assertEquals(message.getPart("in").getName(), "in");
211: Output output = greetMe.getOutput();
212: assertNotNull(output);
213: assertEquals(output.getName(), "greetMeResponse");
214: message = output.getMessage();
215: assertNotNull(message);
216: assertEquals(message.getQName().getLocalPart(),
217: "greetMeResponse");
218: assertEquals(message.getQName().getNamespaceURI(), newDef
219: .getTargetNamespace());
220: assertEquals(message.getParts().size(), 1);
221: assertEquals(message.getPart("out").getName(), "out");
222: assertTrue(greetMe.getFaults().size() == 0);
223:
224: }
225:
226: @Test
227: public void testGreetMeOneWayOperation() throws Exception {
228: PortType portType = newDef.getPortType(new QName(newDef
229: .getTargetNamespace(), "Greeter"));
230: Operation greetMeOneWay = portType.getOperation(
231: "greetMeOneWay", "greetMeOneWayRequest", null);
232: assertNotNull(greetMeOneWay);
233: assertEquals("greetMeOneWay", greetMeOneWay.getName());
234: Input input = greetMeOneWay.getInput();
235: assertNotNull(input);
236: assertEquals(input.getName(), "greetMeOneWayRequest");
237: Message message = input.getMessage();
238: assertNotNull(message);
239: assertEquals(message.getQName().getLocalPart(),
240: "greetMeOneWayRequest");
241: assertEquals(message.getQName().getNamespaceURI(), newDef
242: .getTargetNamespace());
243: assertEquals(message.getParts().size(), 1);
244: assertEquals(message.getPart("in").getName(), "in");
245: Output output = greetMeOneWay.getOutput();
246: assertNull(output);
247: assertTrue(greetMeOneWay.getFaults().size() == 0);
248: }
249:
250: @Test
251: public void testPingMeOperation() throws Exception {
252: PortType portType = newDef.getPortType(new QName(newDef
253: .getTargetNamespace(), "Greeter"));
254: Operation pingMe = portType.getOperation("pingMe",
255: "pingMeRequest", "pingMeResponse");
256: assertNotNull(pingMe);
257: assertEquals("pingMe", pingMe.getName());
258: Input input = pingMe.getInput();
259: assertNotNull(input);
260: assertEquals(input.getName(), "pingMeRequest");
261: Message message = input.getMessage();
262: assertNotNull(message);
263: assertEquals(message.getQName().getLocalPart(), "pingMeRequest");
264: assertEquals(message.getQName().getNamespaceURI(), newDef
265: .getTargetNamespace());
266: assertEquals(message.getParts().size(), 1);
267: assertEquals(message.getPart("in").getName(), "in");
268: Output output = pingMe.getOutput();
269: assertNotNull(output);
270: assertEquals(output.getName(), "pingMeResponse");
271: message = output.getMessage();
272: assertNotNull(message);
273: assertEquals(message.getQName().getLocalPart(),
274: "pingMeResponse");
275: assertEquals(message.getQName().getNamespaceURI(), newDef
276: .getTargetNamespace());
277: assertEquals(message.getParts().size(), 1);
278: assertEquals(message.getPart("out").getName(), "out");
279: assertTrue(pingMe.getFaults().size() == 1);
280: Fault fault = pingMe.getFault("pingMeFault");
281: assertNotNull(fault);
282: assertEquals(fault.getName(), "pingMeFault");
283: message = fault.getMessage();
284: assertNotNull(message);
285: assertEquals(message.getQName().getLocalPart(), "pingMeFault");
286: assertEquals(message.getQName().getNamespaceURI(), newDef
287: .getTargetNamespace());
288: assertEquals(message.getParts().size(), 1);
289: assertEquals(message.getPart("faultDetail").getName(),
290: "faultDetail");
291: assertNull(message.getPart("faultDetail").getTypeName());
292: }
293:
294: @Test
295: public void testBinding() throws Exception {
296: assertEquals(newDef.getBindings().size(), 1);
297: Binding binding = newDef.getBinding(new QName(newDef
298: .getTargetNamespace(), "Greeter_SOAPBinding"));
299: assertNotNull(binding);
300: assertEquals(binding.getBindingOperations().size(), 4);
301: }
302:
303: @Test
304: public void testSchemas() throws Exception {
305: Types types = newDef.getTypes();
306: assertNotNull(types);
307: Collection<ExtensibilityElement> schemas = CastUtils
308: .cast(types.getExtensibilityElements(),
309: ExtensibilityElement.class);
310: assertEquals(schemas.size(), 1);
311: XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
312: Element schemaElem = ((Schema) schemas.iterator().next())
313: .getElement();
314: assertEquals(schemaCollection.read(schemaElem)
315: .getTargetNamespace(),
316: "http://apache.org/hello_world_soap_http/types");
317: }
318:
319: }
|