001: package org.jgroups.tests;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.Address;
007: import org.jgroups.stack.IpAddress;
008: import org.jgroups.util.Rsp;
009: import org.jgroups.util.RspList;
010:
011: import java.util.*;
012:
013: public class RspListTest extends TestCase {
014: RspList rl;
015: Address a1, a2, a3, a4, a5;
016: Rsp rsp1, rsp2, rsp3, rsp4, rsp5;
017:
018: public RspListTest(String name) {
019: super (name);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: rl = new RspList();
025: a1 = new IpAddress(1111);
026: a2 = new IpAddress(2222);
027: a3 = new IpAddress(3333);
028: a4 = new IpAddress(4444);
029: a5 = new IpAddress(5555);
030: rsp1 = new Rsp(a1);
031: rsp2 = new Rsp(a2, true);
032: rsp3 = new Rsp(a3, "hello world");
033: rsp4 = new Rsp(a4, Boolean.TRUE);
034: rsp5 = new Rsp(a5, true);
035: rl.put(a1, rsp1);
036: rl.put(a2, rsp2);
037: rl.put(a3, rsp3);
038: rl.put(a4, rsp4);
039: rl.put(a5, rsp5);
040: }
041:
042: protected void tearDown() throws Exception {
043: rl.clear();
044: super .tearDown();
045: }
046:
047: public void testConstructor() {
048: Collection c = new LinkedList();
049: c.add(rsp1);
050: c.add(rsp2);
051: c.add(rsp3);
052: RspList tmp = new RspList(c);
053: assertEquals(c.size(), tmp.size());
054: assertTrue(tmp.containsKey(a1));
055: assertTrue(tmp.containsKey(a2));
056: assertTrue(tmp.containsKey(a3));
057: assertTrue(tmp.containsValue(rsp1));
058: assertTrue(tmp.containsValue(rsp2));
059: assertTrue(tmp.containsValue(rsp3));
060: }
061:
062: public void testIsEmpty() {
063: RspList tmp = new RspList();
064: assertTrue(tmp.isEmpty());
065: tmp.addRsp(a1, rsp1);
066: assertFalse(tmp.isEmpty());
067: }
068:
069: public void testContainsKey() {
070: assertTrue(rl.containsKey(a1));
071: assertTrue(rl.containsKey(a3));
072: }
073:
074: public void testContainsValue() {
075: assertTrue(rl.containsValue(rsp1));
076: assertTrue(rl.containsValue(rsp3));
077: }
078:
079: public void testGet() {
080: Rsp rsp = (Rsp) rl.get(a1);
081: assertEquals(rsp, rsp1);
082: rsp = (Rsp) rl.get(a3);
083: assertEquals(rsp, rsp3);
084: }
085:
086: public void testPut() {
087: Rsp rsp;
088: rsp = (Rsp) rl.put(new IpAddress(6666), new Rsp(new IpAddress(
089: 6666), true));
090: assertNull(rsp);
091: rsp = (Rsp) rl.put(a2, rsp2);
092: assertEquals(rsp, rsp2);
093: assertEquals(6, rl.size());
094: }
095:
096: public void testRemove() {
097: Rsp rsp;
098: rsp = (Rsp) rl.remove(new IpAddress(6666));
099: assertNull(rsp);
100: rsp = (Rsp) rl.remove(a2);
101: assertEquals(rsp, rsp2);
102: assertEquals(4, rl.size());
103: }
104:
105: public void testClear() {
106: rl.clear();
107: assertEquals(0, rl.size());
108: }
109:
110: public void testKeySet() {
111: RspList tmp = new RspList();
112: Set keys = tmp.keySet();
113: assertNotNull(keys);
114: assertEquals(0, keys.size());
115: }
116:
117: public void testKeySet2() {
118: Set keys = rl.keySet();
119: assertNotNull(keys);
120: assertEquals(rl.size(), keys.size());
121: }
122:
123: public void testAddRsp() {
124: rl.addRsp(new IpAddress(6666), new Integer(322649));
125: assertEquals(6, rl.size());
126: Rsp rsp = (Rsp) rl.get(new IpAddress(6666));
127: assertNotNull(rsp);
128: assertTrue(rsp.wasReceived());
129: assertFalse(rsp.wasSuspected());
130: assertEquals(new Integer(322649), rsp.getValue());
131: }
132:
133: public void testAddRsp2() {
134: rl.addRsp(a1, new Integer(322649));
135: assertEquals(5, rl.size());
136: Rsp rsp = (Rsp) rl.get(a1);
137: assertNotNull(rsp);
138: assertTrue(rsp.wasReceived());
139: assertFalse(rsp.wasSuspected());
140: assertEquals(new Integer(322649), rsp.getValue());
141: }
142:
143: public void testNumSuspectedMembers() {
144: assertEquals(2, rl.numSuspectedMembers());
145: }
146:
147: public void testGetFirst() {
148: Object obj = rl.getFirst();
149: System.out.println("-- first (non-null) value is " + obj);
150: assertNotNull(obj);
151: }
152:
153: public void testGetResults() {
154: Vector v = rl.getResults();
155: assertNotNull(v);
156: assertEquals(2, v.size());
157: }
158:
159: public void testElementAt() {
160: Rsp rsp;
161: Set s = new HashSet();
162: for (int i = 0; i < rl.size(); i++) {
163: rsp = (Rsp) rl.elementAt(i);
164: s.add(rsp.getSender());
165: }
166: System.out.println("-- set is " + s);
167: assertEquals(rl.size(), s.size());
168: }
169:
170: public void testElementAtWithOOBEx() {
171: try {
172: rl.elementAt(6);
173: fail("this should have thrown an ArrayIndexOutOfBoundsException");
174: } catch (ArrayIndexOutOfBoundsException ex) {
175: }
176: }
177:
178: public static Test suite() {
179: return new TestSuite(RspListTest.class);
180: }
181:
182: public static void main(String[] args) {
183: junit.textui.TestRunner.run(RspListTest.suite());
184: }
185: }
|