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.net.URL;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.List;
024:
025: import javax.wsdl.Definition;
026: import javax.wsdl.factory.WSDLFactory;
027: import javax.xml.namespace.QName;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.Bus;
033: import org.apache.cxf.endpoint.Server;
034: import org.apache.cxf.frontend.ServerFactoryBean;
035: import org.apache.cxf.interceptor.LoggingInInterceptor;
036: import org.apache.cxf.interceptor.LoggingOutInterceptor;
037: import org.apache.cxf.jaxws.service.ArrayService;
038: import org.apache.cxf.jaxws.service.ArrayServiceImpl;
039: import org.apache.cxf.jaxws.service.FooServiceImpl;
040: import org.apache.cxf.jaxws.service.Hello;
041: import org.apache.cxf.jaxws.service.HelloInterface;
042: import org.apache.cxf.jaxws.service.SayHi;
043: import org.apache.cxf.jaxws.service.SayHiImpl;
044: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
045: import org.apache.cxf.service.Service;
046: import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
047: import org.apache.cxf.service.model.BindingInfo;
048: import org.apache.cxf.service.model.InterfaceInfo;
049: import org.apache.cxf.transport.local.LocalTransportFactory;
050: import org.apache.cxf.wsdl11.ServiceWSDLBuilder;
051: import org.junit.Test;
052:
053: public class CodeFirstTest extends AbstractJaxWsTest {
054: String address = "local://localhost:9000/Hello";
055:
056: @Test
057: public void testDocLitModel() throws Exception {
058: Definition d = createService(false);
059:
060: Document wsdl = WSDLFactory.newInstance().newWSDLWriter()
061: .getDocument(d);
062: addNamespace("svc", "http://service.jaxws.cxf.apache.org/");
063:
064: assertValid(
065: "/wsdl:definitions/wsdl:service[@name='HelloService']",
066: wsdl);
067: assertValid("//wsdl:port/wsdlsoap:address[@location='"
068: + address + "']", wsdl);
069: assertValid("//wsdl:portType[@name='Hello']", wsdl);
070:
071: assertValid(
072: "/wsdl:definitions/wsdl:types/xsd:schema"
073: + "[@targetNamespace='http://service.jaxws.cxf.apache.org/']"
074: + "/xsd:import[@namespace='http://jaxb.dev.java.net/array']",
075: wsdl);
076:
077: assertValid(
078: "/wsdl:definitions/wsdl:types/xsd:schema"
079: + "[@targetNamespace='http://service.jaxws.cxf.apache.org/']"
080: + "/xsd:element[@type='ns0:stringArray']", wsdl);
081:
082: assertValid("/wsdl:definitions/wsdl:message[@name='sayHi']"
083: + "/wsdl:part[@element='ns1:sayHi'][@name='sayHi']",
084: wsdl);
085:
086: assertValid(
087: "/wsdl:definitions/wsdl:message[@name='getGreetingsResponse']"
088: + "/wsdl:part[@element='ns1:getGreetingsResponse'][@name='getGreetingsResponse']",
089: wsdl);
090:
091: assertValid(
092: "/wsdl:definitions/wsdl:binding/wsdl:operation[@name='getGreetings']"
093: + "/wsdlsoap:operation[@soapAction='myaction']",
094: wsdl);
095:
096: }
097:
098: @Test
099: public void testWrappedModel() throws Exception {
100: Definition d = createService(true);
101:
102: Document wsdl = WSDLFactory.newInstance().newWSDLWriter()
103: .getDocument(d);
104:
105: addNamespace("svc", "http://service.jaxws.cxf.apache.org");
106:
107: assertValid(
108: "/wsdl:definitions/wsdl:service[@name='HelloService']",
109: wsdl);
110: assertValid("//wsdl:port/wsdlsoap:address[@location='"
111: + address + "']", wsdl);
112: assertValid("//wsdl:portType[@name='Hello']", wsdl);
113: assertValid(
114: "/wsdl:definitions/wsdl:message[@name='sayHi']"
115: + "/wsdl:part[@element='ns1:sayHi'][@name='parameters']",
116: wsdl);
117: assertValid(
118: "/wsdl:definitions/wsdl:message[@name='sayHiResponse']"
119: + "/wsdl:part[@element='ns1:sayHiResponse'][@name='result']",
120: wsdl);
121: assertValid("//xsd:complexType[@name='sayHi']"
122: + "/xsd:sequence/xsd:element[@name='arg0']", wsdl);
123: }
124:
125: private Definition createService(boolean wrapped) throws Exception {
126: ReflectionServiceFactoryBean bean = new JaxWsServiceFactoryBean();
127:
128: Bus bus = getBus();
129: bean.setBus(bus);
130: bean.setServiceClass(Hello.class);
131: bean.setWrapped(wrapped);
132:
133: Service service = bean.create();
134:
135: InterfaceInfo i = service.getServiceInfos().get(0)
136: .getInterface();
137: assertEquals(4, i.getOperations().size());
138:
139: ServerFactoryBean svrFactory = new ServerFactoryBean();
140: svrFactory.setBus(bus);
141: svrFactory.setServiceFactory(bean);
142: svrFactory.setAddress(address);
143: svrFactory.create();
144:
145: Collection<BindingInfo> bindings = service.getServiceInfos()
146: .get(0).getBindings();
147: assertEquals(1, bindings.size());
148:
149: ServiceWSDLBuilder wsdlBuilder = new ServiceWSDLBuilder(bus,
150: service.getServiceInfos().get(0));
151: return wsdlBuilder.build();
152: }
153:
154: @Test
155: public void testEndpoint() throws Exception {
156: Hello service = new Hello();
157:
158: EndpointImpl ep = new EndpointImpl(getBus(), service,
159: (String) null);
160: ep.publish("local://localhost:9090/hello");
161:
162: Node res = invoke("local://localhost:9090/hello",
163: LocalTransportFactory.TRANSPORT_ID, "sayHi.xml");
164:
165: assertNotNull(res);
166:
167: addNamespace("h", "http://service.jaxws.cxf.apache.org/");
168: assertValid("//s:Body/h:sayHiResponse/return", res);
169:
170: res = invoke("local://localhost:9090/hello",
171: LocalTransportFactory.TRANSPORT_ID, "getGreetings.xml");
172:
173: assertNotNull(res);
174:
175: addNamespace("h", "http://service.jaxws.cxf.apache.org/");
176: assertValid("//s:Body/h:getGreetingsResponse/return[1]", res);
177: assertValid("//s:Body/h:getGreetingsResponse/return[2]", res);
178: }
179:
180: @Test
181: public void testClient() throws Exception {
182: Hello serviceImpl = new Hello();
183: EndpointImpl ep = new EndpointImpl(getBus(), serviceImpl,
184: (String) null);
185: ep.publish("local://localhost:9090/hello");
186: ep.getServer().getEndpoint().getInInterceptors().add(
187: new LoggingInInterceptor());
188: ep.getServer().getEndpoint().getOutInterceptors().add(
189: new LoggingOutInterceptor());
190: QName serviceName = new QName(
191: "http://service.jaxws.cxf.apache.org/", "HelloService");
192: QName portName = new QName(
193: "http://service.jaxws.cxf.apache.org/", "HelloPort");
194:
195: // need to set the same bus with service , so use the ServiceImpl
196: ServiceImpl service = new ServiceImpl(getBus(), (URL) null,
197: serviceName, null);
198: service.addPort(portName, "http://schemas.xmlsoap.org/soap/",
199: "local://localhost:9090/hello");
200:
201: HelloInterface proxy = service.getPort(portName,
202: HelloInterface.class);
203: assertEquals("Get the wrong result", "hello", proxy
204: .sayHi("hello"));
205: String[] strInput = new String[2];
206: strInput[0] = "Hello";
207: strInput[1] = "Bonjour";
208: String[] strings = proxy.getStringArray(strInput);
209: assertEquals(strings.length, 2);
210: assertEquals(strings[0], "HelloHello");
211: assertEquals(strings[1], "BonjourBonjour");
212: List<String> listInput = new ArrayList<String>();
213: listInput.add("Hello");
214: listInput.add("Bonjour");
215: List<String> list = proxy.getStringList(listInput);
216: assertEquals(list.size(), 2);
217: assertEquals(list.get(0), "HelloHello");
218: assertEquals(list.get(1), "BonjourBonjour");
219: //now the client side can't unmarshal the complex type without binding types annoutation
220: List<String> result = proxy.getGreetings();
221: assertEquals(2, result.size());
222: }
223:
224: @Test
225: public void testRpcClient() throws Exception {
226: SayHiImpl serviceImpl = new SayHiImpl();
227: EndpointImpl ep = new EndpointImpl(getBus(), serviceImpl,
228: (String) null);
229: ep.publish("http://localhost:9090/hello");
230:
231: QName serviceName = new QName("http://mynamespace.com/",
232: "SayHiService");
233: QName portName = new QName("http://mynamespace.com/",
234: "HelloPort");
235:
236: // need to set the same bus with service , so use the ServiceImpl
237: ServiceImpl service = new ServiceImpl(getBus(), (URL) null,
238: serviceName, null);
239: service.addPort(portName, "http://schemas.xmlsoap.org/soap/",
240: "http://localhost:9090/hello");
241:
242: SayHi proxy = service.getPort(portName, SayHi.class);
243: long res = proxy.sayHi(3);
244: assertEquals(3, res);
245: String[] strInput = new String[2];
246: strInput[0] = "Hello";
247: strInput[1] = "Bonjour";
248: String[] strings = proxy.getStringArray(strInput);
249: assertEquals(strings.length, 2);
250: assertEquals(strings[0], "HelloHello");
251: assertEquals(strings[1], "BonjourBonjour");
252:
253: }
254:
255: @Test
256: public void testArrayAndList() throws Exception {
257: ArrayServiceImpl serviceImpl = new ArrayServiceImpl();
258: EndpointImpl ep = new EndpointImpl(getBus(), serviceImpl,
259: (String) null);
260: ep.publish("local://localhost:9090/array");
261: ep.getServer().getEndpoint().getInInterceptors().add(
262: new LoggingInInterceptor());
263: ep.getServer().getEndpoint().getOutInterceptors().add(
264: new LoggingOutInterceptor());
265: QName serviceName = new QName(
266: "http://service.jaxws.cxf.apache.org/", "ArrayService");
267: QName portName = new QName(
268: "http://service.jaxws.cxf.apache.org/", "ArrayPort");
269:
270: // need to set the same bus with service , so use the ServiceImpl
271: ServiceImpl service = new ServiceImpl(getBus(), (URL) null,
272: serviceName, null);
273: service.addPort(portName, "http://schemas.xmlsoap.org/soap/",
274: "local://localhost:9090/array");
275:
276: ArrayService proxy = service.getPort(portName,
277: ArrayService.class);
278: String[] arrayOut = proxy.arrayOutput();
279: assertEquals(arrayOut.length, 3);
280: assertEquals(arrayOut[0], "string1");
281: assertEquals(arrayOut[1], "string2");
282: assertEquals(arrayOut[2], "string3");
283: String[] arrayIn = new String[3];
284: arrayIn[0] = "string1";
285: arrayIn[1] = "string2";
286: arrayIn[2] = "string3";
287: assertEquals(proxy.arrayInput(arrayIn), "string1string2string3");
288: arrayOut = proxy.arrayInputAndOutput(arrayIn);
289: assertEquals(arrayOut.length, 3);
290: assertEquals(arrayOut[0], "string11");
291: assertEquals(arrayOut[1], "string22");
292: assertEquals(arrayOut[2], "string33");
293:
294: List<String> listOut = proxy.listOutput();
295: assertEquals(listOut.size(), 3);
296: assertEquals(listOut.get(0), "string1");
297: assertEquals(listOut.get(1), "string2");
298: assertEquals(listOut.get(2), "string3");
299: List<String> listIn = new ArrayList<String>();
300: listIn.add("list1");
301: listIn.add("list2");
302: listIn.add("list3");
303: assertEquals(proxy.listInput(listIn), "list1list2list3");
304: }
305:
306: @Test
307: public void testNamespacedWebParamsBare() throws Exception {
308: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
309: sf.setAddress("http://localhost/test");
310: sf.setServiceClass(FooServiceImpl.class);
311:
312: Server server = sf.create();
313:
314: Document doc = getWSDLDocument(server);
315:
316: assertValid(
317: "//xsd:schema[@targetNamespace='http://namespace3']",
318: doc);
319: assertValid(
320: "//xsd:schema[@targetNamespace='http://namespace5']",
321: doc);
322:
323: assertValid("//xsd:element[@name='FooEcho2HeaderRequest'][1]",
324: doc);
325: assertInvalid(
326: "//xsd:element[@name='FooEcho2HeaderRequest'][2]", doc);
327: }
328:
329: @Test
330: public void testNamespacedWebParamsWrapped() throws Exception {
331: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
332: sf.setAddress("http://localhost/test");
333: sf.setServiceBean(new FooServiceImpl());
334: sf.getServiceFactory().setWrapped(true);
335:
336: Server server = sf.create();
337:
338: Document doc = getWSDLDocument(server);
339:
340: // DOMUtils.writeXml(doc, System.out);
341: assertValid(
342: "//xsd:schema[@targetNamespace='http://namespace3']",
343: doc);
344: assertValid(
345: "//xsd:schema[@targetNamespace='http://namespace5']",
346: doc);
347: }
348:
349: }
|