001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import java.util.ArrayList;
008: import java.util.Random;
009:
010: import junit.framework.TestCase;
011:
012: public class SequenceValidatorTest extends TestCase {
013:
014: private static final int INITIAL_VALUE = 0;
015:
016: private SequenceValidator sv;
017:
018: public void setUp() throws Exception {
019: this .sv = new SequenceValidator(INITIAL_VALUE);
020: }
021:
022: public void tests() throws Exception {
023:
024: SequenceID one = new SequenceID(1);
025: SequenceID two = new SequenceID(2);
026: SequenceID three = new SequenceID(3);
027:
028: Object key1 = new Object();
029: Object key2 = new Object();
030: assertEquals(0, sv.size());
031: assertTrue(sv.isNext(key1, one));
032: assertTrue(sv.isNext(key2, one));
033: assertFalse(sv.isNext(key1, two));
034: assertFalse(sv.isNext(key2, two));
035:
036: sv.setCurrent(key1, one);
037:
038: assertTrue(sv.isNext(key2, one));
039: assertFalse(sv.isNext(key2, two));
040: assertTrue(sv.isNext(key1, two));
041: assertFalse(sv.isNext(key1, three));
042:
043: sv.remove(key1);
044: assertEquals(1, sv.size());
045: sv.remove(key2);
046: assertEquals(0, sv.size());
047: assertTrue(sv.isNext(key1, one));
048: assertTrue(sv.isNext(key2, one));
049: }
050:
051: public void testSetCurrent() throws Exception {
052:
053: Object key = new Object();
054: SequenceID sid = new SequenceID(1);
055: for (int i = 0; i < 50; i++) {
056: assertTrue(sv.isNext(key, sid));
057: sv.setCurrent(key, sid);
058: sid = sid.next();
059: try {
060: sv.setCurrent(key, sid.next());
061: fail();
062: } catch (InvalidSequenceIDException er) {
063: // expected
064: }
065: }
066: }
067:
068: public void testDisjointSequences() throws Exception {
069:
070: int start = 100;
071: ArrayList sequences = createRandomSequences(start);
072:
073: Object key1 = new Object();
074:
075: assertEquals(0, sv.size());
076: sv.initSequence(key1, sequences);
077:
078: try {
079: sv.initSequence(key1, new ArrayList());
080: fail("Should have thrown an Exception");
081: } catch (TCAssertionError er) {
082: // expected
083: }
084:
085: SequenceID current = sv.getCurrent(key1);
086: assertTrue(current.toLong() == INITIAL_VALUE);
087:
088: while (sequences.size() > 0) {
089: SequenceID next = new SequenceID(start);
090: if (sequences.contains(next)) {
091: assertTrue(sv.isNext(key1, next));
092: sequences.remove(next);
093: sv.setCurrent(key1, next);
094: } else {
095: assertFalse(sv.isNext(key1, next));
096: }
097: start++;
098: }
099: int i = 100;
100: while (i-- > 0) {
101: SequenceID next = new SequenceID(start);
102: assertTrue(sv.isNext(key1, next));
103: sv.setCurrent(key1, next);
104: start++;
105: }
106:
107: }
108:
109: private ArrayList createRandomSequences(int inital) {
110: Random r = new Random();
111: ArrayList sequences = new ArrayList();
112: int count = r.nextInt(100) + 1;
113: while (count-- > 0) {
114: sequences.add(new SequenceID(inital));
115: inital += r.nextInt(5) + 1;
116: }
117: return sequences;
118: }
119: }
|