KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: MembershipTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.Address;
9 import org.jgroups.Membership;
10 import org.jgroups.stack.IpAddress;
11
12 import java.util.Vector JavaDoc;
13
14
15 public class MembershipTest extends TestCase {
16     Membership m1, m2;
17     Vector JavaDoc v1, v2;
18     Address a1, a2, a3, a4, a5;
19
20
21     public MembershipTest(String JavaDoc name) {
22         super(name);
23     }
24
25
26     public void setUp() {
27         a1=new IpAddress(5555);
28         a2=new IpAddress(6666);
29         a3=new IpAddress(6666);
30         a4=new IpAddress(7777);
31         a5=new IpAddress(8888);
32         m1=new Membership();
33     }
34
35     public void tearDown() {
36
37     }
38
39
40     public void testConstructor() {
41         v1=new Vector JavaDoc();
42         v1.addElement(a1);
43         v1.addElement(a2);
44         v1.addElement(a3);
45         m2=new Membership(v1);
46         assertTrue(m2.size() == 2);
47         assertTrue(m2.contains(a1));
48         assertTrue(m2.contains(a2));
49         assertTrue(m2.contains(a3));
50     }
51
52     public void testClone() {
53         v1=new Vector JavaDoc();
54         v1.addElement(a1);
55         v1.addElement(a2);
56         v1.addElement(a3);
57         m2=new Membership(v1);
58         m1=(Membership)m2.clone();
59         assertEquals(m1.size(), m2.size());
60         assertTrue(m1.contains(a1));
61         assertTrue(m1.contains(a2));
62         assertTrue(m2.contains(a1));
63         assertTrue(m2.contains(a2));
64     }
65
66
67     public void testCopy() {
68         v1=new Vector JavaDoc();
69         v1.addElement(a1);
70         v1.addElement(a2);
71         v1.addElement(a3);
72         m2=new Membership(v1);
73         m1=m2.copy();
74         assertEquals(m1.size(), m2.size());
75         assertTrue(m1.contains(a1));
76         assertTrue(m1.contains(a2));
77         assertTrue(m2.contains(a1));
78         assertTrue(m2.contains(a2));
79     }
80
81
82     public void testAdd() {
83         m1.add(a1);
84         m1.add(a2);
85         m1.add(a3);
86         assertTrue(m1.size() == 2); // a3 was *not* added because already present (a2)
87
assertTrue(m1.contains(a1));
88         assertTrue(m1.contains(a2));
89         assertTrue(m1.contains(a3)); // a3 is not present, but is equal to a2 !
90
}
91
92
93     public void testAddVector() {
94         v1=new Vector JavaDoc();
95         v1.addElement(a1);
96         v1.addElement(a2);
97         v1.addElement(a3);
98         m1.add(v1);
99         assertTrue(m1.size() == 2);
100         assertTrue(m1.contains(a1));
101         assertTrue(m1.contains(a2));
102     }
103
104     public void testAddVectorDupl() {
105         v1=new Vector JavaDoc();
106         v1.addElement(a1);
107         v1.addElement(a2);
108         v1.addElement(a3);
109         v1.addElement(a4);
110         v1.addElement(a5);
111
112         m1.add(a5);
113         m1.add(a1);
114         m1.add(v1);
115         assertTrue(m1.size() == 4);
116         assertTrue(m1.contains(a1));
117         assertTrue(m1.contains(a2));
118         assertTrue(m1.contains(a4));
119         assertTrue(m1.contains(a5));
120     }
121
122
123     public void testRemove() {
124         m1.add(a1);
125         m1.add(a2);
126         m1.add(a3);
127         m1.add(a4);
128         m1.add(a5);
129         m1.remove(a2);
130         assertTrue(m1.size() == 3);
131     }
132
133
134     public void testGetMembers() {
135         testAdd();
136         Vector JavaDoc v=m1.getMembers();
137         assertTrue(v.size() == 2);
138     }
139
140
141     public void testSet() {
142         v1=new Vector JavaDoc();
143         v1.addElement(a1);
144         v1.addElement(a2);
145         m1.add(a1);
146         m1.add(a2);
147         m1.add(a4);
148         m1.add(a5);
149         m1.set(v1);
150         assertTrue(m1.size() == 2);
151         assertTrue(m1.contains(a1));
152         assertTrue(m1.contains(a2));
153     }
154
155
156     public void testSet2() {
157         m1=new Membership();
158         m2=new Membership();
159         m1.add(a1);
160         m1.add(a2);
161         m1.add(a4);
162         m2.add(a5);
163         m2.set(m1);
164         assertTrue(m2.size() == 3);
165         assertTrue(m2.contains(a1));
166         assertTrue(m2.contains(a2));
167         assertTrue(m2.contains(a4));
168         assertTrue(!m2.contains(a5));
169     }
170
171
172     public void testMerge() {
173         v1=new Vector JavaDoc();
174         v2=new Vector JavaDoc();
175         m1.add(a1);
176         m1.add(a2);
177         m1.add(a3);
178         m1.add(a4);
179
180
181         v1.addElement(a5);
182         v2.addElement(a2);
183         v2.addElement(a3);
184
185         m1.merge(v1, v2);
186         assertTrue(m1.size() == 3);
187         assertTrue(m1.contains(a1));
188         assertTrue(m1.contains(a4));
189         assertTrue(m1.contains(a5));
190     }
191
192     public void testSort() {
193         m1.add(a3);
194         m1.add(a4);
195         m1.add(a2);
196         m1.add(a1);
197         m1.add(a5);
198         m1.add(a2); // dupl
199
System.out.println("membership: " + m1);
200         assertEquals(4, m1.size());
201         assertEquals(a3, m1.elementAt(0));
202         assertEquals(a4, m1.elementAt(1));
203         assertEquals(a1, m1.elementAt(2));
204         assertEquals(a5, m1.elementAt(3));
205         m1.sort();
206         System.out.println("sorted: " + m1);
207         assertEquals(4, m1.size());
208         assertEquals(a1, m1.elementAt(0));
209         assertEquals(a2, m1.elementAt(1));
210         assertEquals(a4, m1.elementAt(2));
211         assertEquals(a5, m1.elementAt(3));
212     }
213
214
215     public static Test suite() {
216         TestSuite s=new TestSuite(MembershipTest.class);
217         return s;
218     }
219
220     public static void main(String JavaDoc[] args) {
221         junit.textui.TestRunner.run(suite());
222     }
223 }
224
Popular Tags