001: package org.objectweb.celtix.bus.ws.rm;
002:
003: import java.math.BigInteger;
004: import java.util.Date;
005:
006: import javax.xml.datatype.DatatypeConfigurationException;
007: import javax.xml.datatype.DatatypeFactory;
008: import javax.xml.datatype.Duration;
009:
010: import junit.framework.TestCase;
011:
012: import org.objectweb.celtix.bus.configuration.wsrm.AcksPolicyType;
013: import org.objectweb.celtix.bus.configuration.wsrm.SequenceTerminationPolicyType;
014: import org.objectweb.celtix.bus.configuration.wsrm.SourcePolicyType;
015: import org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType;
016: import org.objectweb.celtix.ws.rm.Expires;
017: import org.objectweb.celtix.ws.rm.Identifier;
018: import org.objectweb.celtix.ws.rm.ObjectFactory;
019: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
020: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement.AcknowledgementRange;
021: import org.objectweb.celtix.ws.rm.policy.RMAssertionType;
022: import org.objectweb.celtix.ws.rm.policy.RMAssertionType.BaseRetransmissionInterval;
023: import org.objectweb.celtix.ws.rm.policy.RMAssertionType.ExponentialBackoff;
024: import org.objectweb.celtix.ws.rm.wsdl.SequenceFault;
025:
026: import static org.easymock.EasyMock.expectLastCall;
027: import static org.easymock.classextension.EasyMock.createMock;
028: import static org.easymock.classextension.EasyMock.replay;
029: import static org.easymock.classextension.EasyMock.reset;
030: import static org.easymock.classextension.EasyMock.verify;
031:
032: public class SourceSequenceTest extends TestCase {
033:
034: ObjectFactory factory = new ObjectFactory();
035: Identifier id;
036: EndpointReferenceType ref;
037: RMSource source;
038: RMDestination destination;
039: RMHandler handler;
040: RMAssertionType rma;
041: AcksPolicyType ap;
042:
043: public void setUp() {
044:
045: id = factory.createIdentifier();
046: id.setValue("seq");
047: ref = createMock(EndpointReferenceType.class);
048: source = createMock(RMSource.class);
049: destination = createMock(RMDestination.class);
050: handler = createMock(RMHandler.class);
051: ap = RMUtils.getWSRMConfFactory().createAcksPolicyType();
052: rma = RMUtils.getWSRMPolicyFactory().createRMAssertionType();
053: BaseRetransmissionInterval bri = RMUtils.getWSRMPolicyFactory()
054: .createRMAssertionTypeBaseRetransmissionInterval();
055: bri.setMilliseconds(new BigInteger("3000"));
056: rma.setBaseRetransmissionInterval(bri);
057: ExponentialBackoff eb = RMUtils.getWSRMPolicyFactory()
058: .createRMAssertionTypeExponentialBackoff();
059: eb.getOtherAttributes().put(
060: RetransmissionQueue.EXPONENTIAL_BACKOFF_BASE_ATTR,
061: RetransmissionQueue.DEFAULT_EXPONENTIAL_BACKOFF);
062:
063: }
064:
065: public void testConstructors() {
066:
067: Identifier otherId = factory.createIdentifier();
068: otherId.setValue("otherSeq");
069:
070: SourceSequence seq = null;
071:
072: seq = new SourceSequence(id);
073: assertEquals(id, seq.getIdentifier());
074: assertTrue(!seq.isLastMessage());
075: assertTrue(!seq.isExpired());
076: assertEquals(BigInteger.ZERO, seq.getCurrentMessageNr());
077: assertNotNull(seq.getAcknowledgement());
078: assertEquals(0, seq.getAcknowledgement()
079: .getAcknowledgementRange().size());
080: assertTrue(!seq.allAcknowledged());
081: assertFalse(seq.offeredBy(otherId));
082:
083: Date expiry = new Date(System.currentTimeMillis() + 3600 * 1000);
084:
085: seq = new SourceSequence(id, expiry, null);
086: assertEquals(id, seq.getIdentifier());
087: assertTrue(!seq.isLastMessage());
088: assertTrue(!seq.isExpired());
089: assertEquals(BigInteger.ZERO, seq.getCurrentMessageNr());
090: assertNotNull(seq.getAcknowledgement());
091: assertEquals(0, seq.getAcknowledgement()
092: .getAcknowledgementRange().size());
093: assertTrue(!seq.allAcknowledged());
094: assertFalse(seq.offeredBy(otherId));
095:
096: seq = new SourceSequence(id, expiry, otherId);
097: assertTrue(seq.offeredBy(otherId));
098: assertFalse(seq.offeredBy(id));
099: }
100:
101: public void testSetExpires() throws DatatypeConfigurationException {
102: SourceSequence seq = new SourceSequence(id);
103:
104: Expires expires = factory.createExpires();
105:
106: DatatypeFactory dbf = DatatypeFactory.newInstance();
107: Duration d = dbf.newDuration(0);
108: expires.setValue(d);
109: seq.setExpires(expires);
110: assertTrue(!seq.isExpired());
111:
112: d = dbf.newDuration("PT0S");
113: expires.setValue(d);
114: seq.setExpires(expires);
115: assertTrue(!seq.isExpired());
116:
117: d = dbf.newDuration(1000);
118: expires.setValue(d);
119: seq.setExpires(expires);
120: assertTrue(!seq.isExpired());
121:
122: d = dbf.newDuration("-PT1S");
123: expires.setValue(d);
124: seq.setExpires(expires);
125: assertTrue(seq.isExpired());
126: }
127:
128: public void testEqualsAndHashCode() {
129: SourceSequence seq = new SourceSequence(id);
130: SourceSequence otherSeq = null;
131: assertTrue(!seq.equals(otherSeq));
132: otherSeq = new SourceSequence(id);
133: assertEquals(seq, otherSeq);
134: assertEquals(seq.hashCode(), otherSeq.hashCode());
135: Identifier otherId = factory.createIdentifier();
136: otherId.setValue("otherSeq");
137: otherSeq = new SourceSequence(otherId);
138: assertTrue(!seq.equals(otherSeq));
139: assertTrue(seq.hashCode() != otherSeq.hashCode());
140: assertTrue(!seq.equals(this ));
141: }
142:
143: public void testSetAcknowledged() {
144: SourceSequence seq = new SourceSequence(id);
145: SequenceAcknowledgement ack = seq.getAcknowledgement();
146: ack = RMUtils.getWSRMFactory().createSequenceAcknowledgement();
147: AcknowledgementRange r = RMUtils.getWSRMFactory()
148: .createSequenceAcknowledgementAcknowledgementRange();
149: r.setLower(new BigInteger("1"));
150: r.setUpper(new BigInteger("2"));
151: ack.getAcknowledgementRange().add(r);
152: r = RMUtils.getWSRMFactory()
153: .createSequenceAcknowledgementAcknowledgementRange();
154: r.setLower(new BigInteger("4"));
155: r.setUpper(new BigInteger("6"));
156: ack.getAcknowledgementRange().add(r);
157: r = RMUtils.getWSRMFactory()
158: .createSequenceAcknowledgementAcknowledgementRange();
159: r.setLower(new BigInteger("8"));
160: r.setUpper(new BigInteger("10"));
161: ack.getAcknowledgementRange().add(r);
162: seq.setAcknowledged(ack);
163: assertSame(ack, seq.getAcknowledgement());
164: assertEquals(3, ack.getAcknowledgementRange().size());
165: assertTrue(!seq.isAcknowledged(new BigInteger("3")));
166: }
167:
168: public void testAllAcknowledged() throws SequenceFault {
169:
170: SourceSequence seq = new SourceSequence(id, null, null,
171: new BigInteger("4"), false);
172:
173: assertTrue(!seq.allAcknowledged());
174: seq.setLastMessage(true);
175: assertTrue(!seq.allAcknowledged());
176: SequenceAcknowledgement ack = RMUtils.getWSRMFactory()
177: .createSequenceAcknowledgement();
178: AcknowledgementRange r = RMUtils.getWSRMFactory()
179: .createSequenceAcknowledgementAcknowledgementRange();
180: r.setLower(BigInteger.ONE);
181: r.setUpper(new BigInteger("2"));
182: ack.getAcknowledgementRange().add(r);
183: seq.setAcknowledged(ack);
184: assertTrue(!seq.allAcknowledged());
185: r.setUpper(new BigInteger("4"));
186: assertTrue(seq.allAcknowledged());
187: }
188:
189: public void testNextMessageNumber() {
190: SourceSequence seq = null;
191: SourcePolicyType sp = RMUtils.getWSRMConfFactory()
192: .createSourcePolicyType();
193:
194: // default termination policy
195:
196: SequenceTerminationPolicyType stp = RMUtils
197: .getWSRMConfFactory()
198: .createSequenceTerminationPolicyType();
199: sp.setSequenceTerminationPolicy(stp);
200:
201: seq = new SourceSequence(id);
202: seq.setSource(source);
203: assertTrue(!nextSequences(seq, sp, 10));
204:
205: // termination policy max length = 1
206:
207: seq = new SourceSequence(id);
208: seq.setSource(source);
209: stp.setMaxLength(BigInteger.ONE);
210: assertTrue(nextSequences(seq, sp, 10));
211: assertEquals(BigInteger.ONE, seq.getCurrentMessageNr());
212:
213: // termination policy max length = 5
214: seq = new SourceSequence(id);
215: seq.setSource(source);
216: stp.setMaxLength(new BigInteger("5"));
217: assertTrue(!nextSequences(seq, sp, 2));
218:
219: // termination policy max range exceeded
220:
221: seq = new SourceSequence(id);
222: seq.setSource(source);
223: stp.setMaxLength(null);
224: stp.setMaxRanges(new Integer(3));
225: acknowledge(seq, 1, 2, 4, 5, 6, 8, 9, 10);
226: assertTrue(nextSequences(seq, sp, 10));
227: assertEquals(BigInteger.ONE, seq.getCurrentMessageNr());
228:
229: // termination policy max range not exceeded
230:
231: seq = new SourceSequence(id);
232: seq.setSource(source);
233: stp.setMaxLength(null);
234: stp.setMaxRanges(new Integer(4));
235: acknowledge(seq, 1, 2, 4, 5, 6, 8, 9, 10);
236: assertTrue(!nextSequences(seq, sp, 10));
237:
238: // termination policy max unacknowledged
239: }
240:
241: private boolean nextSequences(SourceSequence seq,
242: SourcePolicyType sp, int n) {
243: reset(source);
244: source.getSequenceTerminationPolicy();
245: expectLastCall().andReturn(sp.getSequenceTerminationPolicy());
246: replay(source);
247:
248: int i = 0;
249: while ((i < n) && !seq.isLastMessage()) {
250: assertNotNull(seq.nextMessageNumber());
251: verify(source);
252: reset(source);
253: source.getSequenceTerminationPolicy();
254: expectLastCall().andReturn(
255: sp.getSequenceTerminationPolicy());
256: replay(source);
257: i++;
258: }
259: return seq.isLastMessage();
260: }
261:
262: // this method cannot be private because of a bug in PMD which otherwise
263: // would report it as an 'unused private method'
264: protected void acknowledge(SourceSequence seq,
265: int... messageNumbers) {
266: SequenceAcknowledgement ack = factory
267: .createSequenceAcknowledgement();
268: int i = 0;
269: while (i < messageNumbers.length) {
270: AcknowledgementRange r = factory
271: .createSequenceAcknowledgementAcknowledgementRange();
272: Integer li = new Integer(messageNumbers[i]);
273: BigInteger l = new BigInteger(li.toString());
274: r.setLower(l);
275: i++;
276:
277: while (i < messageNumbers.length
278: && (messageNumbers[i] - messageNumbers[i - 1]) == 1) {
279: i++;
280: }
281: Integer ui = new Integer(messageNumbers[i - 1]);
282: BigInteger u = new BigInteger(ui.toString());
283: r.setUpper(u);
284: ack.getAcknowledgementRange().add(r);
285: }
286: seq.setAcknowledged(ack);
287: }
288:
289: }
|