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.handler;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.TreeSet;
023:
024: import javax.xml.ws.Binding;
025: import javax.xml.ws.LogicalMessage;
026: import javax.xml.ws.handler.Handler;
027: import javax.xml.ws.handler.LogicalHandler;
028: import javax.xml.ws.handler.LogicalMessageContext;
029: import javax.xml.ws.handler.MessageContext;
030:
031: import org.apache.cxf.interceptor.InterceptorChain;
032: import org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor;
033: import org.apache.cxf.message.Exchange;
034: import org.apache.cxf.message.Message;
035: import org.apache.cxf.message.MessageImpl;
036: import org.apache.cxf.transport.MessageObserver;
037: import org.apache.handlers.types.AddNumbersResponse;
038: import org.easymock.EasyMock;
039: import org.easymock.classextension.IMocksControl;
040: import org.junit.After;
041: import org.junit.Assert;
042: import org.junit.Before;
043: import org.junit.Test;
044:
045: import static org.easymock.EasyMock.eq;
046: import static org.easymock.EasyMock.expect;
047: import static org.easymock.EasyMock.isA;
048: import static org.easymock.classextension.EasyMock.createNiceControl;
049:
050: public class LogicalHandlerInterceptorTest extends Assert {
051:
052: private IMocksControl control;
053: private Binding binding;
054: private HandlerChainInvoker invoker;
055: private Message message;
056: private Exchange exchange;
057:
058: @Before
059: public void setUp() {
060: control = createNiceControl();
061: binding = control.createMock(Binding.class);
062: invoker = control.createMock(HandlerChainInvoker.class);
063: message = control.createMock(Message.class);
064: exchange = control.createMock(Exchange.class);
065: }
066:
067: @After
068: public void tearDown() {
069: }
070:
071: @Test
072: public void testInterceptSuccess() {
073: List<LogicalHandler> list = new ArrayList<LogicalHandler>();
074: list.add(new LogicalHandler() {
075: public void close(MessageContext arg0) {
076: }
077:
078: public boolean handleFault(MessageContext arg0) {
079: return true;
080: }
081:
082: public boolean handleMessage(MessageContext arg0) {
083: return true;
084: }
085: });
086: expect(invoker.getLogicalHandlers()).andReturn(list);
087: expect(message.getExchange()).andReturn(exchange).anyTimes();
088: expect(message.keySet()).andReturn(new TreeSet<String>())
089: .anyTimes();
090: expect(exchange.get(HandlerChainInvoker.class)).andReturn(
091: invoker);
092: expect(exchange.getOutMessage()).andReturn(message);
093: expect(
094: invoker.invokeLogicalHandlers(eq(false),
095: isA(LogicalMessageContext.class))).andReturn(
096: true);
097:
098: control.replay();
099: LogicalHandlerInInterceptor<Message> li = new LogicalHandlerInInterceptor<Message>(
100: binding);
101: assertEquals("unexpected phase", "pre-protocol", li.getPhase());
102: li.handleMessage(message);
103: control.verify();
104: }
105:
106: //JAX-WS spec: If handler returns false, for a request-response MEP, if the message
107: //direction is reversed during processing of a request message then the message
108: //becomes a response message.
109: //NOTE: commented out as this has been covered by other tests.
110: public void xtestReturnFalseClientSide() throws Exception {
111: List<Handler> list = new ArrayList<Handler>();
112: list.add(new LogicalHandler<LogicalMessageContext>() {
113: public void close(MessageContext arg0) {
114: }
115:
116: public boolean handleFault(
117: LogicalMessageContext messageContext) {
118: return true;
119: }
120:
121: public boolean handleMessage(
122: LogicalMessageContext messageContext) {
123: LogicalMessage msg = messageContext.getMessage();
124: AddNumbersResponse resp = new AddNumbersResponse();
125: resp.setReturn(11);
126: msg.setPayload(resp, null);
127: return false;
128: }
129: });
130: HandlerChainInvoker invoker1 = new HandlerChainInvoker(list);
131:
132: IMocksControl control1 = createNiceControl();
133: Binding binding1 = control1.createMock(Binding.class);
134: Exchange exchange1 = control1.createMock(Exchange.class);
135: expect(exchange1.get(HandlerChainInvoker.class)).andReturn(
136: invoker1).anyTimes();
137: Message outMessage = new MessageImpl();
138: outMessage.setExchange(exchange1);
139: InterceptorChain chain = control1
140: .createMock(InterceptorChain.class);
141: outMessage.setInterceptorChain(chain);
142: chain.abort();
143: EasyMock.expectLastCall();
144: MessageObserver observer = control1
145: .createMock(MessageObserver.class);
146: expect(exchange1.get(MessageObserver.class))
147: .andReturn(observer).anyTimes();
148: observer.onMessage(isA(Message.class));
149: EasyMock.expectLastCall();
150:
151: control1.replay();
152:
153: LogicalHandlerInInterceptor<Message> li = new LogicalHandlerInInterceptor<Message>(
154: binding1);
155: li.handleMessage(outMessage);
156: control1.verify();
157: }
158: }
|