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.math.BigInteger;
021: import java.util.Date;
022:
023: import javax.xml.datatype.Duration;
024:
025: import org.apache.cxf.jaxb.DatatypeFactory;
026: import org.apache.cxf.ws.rm.manager.SequenceTerminationPolicyType;
027: import org.apache.cxf.ws.rm.manager.SourcePolicyType;
028: import org.easymock.classextension.EasyMock;
029: import org.easymock.classextension.IMocksControl;
030: import org.junit.After;
031: import org.junit.Assert;
032: import org.junit.Before;
033: import org.junit.Test;
034:
035: public class SourceSequenceTest extends Assert {
036:
037: private IMocksControl control;
038: private ObjectFactory factory;
039: private Identifier id;
040:
041: private Source source;
042: private RMManager manager;
043: private SourcePolicyType sp;
044: private SequenceTerminationPolicyType stp;
045: private RetransmissionQueue rq;
046:
047: @Before
048: public void setUp() {
049: factory = new ObjectFactory();
050: id = factory.createIdentifier();
051: id.setValue("seq");
052:
053: control = EasyMock.createNiceControl();
054: }
055:
056: @After
057: public void tearDown() {
058: source = null;
059: manager = null;
060: sp = null;
061: stp = null;
062: rq = null;
063: }
064:
065: void setUpSource() {
066: source = control.createMock(Source.class);
067: manager = control.createMock(RMManager.class);
068: EasyMock.expect(source.getManager()).andReturn(manager)
069: .anyTimes();
070: rq = control.createMock(RetransmissionQueue.class);
071: EasyMock.expect(manager.getRetransmissionQueue()).andReturn(rq)
072: .anyTimes();
073:
074: // default termination policy
075:
076: org.apache.cxf.ws.rm.manager.ObjectFactory cfgFactory = new org.apache.cxf.ws.rm.manager.ObjectFactory();
077: sp = cfgFactory.createSourcePolicyType();
078: stp = cfgFactory.createSequenceTerminationPolicyType();
079: sp.setSequenceTerminationPolicy(stp);
080: EasyMock.expect(manager.getSourcePolicy()).andReturn(sp)
081: .anyTimes();
082: }
083:
084: @Test
085: public void testConstructors() {
086:
087: Identifier otherId = factory.createIdentifier();
088: otherId.setValue("otherSeq");
089:
090: SourceSequence seq = null;
091:
092: seq = new SourceSequence(id);
093: assertEquals(id, seq.getIdentifier());
094: assertTrue(!seq.isLastMessage());
095: assertTrue(!seq.isExpired());
096: assertEquals(BigInteger.ZERO, seq.getCurrentMessageNr());
097: assertNotNull(seq.getAcknowledgement());
098: assertEquals(0, seq.getAcknowledgement()
099: .getAcknowledgementRange().size());
100: assertTrue(!seq.allAcknowledged());
101: assertFalse(seq.offeredBy(otherId));
102:
103: Date expiry = new Date(System.currentTimeMillis() + 3600 * 1000);
104:
105: seq = new SourceSequence(id, expiry, null);
106: assertEquals(id, seq.getIdentifier());
107: assertTrue(!seq.isLastMessage());
108: assertTrue(!seq.isExpired());
109: assertEquals(BigInteger.ZERO, seq.getCurrentMessageNr());
110: assertNotNull(seq.getAcknowledgement());
111: assertEquals(0, seq.getAcknowledgement()
112: .getAcknowledgementRange().size());
113: assertTrue(!seq.allAcknowledged());
114: assertFalse(seq.offeredBy(otherId));
115:
116: seq = new SourceSequence(id, expiry, otherId);
117: assertTrue(seq.offeredBy(otherId));
118: assertFalse(seq.offeredBy(id));
119: }
120:
121: @Test
122: public void testSetExpires() {
123: SourceSequence seq = new SourceSequence(id);
124:
125: Expires expires = factory.createExpires();
126: seq.setExpires(expires);
127:
128: assertTrue(!seq.isExpired());
129:
130: Duration d = DatatypeFactory.PT0S;
131: expires.setValue(d);
132: seq.setExpires(expires);
133: try {
134: Thread.sleep(1000);
135: } catch (InterruptedException ex) {
136: assertTrue(!seq.isExpired());
137: }
138:
139: d = DatatypeFactory.createDuration("PT1S");
140: expires.setValue(d);
141: seq.setExpires(expires);
142: assertTrue(!seq.isExpired());
143:
144: d = DatatypeFactory.createDuration("-PT1S");
145: expires.setValue(d);
146: seq.setExpires(expires);
147: assertTrue(seq.isExpired());
148: }
149:
150: @Test
151: public void testEqualsAndHashCode() {
152: SourceSequence seq = new SourceSequence(id);
153: SourceSequence otherSeq = null;
154: assertTrue(!seq.equals(otherSeq));
155: otherSeq = new SourceSequence(id);
156: assertEquals(seq, otherSeq);
157: assertEquals(seq.hashCode(), otherSeq.hashCode());
158: Identifier otherId = factory.createIdentifier();
159: otherId.setValue("otherSeq");
160: otherSeq = new SourceSequence(otherId);
161: assertTrue(!seq.equals(otherSeq));
162: assertTrue(seq.hashCode() != otherSeq.hashCode());
163: assertTrue(!seq.equals(this ));
164: }
165:
166: @Test
167: public void testSetAcknowledged() throws RMException {
168: SourceSequence seq = new SourceSequence(id);
169: setUpSource();
170: seq.setSource(source);
171:
172: SequenceAcknowledgement ack = seq.getAcknowledgement();
173: ack = factory.createSequenceAcknowledgement();
174: SequenceAcknowledgement.AcknowledgementRange r = factory
175: .createSequenceAcknowledgementAcknowledgementRange();
176: r.setLower(new BigInteger("1"));
177: r.setUpper(new BigInteger("2"));
178: ack.getAcknowledgementRange().add(r);
179: r = factory.createSequenceAcknowledgementAcknowledgementRange();
180: r.setLower(new BigInteger("4"));
181: r.setUpper(new BigInteger("6"));
182: ack.getAcknowledgementRange().add(r);
183: r = factory.createSequenceAcknowledgementAcknowledgementRange();
184: r.setLower(new BigInteger("8"));
185: r.setUpper(new BigInteger("10"));
186: ack.getAcknowledgementRange().add(r);
187: rq.purgeAcknowledged(seq);
188: EasyMock.expectLastCall();
189:
190: control.replay();
191: seq.setAcknowledged(ack);
192: assertSame(ack, seq.getAcknowledgement());
193: assertEquals(3, ack.getAcknowledgementRange().size());
194: assertTrue(!seq.isAcknowledged(new BigInteger("3")));
195: assertTrue(seq.isAcknowledged(new BigInteger("5")));
196: control.verify();
197: }
198:
199: @Test
200: public void testAllAcknowledged() throws SequenceFault, RMException {
201:
202: SourceSequence seq = new SourceSequence(id, null, null,
203: new BigInteger("4"), false);
204: setUpSource();
205: seq.setSource(source);
206:
207: assertTrue(!seq.allAcknowledged());
208: seq.setLastMessage(true);
209: assertTrue(!seq.allAcknowledged());
210: SequenceAcknowledgement ack = factory
211: .createSequenceAcknowledgement();
212: SequenceAcknowledgement.AcknowledgementRange r = factory
213: .createSequenceAcknowledgementAcknowledgementRange();
214: r.setLower(BigInteger.ONE);
215: r.setUpper(new BigInteger("2"));
216: ack.getAcknowledgementRange().add(r);
217: rq.purgeAcknowledged(seq);
218: EasyMock.expectLastCall();
219:
220: control.replay();
221: seq.setAcknowledged(ack);
222: assertTrue(!seq.allAcknowledged());
223: r.setUpper(new BigInteger("4"));
224: assertTrue(seq.allAcknowledged());
225: control.verify();
226: }
227:
228: @Test
229: public void testNextMessageNumber() throws RMException {
230: SourceSequence seq = null;
231: setUpSource();
232: rq.purgeAcknowledged(EasyMock.isA(SourceSequence.class));
233: EasyMock.expectLastCall().anyTimes();
234: control.replay();
235:
236: // default termination policy
237:
238: seq = new SourceSequence(id);
239: seq.setSource(source);
240: assertTrue(!nextMessages(seq, 10));
241: control.verify();
242:
243: // termination policy max length = 1
244:
245: seq = new SourceSequence(id);
246: seq.setSource(source);
247: stp.setMaxLength(BigInteger.ONE);
248: assertTrue(nextMessages(seq, 10));
249: assertEquals(BigInteger.ONE, seq.getCurrentMessageNr());
250: control.verify();
251:
252: // termination policy max length = 5
253: seq = new SourceSequence(id);
254: seq.setSource(source);
255: stp.setMaxLength(new BigInteger("5"));
256: assertTrue(!nextMessages(seq, 2));
257: control.verify();
258:
259: // termination policy max range exceeded
260:
261: seq = new SourceSequence(id);
262: seq.setSource(source);
263: stp.setMaxLength(null);
264: stp.setMaxRanges(new Integer(3));
265: acknowledge(seq, 1, 2, 4, 5, 6, 8, 9, 10);
266: assertTrue(nextMessages(seq, 10));
267: assertEquals(BigInteger.ONE, seq.getCurrentMessageNr());
268: control.verify();
269:
270: // termination policy max range not exceeded
271:
272: seq = new SourceSequence(id);
273: seq.setSource(source);
274: stp.setMaxLength(null);
275: stp.setMaxRanges(new Integer(4));
276: acknowledge(seq, 1, 2, 4, 5, 6, 8, 9, 10);
277: assertTrue(!nextMessages(seq, 10));
278: control.verify();
279:
280: // termination policy max unacknowledged
281: }
282:
283: @Test
284: public void testGetEndpointIdentfier() {
285: setUpSource();
286: String name = "abc";
287: EasyMock.expect(source.getName()).andReturn(name);
288: control.replay();
289:
290: SourceSequence seq = new SourceSequence(id);
291: seq.setSource(source);
292: assertEquals("Unexpected endpoint identifier", name, seq
293: .getEndpointIdentifier());
294: control.verify();
295: }
296:
297: @Test
298: public void testCheckOfferingSequenceClosed() {
299: SourceSequence seq = null;
300:
301: setUpSource();
302:
303: RMEndpoint rme = control.createMock(RMEndpoint.class);
304: EasyMock.expect(source.getReliableEndpoint()).andReturn(rme)
305: .anyTimes();
306: Destination destination = control.createMock(Destination.class);
307: EasyMock.expect(rme.getDestination()).andReturn(destination)
308: .anyTimes();
309: DestinationSequence dseq = control
310: .createMock(DestinationSequence.class);
311: Identifier did = control.createMock(Identifier.class);
312: EasyMock.expect(destination.getSequence(did)).andReturn(dseq)
313: .anyTimes();
314: EasyMock.expect(dseq.getLastMessageNumber()).andReturn(
315: BigInteger.ONE).anyTimes();
316: EasyMock.expect(did.getValue()).andReturn("dseq").anyTimes();
317:
318: control.replay();
319:
320: seq = new SourceSequence(id, null, did);
321: seq.setSource(source);
322: seq.nextMessageNumber(did, BigInteger.ONE, false);
323: assertTrue(seq.isLastMessage());
324:
325: control.verify();
326: }
327:
328: private boolean nextMessages(SourceSequence seq, int n) {
329: int i = 0;
330: while ((i < n) && !seq.isLastMessage()) {
331: assertNotNull(seq.nextMessageNumber());
332: i++;
333: }
334: return seq.isLastMessage();
335: }
336:
337: protected void acknowledge(SourceSequence seq,
338: int... messageNumbers) throws RMException {
339: SequenceAcknowledgement ack = factory
340: .createSequenceAcknowledgement();
341: int i = 0;
342: while (i < messageNumbers.length) {
343: SequenceAcknowledgement.AcknowledgementRange r = factory
344: .createSequenceAcknowledgementAcknowledgementRange();
345: Integer li = new Integer(messageNumbers[i]);
346: BigInteger l = new BigInteger(li.toString());
347: r.setLower(l);
348: i++;
349:
350: while (i < messageNumbers.length
351: && (messageNumbers[i] - messageNumbers[i - 1]) == 1) {
352: i++;
353: }
354: Integer ui = new Integer(messageNumbers[i - 1]);
355: BigInteger u = new BigInteger(ui.toString());
356: r.setUpper(u);
357: ack.getAcknowledgementRange().add(r);
358: }
359: seq.setAcknowledged(ack);
360: }
361: }
|