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.ws.rm;
019:
020: import java.lang.reflect.Method;
021: import java.math.BigInteger;
022: import java.util.Iterator;
023: import java.util.SortedSet;
024: import java.util.TreeSet;
025:
026: import org.apache.cxf.interceptor.InterceptorChain;
027: import org.apache.cxf.message.Exchange;
028: import org.apache.cxf.message.FaultMode;
029: import org.apache.cxf.message.Message;
030: import org.apache.cxf.phase.Phase;
031: import org.apache.cxf.phase.PhaseInterceptorChain;
032: import org.apache.cxf.ws.addressing.AddressingPropertiesImpl;
033: import org.apache.cxf.ws.addressing.EndpointReferenceType;
034: import org.apache.cxf.ws.addressing.JAXWSAConstants;
035: import org.apache.cxf.ws.addressing.MAPAggregator;
036: import org.apache.cxf.ws.addressing.v200408.AttributedURI;
037: import org.easymock.classextension.EasyMock;
038: import org.easymock.classextension.IMocksControl;
039: import org.junit.Assert;
040: import org.junit.Before;
041: import org.junit.Test;
042:
043: public class RMOutInterceptorTest extends Assert {
044:
045: private IMocksControl control;
046:
047: @Before
048: public void setUp() {
049: control = EasyMock.createNiceControl();
050: }
051:
052: @Test
053: public void testOrdering() {
054: Phase p = new Phase(Phase.PRE_LOGICAL, 1);
055: SortedSet<Phase> phases = new TreeSet<Phase>();
056: phases.add(p);
057: PhaseInterceptorChain chain = new PhaseInterceptorChain(phases);
058: MAPAggregator map = new MAPAggregator();
059: RMOutInterceptor rmi = new RMOutInterceptor();
060: chain.add(rmi);
061: chain.add(map);
062: Iterator it = chain.iterator();
063: assertSame("Unexpected order.", map, it.next());
064: assertSame("Unexpected order.", rmi, it.next());
065: }
066:
067: @Test
068: public void testHandleRuntimeFault() throws NoSuchMethodException,
069: SequenceFault, RMException {
070: Method[] mocked = new Method[] { RMOutInterceptor.class
071: .getDeclaredMethod("isRuntimeFault",
072: new Class[] { Message.class }) };
073: RMOutInterceptor interceptor = control.createMock(
074: RMOutInterceptor.class, mocked);
075: Message message = control.createMock(Message.class);
076: EasyMock.expect(interceptor.isRuntimeFault(message)).andReturn(
077: true);
078: control.replay();
079: interceptor.handle(message);
080: control.verify();
081: }
082:
083: @Test
084: public void testHandleNoMAPs() throws NoSuchMethodException,
085: SequenceFault, RMException {
086: Method[] mocked = new Method[] { RMOutInterceptor.class
087: .getDeclaredMethod("isRuntimeFault",
088: new Class[] { Message.class }) };
089: RMOutInterceptor interceptor = control.createMock(
090: RMOutInterceptor.class, mocked);
091: Message message = control.createMock(Message.class);
092: EasyMock.expect(interceptor.isRuntimeFault(message)).andReturn(
093: false);
094: EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(
095: Boolean.FALSE).anyTimes();
096: EasyMock
097: .expect(
098: message
099: .get(JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_OUTBOUND))
100: .andReturn(null);
101: control.replay();
102: interceptor.handle(message);
103: control.verify();
104: }
105:
106: @Test
107: public void testHandleApplicationMessage()
108: throws NoSuchMethodException, SequenceFault, RMException {
109: AddressingPropertiesImpl maps = createMAPs("greetMe",
110: "localhost:9000/GreeterPort",
111: org.apache.cxf.ws.addressing.Names.WSA_NONE_ADDRESS);
112: Method[] mocked = new Method[] {
113: AbstractRMInterceptor.class.getDeclaredMethod(
114: "getManager", new Class[] {}),
115: RMOutInterceptor.class
116: .getDeclaredMethod("isRuntimeFault",
117: new Class[] { Message.class }),
118: RMOutInterceptor.class
119: .getDeclaredMethod("addAcknowledgements",
120: new Class[] { Destination.class,
121: RMProperties.class,
122: Identifier.class,
123: AttributedURI.class }) };
124: RMOutInterceptor interceptor = control.createMock(
125: RMOutInterceptor.class, mocked);
126: RMManager manager = control.createMock(RMManager.class);
127: EasyMock.expect(interceptor.getManager()).andReturn(manager)
128: .times(5);
129:
130: Message message = control.createMock(Message.class);
131: EasyMock.expect(interceptor.isRuntimeFault(message)).andReturn(
132: false);
133: Exchange ex = control.createMock(Exchange.class);
134: EasyMock.expect(message.getExchange()).andReturn(ex).times(3);
135: EasyMock.expect(ex.getOutMessage()).andReturn(message).times(1);
136: EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(
137: Boolean.TRUE).anyTimes();
138: EasyMock
139: .expect(
140: message
141: .get(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND))
142: .andReturn(maps).anyTimes();
143: RMProperties rmpsOut = new RMProperties();
144: EasyMock.expect(
145: message.get(RMMessageConstants.RM_PROPERTIES_OUTBOUND))
146: .andReturn(rmpsOut);
147: InterceptorChain chain = control
148: .createMock(InterceptorChain.class);
149: EasyMock.expect(message.getInterceptorChain()).andReturn(chain);
150: chain.add(EasyMock.isA(RetransmissionInterceptor.class));
151: EasyMock.expectLastCall();
152: RetransmissionQueue queue = control
153: .createMock(RetransmissionQueue.class);
154: EasyMock.expect(manager.getRetransmissionQueue()).andReturn(
155: queue);
156: queue.start();
157: EasyMock.expectLastCall();
158:
159: Source source = control.createMock(Source.class);
160: EasyMock.expect(manager.getSource(message)).andReturn(source);
161: Destination destination = control.createMock(Destination.class);
162: EasyMock.expect(manager.getDestination(message)).andReturn(
163: destination);
164: SourceSequence sseq = control.createMock(SourceSequence.class);
165: EasyMock.expect(
166: manager.getSequence((Identifier) EasyMock.isNull(),
167: EasyMock.same(message), EasyMock.same(maps)))
168: .andReturn(sseq);
169: EasyMock.expect(
170: sseq.nextMessageNumber((Identifier) EasyMock.isNull(),
171: (BigInteger) EasyMock.isNull(), EasyMock
172: .eq(false))).andReturn(BigInteger.TEN);
173: EasyMock.expect(sseq.isLastMessage()).andReturn(false).times(2);
174: interceptor.addAcknowledgements(EasyMock.same(destination),
175: EasyMock.same(rmpsOut), (Identifier) EasyMock.isNull(),
176: EasyMock.isA(AttributedURI.class));
177: EasyMock.expectLastCall();
178: Identifier sid = control.createMock(Identifier.class);
179: EasyMock.expect(sseq.getIdentifier()).andReturn(sid);
180: EasyMock.expect(sseq.getCurrentMessageNr()).andReturn(
181: BigInteger.TEN);
182:
183: control.replay();
184: interceptor.handle(message);
185: assertSame(sid, rmpsOut.getSequence().getIdentifier());
186: assertEquals(BigInteger.TEN, rmpsOut.getSequence()
187: .getMessageNumber());
188: assertNull(rmpsOut.getSequence().getLastMessage());
189: control.verify();
190: }
191:
192: @Test
193: public void testIsRuntimeFault() {
194: Message message = control.createMock(Message.class);
195: Exchange exchange = control.createMock(Exchange.class);
196: EasyMock.expect(message.getExchange()).andReturn(exchange)
197: .times(2);
198: EasyMock.expect(exchange.getOutFaultMessage()).andReturn(
199: message);
200: EasyMock.expect(message.get(FaultMode.class)).andReturn(
201: FaultMode.RUNTIME_FAULT);
202: control.replay();
203: RMOutInterceptor rmi = new RMOutInterceptor();
204: assertTrue(rmi.isRuntimeFault(message));
205: control.verify();
206: control.reset();
207: EasyMock.expect(message.getExchange()).andReturn(exchange)
208: .times(2);
209: EasyMock.expect(exchange.getOutFaultMessage()).andReturn(null);
210: control.replay();
211: assertTrue(!rmi.isRuntimeFault(message));
212: control.verify();
213: }
214:
215: private AddressingPropertiesImpl createMAPs(String action,
216: String to, String replyTo) {
217: AddressingPropertiesImpl maps = new AddressingPropertiesImpl();
218: maps.setTo(RMUtils.createReference(to));
219: EndpointReferenceType epr = RMUtils.createReference(replyTo);
220: maps.setReplyTo(epr);
221: return maps;
222:
223: }
224: }
|