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:
023: import org.apache.cxf.message.Exchange;
024: import org.apache.cxf.message.Message;
025: import org.apache.cxf.ws.addressing.AddressingPropertiesImpl;
026: import org.apache.cxf.ws.addressing.AttributedURIType;
027: import org.apache.cxf.ws.addressing.EndpointReferenceType;
028: import org.apache.cxf.ws.addressing.JAXWSAConstants;
029: import org.apache.cxf.ws.addressing.v200408.AttributedURI;
030: import org.apache.cxf.ws.rm.persistence.RMStore;
031: import org.easymock.classextension.EasyMock;
032: import org.easymock.classextension.IMocksControl;
033: import org.junit.After;
034: import org.junit.Assert;
035: import org.junit.Before;
036: import org.junit.Test;
037:
038: /**
039: *
040: */
041: public class DestinationTest extends Assert {
042:
043: private IMocksControl control;
044: private RMEndpoint rme;
045: private Destination destination;
046:
047: @Before
048: public void setUp() {
049: control = EasyMock.createNiceControl();
050: rme = control.createMock(RMEndpoint.class);
051: destination = new Destination(rme);
052: }
053:
054: @After
055: public void tearDown() {
056: control.verify();
057: }
058:
059: @Test
060: public void testGetSequence() {
061: Identifier id = control.createMock(Identifier.class);
062: String sid = "s1";
063: EasyMock.expect(id.getValue()).andReturn(sid);
064: control.replay();
065: assertNull(destination.getSequence(id));
066: }
067:
068: @Test
069: public void testGetAllSequences() {
070: control.replay();
071: assertEquals(0, destination.getAllSequences().size());
072: }
073:
074: @Test
075: public void testAddRemoveSequence() {
076: DestinationSequence ds = control
077: .createMock(DestinationSequence.class);
078: ds.setDestination(destination);
079: EasyMock.expectLastCall();
080: Identifier id = control.createMock(Identifier.class);
081: EasyMock.expect(ds.getIdentifier()).andReturn(id).times(3);
082: String sid = "s1";
083: EasyMock.expect(id.getValue()).andReturn(sid).times(3);
084: RMManager manager = control.createMock(RMManager.class);
085: EasyMock.expect(rme.getManager()).andReturn(manager).times(2);
086: RMStore store = control.createMock(RMStore.class);
087: EasyMock.expect(manager.getStore()).andReturn(store).times(2);
088: store.createDestinationSequence(ds);
089: EasyMock.expectLastCall();
090: store.removeDestinationSequence(id);
091: EasyMock.expectLastCall();
092: control.replay();
093: destination.addSequence(ds);
094: assertEquals(1, destination.getAllSequences().size());
095: assertSame(ds, destination.getSequence(id));
096: destination.removeSequence(ds);
097: assertEquals(0, destination.getAllSequences().size());
098: }
099:
100: @Test
101: public void testAcknowledgeNoSequence() throws SequenceFault,
102: RMException {
103: Message message = setupMessage();
104: RMProperties rmps = control.createMock(RMProperties.class);
105: EasyMock.expect(
106: message.get(RMMessageConstants.RM_PROPERTIES_INBOUND))
107: .andReturn(rmps);
108: EasyMock.expect(rmps.getSequence()).andReturn(null);
109: control.replay();
110: destination.acknowledge(message);
111: }
112:
113: @Test
114: public void testAcknowledgeUnknownSequence() throws RMException {
115: Message message = setupMessage();
116: RMProperties rmps = control.createMock(RMProperties.class);
117: EasyMock.expect(
118: message.get(RMMessageConstants.RM_PROPERTIES_INBOUND))
119: .andReturn(rmps);
120: SequenceType st = control.createMock(SequenceType.class);
121: EasyMock.expect(rmps.getSequence()).andReturn(st);
122: Identifier id = control.createMock(Identifier.class);
123: EasyMock.expect(st.getIdentifier()).andReturn(id).times(2);
124: String sid = "sid";
125: EasyMock.expect(id.getValue()).andReturn(sid);
126: control.replay();
127: try {
128: destination.acknowledge(message);
129: fail("Expected SequenceFault not thrown.");
130: } catch (SequenceFault ex) {
131: assertEquals(RMConstants.getUnknownSequenceFaultCode(), ex
132: .getSequenceFault().getFaultCode());
133: }
134: }
135:
136: @Test
137: public void testAcknowledgeAlreadyAcknowledgedMessage()
138: throws SequenceFault, RMException, NoSuchMethodException {
139:
140: Method m1 = Destination.class.getDeclaredMethod("getSequence",
141: new Class[] { Identifier.class });
142: destination = control.createMock(Destination.class,
143: new Method[] { m1 });
144: Message message = setupMessage();
145: RMProperties rmps = control.createMock(RMProperties.class);
146: EasyMock.expect(
147: message.get(RMMessageConstants.RM_PROPERTIES_INBOUND))
148: .andReturn(rmps);
149: SequenceType st = control.createMock(SequenceType.class);
150: EasyMock.expect(rmps.getSequence()).andReturn(st);
151: Identifier id = control.createMock(Identifier.class);
152: EasyMock.expect(st.getIdentifier()).andReturn(id);
153: DestinationSequence ds = control
154: .createMock(DestinationSequence.class);
155: EasyMock.expect(destination.getSequence(id)).andReturn(ds);
156: BigInteger nr = BigInteger.TEN;
157: EasyMock.expect(st.getMessageNumber()).andReturn(nr);
158: RMException ex = new RMException(new RuntimeException(
159: "already acknowledged"));
160: ds.applyDeliveryAssurance(nr);
161: EasyMock.expectLastCall().andThrow(ex);
162: control.replay();
163: try {
164: destination.acknowledge(message);
165: fail("Expected RMEcception not thrown.");
166: } catch (RMException e) {
167: assertSame(ex, e);
168: }
169:
170: }
171:
172: @Test
173: public void testAcknowledgeLastMessage() throws Exception {
174:
175: Method m1 = Destination.class.getDeclaredMethod("getSequence",
176: new Class[] { Identifier.class });
177: Method m2 = Destination.class.getMethod("getReliableEndpoint",
178: new Class[] {});
179:
180: destination = control.createMock(Destination.class,
181: new Method[] { m1, m2 });
182: Message message = setupMessage();
183: RMProperties rmps = control.createMock(RMProperties.class);
184: EasyMock.expect(
185: message.get(RMMessageConstants.RM_PROPERTIES_INBOUND))
186: .andReturn(rmps);
187: SequenceType st = control.createMock(SequenceType.class);
188: EasyMock.expect(rmps.getSequence()).andReturn(st);
189: Identifier id = control.createMock(Identifier.class);
190: EasyMock.expect(st.getIdentifier()).andReturn(id);
191: BigInteger nr = BigInteger.TEN;
192: EasyMock.expect(st.getMessageNumber()).andReturn(nr).times(2);
193: DestinationSequence ds = control
194: .createMock(DestinationSequence.class);
195: EasyMock.expect(destination.getSequence(id)).andReturn(ds);
196:
197: ds.applyDeliveryAssurance(nr);
198: EasyMock.expectLastCall();
199: ds.acknowledge(message);
200: EasyMock.expectLastCall();
201: SequenceType.LastMessage lm = control
202: .createMock(SequenceType.LastMessage.class);
203: EasyMock.expect(st.getLastMessage()).andReturn(lm);
204: ds.setLastMessageNumber(nr);
205: EasyMock.expectLastCall();
206: ds.scheduleImmediateAcknowledgement();
207: EasyMock.expectLastCall();
208: AddressingPropertiesImpl maps = control
209: .createMock(AddressingPropertiesImpl.class);
210: EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(
211: null);
212: EasyMock
213: .expect(
214: message
215: .get(JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_INBOUND))
216: .andReturn(maps);
217: EndpointReferenceType replyToEPR = control
218: .createMock(EndpointReferenceType.class);
219: EasyMock.expect(maps.getReplyTo()).andReturn(replyToEPR).times(
220: 2);
221: AttributedURIType replyToURI = control
222: .createMock(AttributedURIType.class);
223: EasyMock.expect(replyToEPR.getAddress()).andReturn(replyToURI);
224: String replyToAddress = "replyTo";
225: EasyMock.expect(replyToURI.getValue())
226: .andReturn(replyToAddress);
227: org.apache.cxf.ws.addressing.v200408.EndpointReferenceType acksToEPR = control
228: .createMock(org.apache.cxf.ws.addressing.v200408.EndpointReferenceType.class);
229: EasyMock.expect(ds.getAcksTo()).andReturn(acksToEPR);
230: AttributedURI acksToURI = control
231: .createMock(AttributedURI.class);
232: EasyMock.expect(acksToEPR.getAddress()).andReturn(acksToURI);
233: String acksToAddress = "acksTo";
234: EasyMock.expect(acksToURI.getValue()).andReturn(acksToAddress);
235: EasyMock.expect(ds.canPiggybackAckOnPartialResponse())
236: .andReturn(false);
237: EasyMock.expect(destination.getReliableEndpoint()).andReturn(
238: rme);
239: Proxy proxy = control.createMock(Proxy.class);
240: EasyMock.expect(rme.getProxy()).andReturn(proxy);
241: proxy.acknowledge(ds);
242: EasyMock.expectLastCall();
243:
244: control.replay();
245: destination.acknowledge(message);
246: }
247:
248: private Message setupMessage() {
249: Message message = control.createMock(Message.class);
250: Exchange exchange = control.createMock(Exchange.class);
251: EasyMock.expect(message.getExchange()).andReturn(exchange);
252: EasyMock.expect(exchange.getOutMessage()).andReturn(null);
253: EasyMock.expect(exchange.getOutFaultMessage()).andReturn(null);
254: return message;
255: }
256:
257: }
|