001: package org.objectweb.celtix.systest.handlers;
002:
003: import java.net.URL;
004: import java.util.Iterator;
005: import java.util.List;
006: import javax.xml.bind.JAXBContext;
007: import javax.xml.namespace.QName;
008: import javax.xml.ws.BindingProvider;
009: import javax.xml.ws.LogicalMessage;
010: import javax.xml.ws.ProtocolException;
011: import javax.xml.ws.handler.Handler;
012: import javax.xml.ws.handler.LogicalMessageContext;
013: import javax.xml.ws.handler.MessageContext;
014:
015: import junit.framework.Test;
016: import junit.framework.TestSuite;
017:
018: import org.objectweb.celtix.BusException;
019: import org.objectweb.celtix.systest.common.ClientServerSetupBase;
020: import org.objectweb.celtix.systest.common.ClientServerTestBase;
021: import org.objectweb.handler_test.HandlerTest;
022: import org.objectweb.handler_test.HandlerTestService;
023: import org.objectweb.handler_test.PingException;
024: import org.objectweb.handler_test.types.PingResponse;
025: import org.objectweb.hello_world_soap_http.types.GreetMe;
026:
027: public class HandlerInvocationTest extends ClientServerTestBase {
028:
029: private final QName serviceName = new QName(
030: "http://objectweb.org/hello_world_soap_http",
031: "HandlerTestService");
032: private final QName portName = new QName(
033: "http://objectweb.org/hello_world_soap_http", "SoapPort");
034:
035: private URL wsdl;
036: private HandlerTestService service;
037: private HandlerTest handlerTest;
038:
039: public static Test suite() throws Exception {
040: TestSuite suite = new TestSuite(HandlerInvocationTest.class);
041: return new ClientServerSetupBase(suite) {
042: public void startServers() throws Exception {
043: assertTrue("server did not launch correctly",
044: launchServer(Server.class));
045: }
046: };
047: }
048:
049: public void setUp() throws BusException {
050: try {
051: super .setUp();
052:
053: wsdl = HandlerInvocationTest.class
054: .getResource("/wsdl/handler_test.wsdl");
055: service = new HandlerTestService(wsdl, serviceName);
056: handlerTest = service.getPort(portName, HandlerTest.class);
057: if (!"testHandlersInvoked".equals(getName())) {
058: addHandlersToChain((BindingProvider) handlerTest,
059: new TestStreamHandler(false));
060: }
061: } catch (Exception ex) {
062: ex.printStackTrace();
063: fail(ex.toString());
064: }
065: }
066:
067: public void testHandlersInvoked() throws PingException {
068:
069: TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
070: false);
071: TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
072: false);
073: TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
074: TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
075: TestStreamHandler streamHandler = new TestStreamHandler(false);
076:
077: addHandlersToChain((BindingProvider) handlerTest, handler1,
078: handler2, soapHandler1, soapHandler2, streamHandler);
079:
080: List<String> resp = handlerTest.ping();
081: assertNotNull(resp);
082:
083: assertEquals("handle message was not invoked", 2, handler1
084: .getHandleMessageInvoked());
085: assertEquals("handle message was not invoked", 2, handler2
086: .getHandleMessageInvoked());
087: assertEquals("handle message was not invoked", 2, soapHandler1
088: .getHandleMessageInvoked());
089: assertEquals("handle message was not invoked", 2, soapHandler2
090: .getHandleMessageInvoked());
091: assertEquals("handle message was not invoked", 2, streamHandler
092: .getHandleMessageInvoked());
093: assertTrue("close must be called", handler1.isCloseInvoked());
094: assertTrue("close must be called", handler2.isCloseInvoked());
095: assertTrue("close must be called", soapHandler1
096: .isCloseInvoked());
097: assertTrue("close must be called", soapHandler2
098: .isCloseInvoked());
099: assertTrue("close must be called", streamHandler
100: .isCloseInvoked());
101:
102: // the server has encoded into the response the order in
103: // which the handlers have been invoked, parse it and make
104: // sure everything is ok
105:
106: // expected order for inbound interceptors
107: //
108: // note: the stream handler does not add itself to the list on
109: // the way out of the server. It compresses the message so
110: // the fact that we are here indicates that it has
111: // participated correctly in the message exchange.
112: String[] handlerNames = { "streamHandler5", "soapHandler4",
113: "soapHandler3", "handler2", "handler1", "servant",
114: "handler1", "handler2", "soapHandler3", "soapHandler4" };
115:
116: assertEquals(handlerNames.length, resp.size());
117:
118: Iterator iter = resp.iterator();
119: for (String expected : handlerNames) {
120: assertEquals(expected, iter.next());
121: }
122: }
123:
124: public void testLogicalHandlerStopProcessing() throws PingException {
125:
126: final String clientHandlerMessage = "handler2 client side";
127:
128: TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
129: false);
130: TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
131: false) {
132: public boolean handleMessage(LogicalMessageContext ctx) {
133: super .handleMessage(ctx);
134: try {
135: Boolean outbound = (Boolean) ctx
136: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
137: if (outbound) {
138: LogicalMessage msg = ctx.getMessage();
139: assertNotNull("logical message is null", msg);
140: JAXBContext jaxbCtx = JAXBContext
141: .newInstance(GreetMe.class.getPackage()
142: .getName());
143: PingResponse resp = new PingResponse();
144: resp.getHandlersInfo()
145: .add(clientHandlerMessage);
146:
147: msg.setPayload(resp, jaxbCtx);
148: }
149:
150: } catch (Exception e) {
151: e.printStackTrace();
152: fail(e.toString());
153: }
154: return false;
155: }
156: };
157:
158: addHandlersToChain((BindingProvider) handlerTest, handler1,
159: handler2);
160:
161: List<String> resp = handlerTest.ping();
162: assertEquals(clientHandlerMessage, resp.get(0));
163:
164: assertEquals(
165: "handler must be invoked for inbound & outbound message",
166: 2, handler1.getHandleMessageInvoked());
167: assertEquals("second handler must be invoked exactly once", 1,
168: handler2.getHandleMessageInvoked());
169: assertTrue("close must be called", handler1.isCloseInvoked());
170: assertTrue("close must be called", handler2.isCloseInvoked());
171: }
172:
173: public void testLogicalHandlerStopProcessingServerSide()
174: throws PingException {
175:
176: String[] expectedHandlers = { "streamHandler5", "soapHandler4",
177: "soapHandler3", "handler2", "soapHandler3",
178: "soapHandler4" };
179:
180: List<String> resp = handlerTest
181: .pingWithArgs("handler2 inbound stop");
182:
183: assertEquals(expectedHandlers.length, resp.size());
184:
185: int i = 0;
186: for (String expected : expectedHandlers) {
187: assertEquals(expected, resp.get(i++));
188: }
189:
190: String[] expectedHandlers1 = { "streamHandler5",
191: "soapHandler4", "soapHandler3", "soapHandler4" };
192: resp = handlerTest.pingWithArgs("soapHandler3 inbound stop");
193: assertEquals(expectedHandlers1.length, resp.size());
194: i = 0;
195: for (String expected : expectedHandlers1) {
196: assertEquals(expected, resp.get(i++));
197: }
198: }
199:
200: public void testLogicalHandlerThrowsProtocolException()
201: throws Exception {
202:
203: final String clientHandlerMessage = "handler1 client side";
204:
205: TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
206: false) {
207: public boolean handleMessage(LogicalMessageContext ctx) {
208: super .handleMessage(ctx);
209: throw new ProtocolException(clientHandlerMessage);
210: }
211: };
212: TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
213: false);
214:
215: addHandlersToChain((BindingProvider) handlerTest, handler1,
216: handler2);
217:
218: try {
219: handlerTest.ping();
220: fail("did not get expected exception");
221: } catch (ProtocolException e) {
222: assertEquals(clientHandlerMessage, e.getMessage());
223: }
224: assertTrue(!handler2.isHandleFaultInvoked());
225: assertTrue(handler1.isCloseInvoked());
226: assertTrue(!handler2.isCloseInvoked());
227: }
228:
229: public void testLogicalHandlerThrowsProtocolExceptionServerSide()
230: throws PingException {
231: try {
232: handlerTest
233: .pingWithArgs("handler2 inbound throw javax.xml.ws.ProtocolException");
234: fail("did not get expected exception");
235: } catch (ProtocolException e) {
236: // happy now
237: }
238: }
239:
240: public void testLogicalHandlerHandlerFault() {
241:
242: TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
243: false);
244: TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
245: false);
246: TestStreamHandler streamHandler = new TestStreamHandler(false);
247: addHandlersToChain((BindingProvider) handlerTest, handler1,
248: handler2, streamHandler);
249:
250: try {
251: handlerTest.pingWithArgs("servant throw exception");
252: fail("did not get expected PingException");
253: } catch (PingException e) {
254: assertTrue(e.getMessage().contains("from servant"));
255: }
256:
257: assertEquals(1, handler1.getHandleMessageInvoked());
258: assertEquals(1, handler2.getHandleMessageInvoked());
259: assertEquals(1, streamHandler.getHandleMessageInvoked());
260: assertEquals(1, handler1.getHandleFaultInvoked());
261: assertEquals(1, handler2.getHandleFaultInvoked());
262: assertEquals(1, streamHandler.getHandleFaultInvoked());
263: }
264:
265: public void testLogicalHandlerOneWay() {
266: TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
267: false);
268: TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
269: false);
270: TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
271:
272: addHandlersToChain((BindingProvider) handlerTest, handler1,
273: handler2, soapHandler1);
274:
275: handlerTest.pingOneWay();
276:
277: assertEquals(1, handler1.getHandleMessageInvoked());
278: assertEquals(1, handler2.getHandleMessageInvoked());
279: assertEquals(1, soapHandler1.getHandleMessageInvoked());
280: }
281:
282: void addHandlersToChain(BindingProvider bp, Handler... handlers) {
283: List<Handler> handlerChain = bp.getBinding().getHandlerChain();
284: assertNotNull(handlerChain);
285: for (Handler h : handlers) {
286: handlerChain.add(h);
287: }
288: }
289: }
|