001: // $Id: MembershipTest.java,v 1.4 2004/09/23 11:04:04 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.Address;
009: import org.jgroups.Membership;
010: import org.jgroups.stack.IpAddress;
011:
012: import java.util.Vector;
013:
014: public class MembershipTest extends TestCase {
015: Membership m1, m2;
016: Vector v1, v2;
017: Address a1, a2, a3, a4, a5;
018:
019: public MembershipTest(String name) {
020: super (name);
021: }
022:
023: public void setUp() {
024: a1 = new IpAddress(5555);
025: a2 = new IpAddress(6666);
026: a3 = new IpAddress(6666);
027: a4 = new IpAddress(7777);
028: a5 = new IpAddress(8888);
029: m1 = new Membership();
030: }
031:
032: public void tearDown() {
033:
034: }
035:
036: public void testConstructor() {
037: v1 = new Vector();
038: v1.addElement(a1);
039: v1.addElement(a2);
040: v1.addElement(a3);
041: m2 = new Membership(v1);
042: assertTrue(m2.size() == 2);
043: assertTrue(m2.contains(a1));
044: assertTrue(m2.contains(a2));
045: assertTrue(m2.contains(a3));
046: }
047:
048: public void testClone() {
049: v1 = new Vector();
050: v1.addElement(a1);
051: v1.addElement(a2);
052: v1.addElement(a3);
053: m2 = new Membership(v1);
054: m1 = (Membership) m2.clone();
055: assertEquals(m1.size(), m2.size());
056: assertTrue(m1.contains(a1));
057: assertTrue(m1.contains(a2));
058: assertTrue(m2.contains(a1));
059: assertTrue(m2.contains(a2));
060: }
061:
062: public void testCopy() {
063: v1 = new Vector();
064: v1.addElement(a1);
065: v1.addElement(a2);
066: v1.addElement(a3);
067: m2 = new Membership(v1);
068: m1 = m2.copy();
069: assertEquals(m1.size(), m2.size());
070: assertTrue(m1.contains(a1));
071: assertTrue(m1.contains(a2));
072: assertTrue(m2.contains(a1));
073: assertTrue(m2.contains(a2));
074: }
075:
076: public void testAdd() {
077: m1.add(a1);
078: m1.add(a2);
079: m1.add(a3);
080: assertTrue(m1.size() == 2); // a3 was *not* added because already present (a2)
081: assertTrue(m1.contains(a1));
082: assertTrue(m1.contains(a2));
083: assertTrue(m1.contains(a3)); // a3 is not present, but is equal to a2 !
084: }
085:
086: public void testAddVector() {
087: v1 = new Vector();
088: v1.addElement(a1);
089: v1.addElement(a2);
090: v1.addElement(a3);
091: m1.add(v1);
092: assertTrue(m1.size() == 2);
093: assertTrue(m1.contains(a1));
094: assertTrue(m1.contains(a2));
095: }
096:
097: public void testAddVectorDupl() {
098: v1 = new Vector();
099: v1.addElement(a1);
100: v1.addElement(a2);
101: v1.addElement(a3);
102: v1.addElement(a4);
103: v1.addElement(a5);
104:
105: m1.add(a5);
106: m1.add(a1);
107: m1.add(v1);
108: assertTrue(m1.size() == 4);
109: assertTrue(m1.contains(a1));
110: assertTrue(m1.contains(a2));
111: assertTrue(m1.contains(a4));
112: assertTrue(m1.contains(a5));
113: }
114:
115: public void testRemove() {
116: m1.add(a1);
117: m1.add(a2);
118: m1.add(a3);
119: m1.add(a4);
120: m1.add(a5);
121: m1.remove(a2);
122: assertTrue(m1.size() == 3);
123: }
124:
125: public void testGetMembers() {
126: testAdd();
127: Vector v = m1.getMembers();
128: assertTrue(v.size() == 2);
129: }
130:
131: public void testSet() {
132: v1 = new Vector();
133: v1.addElement(a1);
134: v1.addElement(a2);
135: m1.add(a1);
136: m1.add(a2);
137: m1.add(a4);
138: m1.add(a5);
139: m1.set(v1);
140: assertTrue(m1.size() == 2);
141: assertTrue(m1.contains(a1));
142: assertTrue(m1.contains(a2));
143: }
144:
145: public void testSet2() {
146: m1 = new Membership();
147: m2 = new Membership();
148: m1.add(a1);
149: m1.add(a2);
150: m1.add(a4);
151: m2.add(a5);
152: m2.set(m1);
153: assertTrue(m2.size() == 3);
154: assertTrue(m2.contains(a1));
155: assertTrue(m2.contains(a2));
156: assertTrue(m2.contains(a4));
157: assertTrue(!m2.contains(a5));
158: }
159:
160: public void testMerge() {
161: v1 = new Vector();
162: v2 = new Vector();
163: m1.add(a1);
164: m1.add(a2);
165: m1.add(a3);
166: m1.add(a4);
167:
168: v1.addElement(a5);
169: v2.addElement(a2);
170: v2.addElement(a3);
171:
172: m1.merge(v1, v2);
173: assertTrue(m1.size() == 3);
174: assertTrue(m1.contains(a1));
175: assertTrue(m1.contains(a4));
176: assertTrue(m1.contains(a5));
177: }
178:
179: public void testSort() {
180: m1.add(a3);
181: m1.add(a4);
182: m1.add(a2);
183: m1.add(a1);
184: m1.add(a5);
185: m1.add(a2); // dupl
186: System.out.println("membership: " + m1);
187: assertEquals(4, m1.size());
188: assertEquals(a3, m1.elementAt(0));
189: assertEquals(a4, m1.elementAt(1));
190: assertEquals(a1, m1.elementAt(2));
191: assertEquals(a5, m1.elementAt(3));
192: m1.sort();
193: System.out.println("sorted: " + m1);
194: assertEquals(4, m1.size());
195: assertEquals(a1, m1.elementAt(0));
196: assertEquals(a2, m1.elementAt(1));
197: assertEquals(a4, m1.elementAt(2));
198: assertEquals(a5, m1.elementAt(3));
199: }
200:
201: public static Test suite() {
202: TestSuite s = new TestSuite(MembershipTest.class);
203: return s;
204: }
205:
206: public static void main(String[] args) {
207: junit.textui.TestRunner.run(suite());
208: }
209: }
|