KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > RspListTest


1 package org.jgroups.tests;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jgroups.Address;
7 import org.jgroups.stack.IpAddress;
8 import org.jgroups.util.Rsp;
9 import org.jgroups.util.RspList;
10
11 import java.util.*;
12
13 public class RspListTest extends TestCase {
14     RspList rl;
15     Address a1, a2, a3, a4, a5;
16     Rsp rsp1, rsp2, rsp3, rsp4, rsp5;
17
18     public RspListTest(String JavaDoc name) {
19         super(name);
20     }
21
22
23     public void setUp() throws Exception JavaDoc {
24         super.setUp();
25         rl=new RspList();
26         a1=new IpAddress(1111);
27         a2=new IpAddress(2222);
28         a3=new IpAddress(3333);
29         a4=new IpAddress(4444);
30         a5=new IpAddress(5555);
31         rsp1=new Rsp(a1);
32         rsp2=new Rsp(a2, true);
33         rsp3=new Rsp(a3, "hello world");
34         rsp4=new Rsp(a4, Boolean.TRUE);
35         rsp5=new Rsp(a5, true);
36         rl.put(a1, rsp1);
37         rl.put(a2, rsp2);
38         rl.put(a3, rsp3);
39         rl.put(a4, rsp4);
40         rl.put(a5, rsp5);
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44         rl.clear();
45         super.tearDown();
46     }
47
48     public void testConstructor() {
49         Collection c=new LinkedList();
50         c.add(rsp1); c.add(rsp2); c.add(rsp3);
51         RspList tmp=new RspList(c);
52         assertEquals(c.size(), tmp.size());
53         assertTrue(tmp.containsKey(a1));
54         assertTrue(tmp.containsKey(a2));
55         assertTrue(tmp.containsKey(a3));
56         assertTrue(tmp.containsValue(rsp1));
57         assertTrue(tmp.containsValue(rsp2));
58         assertTrue(tmp.containsValue(rsp3));
59     }
60
61     public void testIsEmpty() {
62         RspList tmp=new RspList();
63         assertTrue(tmp.isEmpty());
64         tmp.addRsp(a1, rsp1);
65         assertFalse(tmp.isEmpty());
66     }
67
68     public void testContainsKey() {
69         assertTrue(rl.containsKey(a1));
70         assertTrue(rl.containsKey(a3));
71     }
72
73     public void testContainsValue() {
74         assertTrue(rl.containsValue(rsp1));
75         assertTrue(rl.containsValue(rsp3));
76     }
77
78     public void testGet() {
79         Rsp rsp=(Rsp)rl.get(a1);
80         assertEquals(rsp, rsp1);
81         rsp=(Rsp)rl.get(a3);
82         assertEquals(rsp, rsp3);
83     }
84
85     public void testPut() {
86         Rsp rsp;
87         rsp=(Rsp)rl.put(new IpAddress(6666), new Rsp(new IpAddress(6666), true));
88         assertNull(rsp);
89         rsp=(Rsp)rl.put(a2, rsp2);
90         assertEquals(rsp, rsp2);
91         assertEquals(6, rl.size());
92     }
93
94     public void testRemove() {
95         Rsp rsp;
96         rsp=(Rsp)rl.remove(new IpAddress(6666));
97         assertNull(rsp);
98         rsp=(Rsp)rl.remove(a2);
99         assertEquals(rsp, rsp2);
100         assertEquals(4, rl.size());
101     }
102
103     public void testClear() {
104         rl.clear();
105         assertEquals(0, rl.size());
106     }
107
108     public void testKeySet() {
109         RspList tmp=new RspList();
110         Set keys=tmp.keySet();
111         assertNotNull(keys);
112         assertEquals(0, keys.size());
113     }
114
115     public void testKeySet2() {
116         Set keys=rl.keySet();
117         assertNotNull(keys);
118         assertEquals(rl.size(), keys.size());
119     }
120
121     public void testAddRsp() {
122         rl.addRsp(new IpAddress(6666), new Integer JavaDoc(322649));
123         assertEquals(6, rl.size());
124         Rsp rsp=(Rsp)rl.get(new IpAddress(6666));
125         assertNotNull(rsp);
126         assertTrue(rsp.wasReceived());
127         assertFalse(rsp.wasSuspected());
128         assertEquals(new Integer JavaDoc(322649), rsp.getValue());
129     }
130
131     public void testAddRsp2() {
132         rl.addRsp(a1, new Integer JavaDoc(322649));
133         assertEquals(5, rl.size());
134         Rsp rsp=(Rsp)rl.get(a1);
135         assertNotNull(rsp);
136         assertTrue(rsp.wasReceived());
137         assertFalse(rsp.wasSuspected());
138         assertEquals(new Integer JavaDoc(322649), rsp.getValue());
139     }
140
141     public void testNumSuspectedMembers() {
142         assertEquals(2, rl.numSuspectedMembers());
143     }
144
145     public void testGetFirst() {
146         Object JavaDoc obj=rl.getFirst();
147         System.out.println("-- first (non-null) value is " + obj);
148         assertNotNull(obj);
149     }
150
151     public void testGetResults() {
152         Vector v=rl.getResults();
153         assertNotNull(v);
154         assertEquals(2, v.size());
155     }
156
157     public void testElementAt() {
158         Rsp rsp;
159         Set s=new HashSet();
160         for(int i=0; i < rl.size(); i++) {
161             rsp=(Rsp)rl.elementAt(i);
162             s.add(rsp.getSender());
163         }
164         System.out.println("-- set is " + s);
165         assertEquals(rl.size(), s.size());
166     }
167
168
169     public void testElementAtWithOOBEx() {
170         try {
171             rl.elementAt(6);
172             fail("this should have thrown an ArrayIndexOutOfBoundsException");
173         }
174         catch(ArrayIndexOutOfBoundsException JavaDoc ex) {
175         }
176     }
177
178     public static Test suite() {
179         return new TestSuite(RspListTest.class);
180     }
181
182     public static void main(String JavaDoc[] args) {
183         junit.textui.TestRunner.run(RspListTest.suite());
184     }
185 }
186
Popular Tags