001: package org.objectweb.celtix.bus.jaxws;
002:
003: import java.lang.reflect.Method;
004: import java.util.List;
005: import java.util.Properties;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.ws.Endpoint;
009: import javax.xml.ws.WebServiceContext;
010: import javax.xml.ws.WebServiceException;
011: import javax.xml.ws.handler.Handler;
012: import javax.xml.ws.spi.Provider;
013:
014: import junit.framework.TestCase;
015:
016: import org.mortbay.http.HttpContext;
017: import org.mortbay.http.HttpServer;
018: import org.mortbay.http.SocketListener;
019: import org.mortbay.util.InetAddrPort;
020: import org.objectweb.celtix.Bus;
021: import org.objectweb.celtix.BusException;
022: import org.objectweb.celtix.bindings.BindingManager;
023: import org.objectweb.celtix.bindings.DataBindingCallback;
024: import org.objectweb.celtix.bus.bindings.TestBinding;
025: import org.objectweb.celtix.bus.bindings.TestBindingFactory;
026: import org.objectweb.celtix.bus.jaxws.spi.ProviderImpl;
027: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
028: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
029: import org.objectweb.hello_world_soap_http.AnnotatedGreeterImpl;
030: import org.objectweb.hello_world_soap_http.HWSourcePayloadProvider;
031: import org.objectweb.hello_world_soap_http.HelloWorldServiceProvider;
032: import org.objectweb.hello_world_soap_http.NotAnnotatedProvider;
033:
034: public class EndpointImplTest extends TestCase {
035: private String epfClassName;
036: private Bus bus;
037: private Endpoint endpoint;
038: private AnnotatedGreeterImpl servant;
039:
040: public void setUp() throws Exception {
041: epfClassName = System
042: .getProperty(Provider.JAXWSPROVIDER_PROPERTY);
043: System.setProperty(Provider.JAXWSPROVIDER_PROPERTY,
044: ProviderImpl.JAXWS_PROVIDER);
045: bus = Bus.init();
046: BindingManager bm = bus.getBindingManager();
047: bm.registerBinding(TestBinding.TEST_BINDING,
048: new TestBindingFactory(bus));
049: servant = new AnnotatedGreeterImpl();
050: endpoint = Endpoint.create(TestBinding.TEST_BINDING, servant);
051:
052: }
053:
054: public void tearDown() throws Exception {
055: bus.shutdown(true);
056: if (null == epfClassName) {
057: Properties properties = System.getProperties();
058: properties.remove(Provider.JAXWSPROVIDER_PROPERTY);
059: System.setProperties(properties);
060: } else {
061: System.setProperty(Provider.JAXWSPROVIDER_PROPERTY,
062: epfClassName);
063: }
064: }
065:
066: public void testPublishUsingAddress() throws Exception {
067: assertNotNull(endpoint);
068: assertTrue(!endpoint.isPublished());
069: String address = "http://localhost:8080/test";
070: try {
071: endpoint.publish(address);
072: } catch (IllegalArgumentException ex) {
073: assertTrue(ex.getCause() instanceof BusException);
074: assertEquals("BINDING_INCOMPATIBLE_ADDRESS_EXC",
075: ((BusException) ex.getCause()).getCode());
076: }
077: address = "test://localhost:7777/test";
078: endpoint.publish(address);
079: assertTrue(endpoint.isPublished());
080: endpoint.stop();
081: assertTrue(!endpoint.isPublished());
082: endpoint.stop();
083: assertTrue(!endpoint.isPublished());
084: }
085:
086: public void testPublishUsingHttpContext() throws Exception {
087:
088: assertNotNull(endpoint);
089: assertTrue(!endpoint.isPublished());
090:
091: HttpServer server = new HttpServer();
092: SocketListener listener = new SocketListener(new InetAddrPort(
093: 27220));
094: server.addListener(listener);
095: try {
096: server.start();
097: } catch (Exception e) {
098: // TODO Auto-generated catch block
099: e.printStackTrace();
100: }
101:
102: HttpContext context = server
103: .getContext("http://localhost:27220/test");
104: endpoint.publish(context);
105: assertTrue(endpoint.isPublished());
106:
107: listener.stop();
108: server.stop(true);
109:
110: }
111:
112: public void testPublishUsingEndpointReferenceTypeContext()
113: throws Exception {
114:
115: assertNotNull(endpoint);
116: assertTrue(!endpoint.isPublished());
117:
118: EndpointReferenceType context = EndpointReferenceUtils
119: .getEndpointReference("http://localhost:8080/test");
120: endpoint.publish(context);
121:
122: assertTrue(endpoint.isPublished());
123: }
124:
125: public void testResourceInjectionApplicationContext() {
126: // WebServiceContext is specified by a resource annotation.
127: // This should be inject when the endpoing is published.
128: //i.e in context of Endpoint.publish.
129: //Such injection should not happen for Endpoint.create.
130: //JAX_WS Spec 5.2.1.
131:
132: WebServiceContext ctx = servant.getContext();
133: assertNull(ctx);
134: try {
135: String address = "http://localhost:8080/test";
136: endpoint.publish(address);
137: } catch (IllegalArgumentException ex) {
138: assertTrue(ex.getCause() instanceof BusException);
139: assertEquals("BINDING_INCOMPATIBLE_ADDRESS_EXC",
140: ((BusException) ex.getCause()).getCode());
141: }
142: ctx = servant.getContext();
143: assertNotNull(ctx);
144: }
145:
146: public void testHandlerAnnotation() {
147:
148: List<Handler> handlers = endpoint.getBinding()
149: .getHandlerChain();
150: assertNotNull(handlers);
151: }
152:
153: public void testCreateWithProvider() {
154: HelloWorldServiceProvider provider = new HelloWorldServiceProvider();
155: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider);
156: assertNotNull(endpoint);
157:
158: EndpointImpl impl = (EndpointImpl) endpoint;
159: assertNotNull(impl.getWebServiceProvider());
160: assertEquals(DataBindingCallback.Mode.MESSAGE, impl
161: .getServiceMode());
162: }
163:
164: public void testBadProvider() {
165: NotAnnotatedProvider badProvider = new NotAnnotatedProvider();
166: try {
167: Endpoint.create(TestBinding.TEST_BINDING, badProvider);
168: fail("Should have received a exception");
169: } catch (WebServiceException ex) {
170: // expected
171: }
172: }
173:
174: public void testGetMethod() {
175: QName opName = new QName("", "PutLastTradedPrice");
176: assertTrue(endpoint instanceof EndpointImpl);
177: EndpointImpl impl = (EndpointImpl) endpoint;
178: //Check if a method by a localPart of opName exists on the Implementor.
179: Method m = impl.getMethod(opName);
180:
181: assertNotNull(m);
182:
183: opName = new QName("", "putLastTradedPrice");
184: m = impl.getMethod(opName);
185: assertNull(m);
186:
187: //Test for provider
188: HelloWorldServiceProvider provider = new HelloWorldServiceProvider();
189: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider);
190: assertTrue(endpoint instanceof EndpointImpl);
191: impl = (EndpointImpl) endpoint;
192: opName = new QName("", "invoke");
193: //Check if a method by a localPart of opName exists on the Implementor.
194: m = impl.getMethod(opName);
195: assertNotNull(m);
196: assertEquals("invoke", m.getName());
197: }
198:
199: public void testGetServiceMode() {
200: EndpointImpl impl = (EndpointImpl) endpoint;
201: assertNotNull(impl);
202: DataBindingCallback.Mode mode = impl.getServiceMode();
203: assertNotNull(mode);
204: assertEquals(DataBindingCallback.Mode.PARTS, mode);
205:
206: //Test for provider
207: HelloWorldServiceProvider provider = new HelloWorldServiceProvider();
208: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider);
209: assertTrue(endpoint instanceof EndpointImpl);
210: impl = (EndpointImpl) endpoint;
211:
212: mode = impl.getServiceMode();
213: assertNotNull(mode);
214: assertEquals(DataBindingCallback.Mode.MESSAGE, mode);
215:
216: HWSourcePayloadProvider provider1 = new HWSourcePayloadProvider();
217: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider1);
218: assertTrue(endpoint instanceof EndpointImpl);
219:
220: impl = (EndpointImpl) endpoint;
221: mode = impl.getServiceMode();
222: assertNotNull(mode);
223: assertEquals(DataBindingCallback.Mode.PAYLOAD, mode);
224: }
225:
226: /*
227: public void testCreateDataBindingCallback() {
228: ObjectMessageContextImpl ctx = new ObjectMessageContextImpl();
229: EndpointImpl impl = (EndpointImpl)endpoint;
230: assertNotNull(impl);
231: DataBindingCallback cb =
232: impl.createDataBindingCallback(ctx, DataBindingCallback.Mode.PARTS);
233: assertNotNull(cb);
234: assertTrue(cb instanceof JAXBDataBindingCallback);
235: assertEquals(DataBindingCallback.Mode.PARTS, cb.getMode());
236:
237: //Test for provider
238: HelloWorldServiceProvider provider = new HelloWorldServiceProvider();
239: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider);
240: assertTrue(endpoint instanceof EndpointImpl);
241: impl = (EndpointImpl)endpoint;
242:
243: cb = impl.createDataBindingCallback(ctx, DataBindingCallback.Mode.MESSAGE);
244: assertNotNull(cb);
245: assertTrue(cb instanceof DynamicDataBindingCallback);
246: assertEquals(DataBindingCallback.Mode.MESSAGE, cb.getMode());
247:
248: HWSourcePayloadProvider provider1 = new HWSourcePayloadProvider();
249: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider1);
250: assertTrue(endpoint instanceof EndpointImpl);
251:
252: impl = (EndpointImpl)endpoint;
253: cb = impl.createDataBindingCallback(ctx, DataBindingCallback.Mode.PAYLOAD);
254: assertNotNull(cb);
255: assertTrue(cb instanceof DynamicDataBindingCallback);
256: assertEquals(DataBindingCallback.Mode.PAYLOAD, cb.getMode());
257: }
258: */
259:
260: public void testGetWebServiceAnnotatedClass() {
261: EndpointImpl impl = (EndpointImpl) endpoint;
262: assertNotNull(impl);
263: List<Class<?>> classList = impl.getWebServiceAnnotatedClass();
264:
265: assertNotNull(classList);
266: assertEquals(1, classList.size());
267:
268: //Test for provider
269: HelloWorldServiceProvider provider = new HelloWorldServiceProvider();
270: endpoint = Endpoint.create(TestBinding.TEST_BINDING, provider);
271: assertTrue(endpoint instanceof EndpointImpl);
272: impl = (EndpointImpl) endpoint;
273:
274: classList = impl.getWebServiceAnnotatedClass();
275: assertNotNull(classList);
276: assertEquals(0, classList.size());
277: }
278:
279: }
|