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.Proxy;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.xml.namespace.QName;
026:
027: import org.apache.cxf.Bus;
028: import org.apache.cxf.BusFactory;
029: import org.apache.cxf.binding.BindingFactoryManager;
030: import org.apache.cxf.binding.soap.SoapBindingFactory;
031: import org.apache.cxf.binding.soap.SoapTransportFactory;
032: import org.apache.cxf.bus.CXFBusFactory;
033: import org.apache.cxf.bus.spring.SpringBusFactory;
034: import org.apache.cxf.configuration.Configurer;
035: import org.apache.cxf.endpoint.Client;
036: import org.apache.cxf.interceptor.Fault;
037: import org.apache.cxf.interceptor.Interceptor;
038: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
039: import org.apache.cxf.message.Message;
040: import org.apache.cxf.phase.AbstractPhaseInterceptor;
041: import org.apache.cxf.transport.DestinationFactoryManager;
042: import org.apache.cxf.transport.local.LocalTransportFactory;
043: import org.apache.hello_world_soap_http.Greeter;
044: import org.apache.hello_world_soap_http.GreeterImpl;
045: import org.apache.hello_world_soap_http.SOAPService;
046: import org.junit.After;
047: import org.junit.Assert;
048: import org.junit.Ignore;
049: import org.junit.Test;
050:
051: public class ConfiguredEndpointTest extends Assert {
052: private static final QName SERVICE_NAME = new QName(
053: "http://apache.org/hello_world_soap_http", "SOAPService");
054: private static final QName PORT_NAME = new QName(
055: "http://apache.org/hello_world_soap_http", "SoapPort");
056:
057: private BusFactory factory;
058:
059: @After
060: public void tearDown() {
061: Bus bus = BusFactory.getDefaultBus();
062: if (null != bus) {
063: bus.shutdown(true);
064: BusFactory.setDefaultBus(null);
065: }
066: System.clearProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
067: }
068:
069: @Test
070: public void testCXFDefaultClientEndpoint() {
071: factory = new CXFBusFactory();
072: BusFactory.setDefaultBus(null);
073: factory.createBus();
074: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
075: CXFBusFactory.class.getName());
076: doTestDefaultClientEndpoint();
077: }
078:
079: @Test
080: public void testSpringDefaultClientEndpoint() {
081: factory = new SpringBusFactory();
082: BusFactory.setDefaultBus(null);
083: factory.createBus();
084: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
085: SpringBusFactory.class.getName());
086: doTestDefaultClientEndpoint();
087: }
088:
089: private void doTestDefaultClientEndpoint() {
090:
091: javax.xml.ws.Service service = new SOAPService();
092: Greeter greeter = service.getPort(PORT_NAME, Greeter.class);
093:
094: JaxWsClientProxy eih = (JaxWsClientProxy) Proxy
095: .getInvocationHandler(greeter);
096: Client client = eih.getClient();
097: JaxWsEndpointImpl endpoint = (JaxWsEndpointImpl) client
098: .getEndpoint();
099: assertEquals("Unexpected bean name", PORT_NAME.toString()
100: + ".endpoint", endpoint.getBeanName());
101: assertTrue("Unexpected value for property validating",
102: !Boolean.TRUE.equals(endpoint
103: .get(Message.SCHEMA_VALIDATION_ENABLED)));
104:
105: // System.out.println("endpoint interceptors");
106: List<Interceptor> interceptors = endpoint.getInInterceptors();
107: printInterceptors("in", interceptors);
108: assertNull("Unexpected test interceptor",
109: findTestInterceptor(interceptors));
110: interceptors = endpoint.getOutInterceptors();
111: printInterceptors("out", interceptors);
112: assertNull("Unexpected test interceptor",
113: findTestInterceptor(interceptors));
114: interceptors = endpoint.getInFaultInterceptors();
115: printInterceptors("inFault", interceptors);
116: assertNull("Unexpected test interceptor",
117: findTestInterceptor(interceptors));
118: interceptors = endpoint.getOutFaultInterceptors();
119: printInterceptors("outFault", interceptors);
120: assertNull("Unexpected test interceptor",
121: findTestInterceptor(interceptors));
122:
123: // System.out.println("service interceptors");
124: org.apache.cxf.service.ServiceImpl svc = (org.apache.cxf.service.ServiceImpl) endpoint
125: .getService();
126: assertEquals("Unexpected bean name", SERVICE_NAME.toString(),
127: svc.getBeanName());
128: interceptors = svc.getInInterceptors();
129: printInterceptors("in", interceptors);
130: assertNull("Unexpected test interceptor",
131: findTestInterceptor(interceptors));
132: interceptors = svc.getOutInterceptors();
133: printInterceptors("out", interceptors);
134: assertNull("Unexpected test interceptor",
135: findTestInterceptor(interceptors));
136: interceptors = svc.getInFaultInterceptors();
137: printInterceptors("inFault", interceptors);
138: assertNull("Unexpected test interceptor",
139: findTestInterceptor(interceptors));
140: interceptors = svc.getOutFaultInterceptors();
141: printInterceptors("outFault", interceptors);
142: assertNull("Unexpected test interceptor",
143: findTestInterceptor(interceptors));
144: }
145:
146: @Test
147: @Ignore
148: public void testCXFConfiguredClientEndpoint() {
149: CXFBusFactory cf = new CXFBusFactory();
150: factory = cf;
151: BusFactory.setDefaultBus(null);
152: Map<String, Object> properties = new HashMap<String, Object>();
153: properties.put(Configurer.USER_CFG_FILE_PROPERTY_NAME,
154: "org/apache/cxf/jaxws/configured-endpoints.xml");
155: BusFactory.setDefaultBus(cf.createBus(null, properties));
156: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
157: CXFBusFactory.class.getName());
158: doTestConfiguredClientEndpoint();
159: }
160:
161: @Test
162: public void testSpringConfiguredClientEndpoint() {
163: SpringBusFactory sf = new SpringBusFactory();
164: factory = sf;
165: BusFactory.setDefaultBus(null);
166: BusFactory
167: .setDefaultBus(sf
168: .createBus("org/apache/cxf/jaxws/configured-endpoints.xml"));
169: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
170: SpringBusFactory.class.getName());
171: doTestConfiguredClientEndpoint();
172: }
173:
174: private void doTestConfiguredClientEndpoint() {
175:
176: javax.xml.ws.Service service = new SOAPService();
177: Greeter greeter = service.getPort(PORT_NAME, Greeter.class);
178:
179: JaxWsClientProxy eih = (JaxWsClientProxy) Proxy
180: .getInvocationHandler(greeter);
181: Client client = eih.getClient();
182: JaxWsEndpointImpl endpoint = (JaxWsEndpointImpl) client
183: .getEndpoint();
184: // The service shouldn't pick up the <jaxws:endpoint>...
185: // assertEquals("Unexpected bean name", PORT_NAME.toString() + ".endpoint", endpoint.getBeanName());
186: // // assertTrue("Unexpected value for property validating", endpoint.getValidating());
187: // List<Interceptor> interceptors = endpoint.getInInterceptors();
188: // assertEquals("Unexpected number of interceptors.", 1, interceptors.size());
189: // assertEquals("Unexpected interceptor id.", "endpoint-in",
190: // findTestInterceptor(interceptors).getId());
191: // interceptors = endpoint.getOutInterceptors();
192: // assertEquals("Unexpected number of interceptors.", 1, interceptors.size());
193: // assertEquals("Unexpected interceptor id.", "endpoint-out",
194: // findTestInterceptor(interceptors).getId());
195: // interceptors = endpoint.getInFaultInterceptors();
196: // assertEquals("Unexpected number of interceptors.", 1, interceptors.size());
197: // assertEquals("Unexpected interceptor id.", "endpoint-in-fault",
198: // findTestInterceptor(interceptors).getId());
199: // interceptors = endpoint.getOutFaultInterceptors();
200: // assertEquals("Unexpected number of interceptors.", 1, interceptors.size());
201: // assertEquals("Unexpected interceptor id.", "endpoint-out-fault",
202: // findTestInterceptor(interceptors).getId());
203:
204: org.apache.cxf.service.ServiceImpl svc = (org.apache.cxf.service.ServiceImpl) endpoint
205: .getService();
206: assertEquals("Unexpected bean name.", SERVICE_NAME.toString(),
207: svc.getBeanName());
208: List<Interceptor> interceptors = svc.getInInterceptors();
209: assertEquals("Unexpected number of interceptors.", 1,
210: interceptors.size());
211: assertEquals("Unexpected interceptor id.", "service-in",
212: findTestInterceptor(interceptors).getId());
213: interceptors = svc.getOutInterceptors();
214: assertEquals("Unexpected number of interceptors.", 1,
215: interceptors.size());
216: assertEquals("Unexpected interceptor id.", "service-out",
217: findTestInterceptor(interceptors).getId());
218: interceptors = svc.getInFaultInterceptors();
219: assertEquals("Unexpected number of interceptors.", 1,
220: interceptors.size());
221: assertEquals("Unexpected interceptor id.", "service-in-fault",
222: findTestInterceptor(interceptors).getId());
223: interceptors = svc.getOutFaultInterceptors();
224: assertEquals("Unexpected number of interceptors.", 1,
225: interceptors.size());
226: assertEquals("Unexpected interceptor id.", "service-out-fault",
227: findTestInterceptor(interceptors).getId());
228: }
229:
230: @Test
231: public void testCXFDefaultServerEndpoint() {
232: factory = new CXFBusFactory();
233: BusFactory.setDefaultBus(null);
234: factory.createBus();
235: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
236: CXFBusFactory.class.getName());
237: initializeBus();
238: doTestDefaultServerEndpoint();
239: }
240:
241: @Test
242: public void testSpringDefaultServerEndpoint() {
243: factory = new SpringBusFactory();
244: BusFactory.setDefaultBus(null);
245: factory.createBus();
246: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
247: SpringBusFactory.class.getName());
248: initializeBus();
249: doTestDefaultServerEndpoint();
250: }
251:
252: private void doTestDefaultServerEndpoint() {
253:
254: Object implementor = new GreeterImpl();
255: EndpointImpl ei = (EndpointImpl) (javax.xml.ws.Endpoint
256: .create(implementor));
257: ei.publish("http://localhost/greeter");
258:
259: JaxWsEndpointImpl endpoint = (JaxWsEndpointImpl) ei
260: .getEndpoint();
261: assertEquals("Unexpected bean name", PORT_NAME.toString()
262: + ".endpoint", endpoint.getBeanName());
263: assertTrue("Unexpected value for property validating",
264: !Boolean.TRUE.equals(endpoint
265: .get(Message.SCHEMA_VALIDATION_ENABLED)));
266:
267: List<Interceptor> interceptors = endpoint.getInInterceptors();
268: assertNull("Unexpected test interceptor",
269: findTestInterceptor(interceptors));
270: interceptors = endpoint.getOutInterceptors();
271: assertNull("Unexpected test interceptor",
272: findTestInterceptor(interceptors));
273: interceptors = endpoint.getInFaultInterceptors();
274: assertNull("Unexpected test interceptor",
275: findTestInterceptor(interceptors));
276: interceptors = endpoint.getOutFaultInterceptors();
277: assertNull("Unexpected test interceptor",
278: findTestInterceptor(interceptors));
279:
280: org.apache.cxf.service.ServiceImpl svc = (org.apache.cxf.service.ServiceImpl) endpoint
281: .getService();
282: assertEquals("Unexpected bean name", SERVICE_NAME.toString(),
283: svc.getBeanName());
284: interceptors = svc.getInInterceptors();
285: assertNull("Unexpected test interceptor",
286: findTestInterceptor(interceptors));
287: interceptors = svc.getOutInterceptors();
288: assertNull("Unexpected test interceptor",
289: findTestInterceptor(interceptors));
290: interceptors = svc.getInFaultInterceptors();
291: assertNull("Unexpected test interceptor",
292: findTestInterceptor(interceptors));
293: interceptors = svc.getOutFaultInterceptors();
294: assertNull("Unexpected test interceptor",
295: findTestInterceptor(interceptors));
296: }
297:
298: @Test
299: @Ignore
300: public void xtestCXFConfiguredServerEndpoint() {
301: CXFBusFactory cf = new CXFBusFactory();
302: factory = cf;
303: BusFactory.setDefaultBus(null);
304: Map<String, Object> properties = new HashMap<String, Object>();
305: properties.put(Configurer.USER_CFG_FILE_PROPERTY_NAME,
306: "org/apache/cxf/jaxws/configured-endpoints.xml");
307: BusFactory.setDefaultBus(cf.createBus(null, properties));
308: initializeBus();
309: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
310: CXFBusFactory.class.getName());
311: doTestConfiguredServerEndpoint();
312: }
313:
314: @Test
315: public void testSpringConfiguredServerEndpoint() {
316: SpringBusFactory sf = new SpringBusFactory();
317: factory = sf;
318: BusFactory.setDefaultBus(null);
319: BusFactory
320: .setDefaultBus(sf
321: .createBus("org/apache/cxf/jaxws/configured-endpoints.xml"));
322: initializeBus();
323: System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
324: SpringBusFactory.class.getName());
325: doTestConfiguredServerEndpoint();
326: }
327:
328: private void doTestConfiguredServerEndpoint() {
329:
330: Object implementor = new GreeterImpl();
331: EndpointImpl ei = (EndpointImpl) (javax.xml.ws.Endpoint
332: .create(implementor));
333: ei.publish("http://localhost/greeter");
334:
335: JaxWsEndpointImpl endpoint = (JaxWsEndpointImpl) ei
336: .getEndpoint();
337: assertEquals("Unexpected bean name", PORT_NAME.toString()
338: + ".endpoint", endpoint.getBeanName());
339: assertTrue("Unexpected value for property validating", Boolean
340: .valueOf((String) ei.getProperties().get(
341: Message.SCHEMA_VALIDATION_ENABLED)));
342: List<Interceptor> interceptors = endpoint.getInInterceptors();
343: assertEquals("Unexpected number of interceptors.", 6,
344: interceptors.size());
345: assertEquals("Unexpected interceptor id.", "endpoint-in",
346: findTestInterceptor(interceptors).getId());
347: interceptors = endpoint.getOutInterceptors();
348: assertEquals("Unexpected number of interceptors.", 7,
349: interceptors.size());
350: assertEquals("Unexpected interceptor id.", "endpoint-out",
351: findTestInterceptor(interceptors).getId());
352: interceptors = endpoint.getInFaultInterceptors();
353: assertEquals("Unexpected number of interceptors.", 4,
354: interceptors.size());
355: assertEquals("Unexpected interceptor id.", "endpoint-in-fault",
356: findTestInterceptor(interceptors).getId());
357: interceptors = endpoint.getOutFaultInterceptors();
358: assertEquals("Unexpected number of interceptors.", 4,
359: interceptors.size());
360: assertEquals("Unexpected interceptor id.",
361: "endpoint-out-fault", findTestInterceptor(interceptors)
362: .getId());
363:
364: org.apache.cxf.service.ServiceImpl svc = (org.apache.cxf.service.ServiceImpl) endpoint
365: .getService();
366: assertEquals("Unexpected bean name.", SERVICE_NAME.toString(),
367: svc.getBeanName());
368: interceptors = svc.getInInterceptors();
369: assertEquals("Unexpected number of interceptors.", 1,
370: interceptors.size());
371: assertEquals("Unexpected interceptor id.", "service-in",
372: findTestInterceptor(interceptors).getId());
373: interceptors = svc.getOutInterceptors();
374: assertEquals("Unexpected number of interceptors.", 1,
375: interceptors.size());
376: assertEquals("Unexpected interceptor id.", "service-out",
377: findTestInterceptor(interceptors).getId());
378: interceptors = svc.getInFaultInterceptors();
379: assertEquals("Unexpected number of interceptors.", 1,
380: interceptors.size());
381: assertEquals("Unexpected interceptor id.", "service-in-fault",
382: findTestInterceptor(interceptors).getId());
383: interceptors = svc.getOutFaultInterceptors();
384: assertEquals("Unexpected number of interceptors.", 1,
385: interceptors.size());
386: assertEquals("Unexpected interceptor id.", "service-out-fault",
387: findTestInterceptor(interceptors).getId());
388: }
389:
390: private void initializeBus() {
391: Bus bus = BusFactory.getDefaultBus();
392:
393: SoapBindingFactory bindingFactory = new SoapBindingFactory();
394:
395: bus.getExtension(BindingFactoryManager.class)
396: .registerBindingFactory(
397: "http://schemas.xmlsoap.org/wsdl/soap/",
398: bindingFactory);
399:
400: DestinationFactoryManager dfm = bus
401: .getExtension(DestinationFactoryManager.class);
402: SoapTransportFactory soapDF = new SoapTransportFactory();
403: soapDF.setBus(bus);
404: dfm.registerDestinationFactory(
405: "http://schemas.xmlsoap.org/wsdl/soap/", soapDF);
406: dfm.registerDestinationFactory(
407: "http://schemas.xmlsoap.org/soap/", soapDF);
408:
409: LocalTransportFactory localTransport = new LocalTransportFactory();
410: dfm.registerDestinationFactory(
411: "http://schemas.xmlsoap.org/soap/http", localTransport);
412: dfm.registerDestinationFactory(
413: "http://schemas.xmlsoap.org/wsdl/soap/http",
414: localTransport);
415: }
416:
417: private TestInterceptor findTestInterceptor(
418: List<Interceptor> interceptors) {
419: for (Interceptor i : interceptors) {
420: if (i instanceof TestInterceptor) {
421: return (TestInterceptor) i;
422: }
423: }
424: return null;
425: }
426:
427: private void printInterceptors(String type,
428: List<Interceptor> interceptors) {
429: //for (Interceptor i : interceptors) {
430: //System.out.println(" " + type + ": " + i.getClass().getName());
431: //}
432: }
433:
434: static final class TestInterceptor extends AbstractPhaseInterceptor {
435: public TestInterceptor(String name) {
436: super (name, "");
437: }
438:
439: public void handleMessage(Message message) throws Fault {
440: // TODO Auto-generated method stub
441: }
442: }
443: }
|