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.binding.soap;
019:
020: import java.net.URL;
021: import java.util.Collection;
022: import java.util.List;
023:
024: import javax.wsdl.Definition;
025: import javax.wsdl.factory.WSDLFactory;
026: import javax.xml.namespace.QName;
027:
028: import org.xml.sax.InputSource;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.BusException;
032: import org.apache.cxf.binding.BindingFactoryManager;
033: import org.apache.cxf.binding.BindingFactoryManagerImpl;
034: import org.apache.cxf.binding.soap.model.SoapBindingInfo;
035: import org.apache.cxf.binding.soap.model.SoapBodyInfo;
036: import org.apache.cxf.binding.soap.model.SoapOperationInfo;
037: import org.apache.cxf.service.model.BindingFaultInfo;
038: import org.apache.cxf.service.model.BindingInfo;
039: import org.apache.cxf.service.model.BindingMessageInfo;
040: import org.apache.cxf.service.model.BindingOperationInfo;
041: import org.apache.cxf.service.model.MessagePartInfo;
042: import org.apache.cxf.service.model.ServiceInfo;
043: import org.apache.cxf.transport.DestinationFactoryManager;
044: import org.apache.cxf.wsdl.WSDLConstants;
045: import org.apache.cxf.wsdl11.WSDLServiceBuilder;
046: import org.easymock.IMocksControl;
047: import org.junit.Assert;
048: import org.junit.Before;
049: import org.junit.Test;
050:
051: import static org.easymock.EasyMock.expect;
052: import static org.easymock.EasyMock.expectLastCall;
053: import static org.easymock.classextension.EasyMock.createNiceControl;
054:
055: public class SoapBindingFactoryTest extends Assert {
056: IMocksControl control;
057:
058: @Before
059: public void setUp() {
060: control = createNiceControl();
061: }
062:
063: private Bus getMockBus() {
064: return control.createMock(Bus.class);
065: }
066:
067: private BindingFactoryManager getBindingFactoryManager(String ns,
068: Bus bus) throws BusException {
069: SoapBindingFactory bindingFactory = new SoapBindingFactory();
070: BindingFactoryManager bfm = new BindingFactoryManagerImpl();
071: bfm.registerBindingFactory(ns, bindingFactory);
072: return bfm;
073: }
074:
075: @Test
076: public void testFactory() throws Exception {
077: Definition d = createDefinition("/wsdl/hello_world.wsdl");
078:
079: Bus bus = getMockBus();
080:
081: BindingFactoryManager bfm = getBindingFactoryManager(
082: WSDLConstants.SOAP11_NAMESPACE, bus);
083:
084: bus.getExtension(BindingFactoryManager.class);
085: expectLastCall().andReturn(bfm).anyTimes();
086:
087: DestinationFactoryManager dfm = control
088: .createMock(DestinationFactoryManager.class);
089: expect(bus.getExtension(DestinationFactoryManager.class))
090: .andStubReturn(dfm);
091:
092: control.replay();
093:
094: WSDLServiceBuilder builder = new WSDLServiceBuilder(bus);
095: ServiceInfo serviceInfo = builder.buildServices(
096: d,
097: new QName("http://apache.org/hello_world_soap_http",
098: "SOAPService")).get(0);
099:
100: BindingInfo bi = serviceInfo.getBindings().iterator().next();
101:
102: assertTrue(bi instanceof SoapBindingInfo);
103:
104: SoapBindingInfo sbi = (SoapBindingInfo) bi;
105: assertEquals("document", sbi.getStyle());
106: assertTrue(WSDLConstants.NS_SOAP11_HTTP_BINDING
107: .equalsIgnoreCase(sbi.getTransportURI()));
108: assertTrue(sbi.getSoapVersion() instanceof Soap11);
109:
110: BindingOperationInfo boi = sbi.getOperation(new QName(
111: "http://apache.org/hello_world_soap_http", "sayHi"));
112: SoapOperationInfo sboi = boi
113: .getExtensor(SoapOperationInfo.class);
114: assertNotNull(sboi);
115: assertEquals("document", sboi.getStyle());
116: assertEquals("", sboi.getAction());
117:
118: BindingMessageInfo input = boi.getInput();
119: SoapBodyInfo bodyInfo = input.getExtensor(SoapBodyInfo.class);
120: assertEquals("literal", bodyInfo.getUse());
121:
122: List<MessagePartInfo> parts = bodyInfo.getParts();
123: assertNotNull(parts);
124: assertEquals(1, parts.size());
125: }
126:
127: @Test
128: public void testSoap12Factory() throws Exception {
129: Definition d = createDefinition("/wsdl/hello_world_soap12.wsdl");
130:
131: Bus bus = getMockBus();
132:
133: BindingFactoryManager bfm = getBindingFactoryManager(
134: WSDLConstants.SOAP12_NAMESPACE, bus);
135:
136: expect(bus.getExtension(BindingFactoryManager.class))
137: .andReturn(bfm);
138:
139: DestinationFactoryManager dfm = control
140: .createMock(DestinationFactoryManager.class);
141: expect(bus.getExtension(DestinationFactoryManager.class))
142: .andStubReturn(dfm);
143:
144: control.replay();
145:
146: WSDLServiceBuilder builder = new WSDLServiceBuilder(bus);
147: ServiceInfo serviceInfo = builder.buildServices(
148: d,
149: new QName("http://apache.org/hello_world_soap12_http",
150: "SOAPService")).get(0);
151:
152: BindingInfo bi = serviceInfo.getBindings().iterator().next();
153:
154: assertTrue(bi instanceof SoapBindingInfo);
155:
156: SoapBindingInfo sbi = (SoapBindingInfo) bi;
157: assertEquals("document", sbi.getStyle());
158: assertTrue(WSDLConstants.SOAP12_HTTP_TRANSPORT
159: .equalsIgnoreCase(sbi.getTransportURI()));
160: assertTrue(sbi.getSoapVersion() instanceof Soap12);
161:
162: BindingOperationInfo boi = sbi.getOperation(new QName(
163: "http://apache.org/hello_world_soap12_http", "sayHi"));
164: SoapOperationInfo sboi = boi
165: .getExtensor(SoapOperationInfo.class);
166: assertNotNull(sboi);
167: assertEquals("document", sboi.getStyle());
168: assertEquals("", sboi.getAction());
169:
170: BindingMessageInfo input = boi.getInput();
171: SoapBodyInfo bodyInfo = input.getExtensor(SoapBodyInfo.class);
172: assertEquals("literal", bodyInfo.getUse());
173:
174: List<MessagePartInfo> parts = bodyInfo.getParts();
175: assertNotNull(parts);
176: assertEquals(1, parts.size());
177:
178: boi = sbi.getOperation(new QName(
179: "http://apache.org/hello_world_soap12_http", "pingMe"));
180: sboi = boi.getExtensor(SoapOperationInfo.class);
181: assertNotNull(sboi);
182: assertEquals("document", sboi.getStyle());
183: Collection<BindingFaultInfo> faults = boi.getFaults();
184: assertEquals(1, faults.size());
185: BindingFaultInfo faultInfo = boi.getFault(new QName(
186: "http://apache.org/hello_world_soap12_http",
187: "pingMeFault"));
188: assertNotNull(faultInfo);
189: }
190:
191: public Definition createDefinition(String wsdlURL) throws Exception {
192: URL resource = getClass().getResource(wsdlURL);
193: return WSDLFactory.newInstance().newWSDLReader().readWSDL(null,
194: new InputSource(resource.openStream()));
195: }
196: }
|