KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > ChangeListenerCollectionTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tx;
5
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.LinkedList JavaDoc;
9
10 import junit.framework.TestCase;
11
12 public class ChangeListenerCollectionTest extends TestCase {
13
14   ChangeListenerCollection collection = new ChangeListenerCollection("callback");
15
16   private static class Bad {
17     // NOTE: this class does NOT have the proper callback method (ie. callback(Itertor) )
18

19     void callback() {
20       //
21
}
22
23     void callback(Object JavaDoc arg) {
24       //
25
}
26
27     void callback(Iterator JavaDoc iter, Object JavaDoc arg) {
28       //
29
}
30
31   }
32
33   // This method private on purpose. The callback method is allowed to have any access flags
34
private void callback(Iterator JavaDoc changes) {
35     fail("shouldn't be called during test");
36   }
37
38   void DONT_CALL_ME() {
39     // this method here simply to silence compiler warning about callback() never being called
40
callback(null);
41   }
42
43   public void testAddRemove() throws Exception JavaDoc {
44     try {
45       collection.add(null);
46       fail("allowed to add null");
47     } catch (NullPointerException JavaDoc npe) {
48       // expected
49
}
50
51     // test add with instance that has the callback method
52
assertFalse(collection.contains(this));
53     assertEquals(0, collection.size());
54     assertNull(collection.getCallbackFor(this));
55     collection.add(this);
56     assertTrue(collection.contains(this));
57     assertEquals(1, collection.size());
58     Method JavaDoc callback = collection.getCallbackFor(this);
59     assertNotNull(callback);
60     assertEquals(getClass().getDeclaredMethod("callback", new Class JavaDoc[] { Iterator JavaDoc.class }), callback);
61
62     // test remove
63
collection.remove(this);
64     assertEquals(0, collection.size());
65     assertNull(collection.getCallbackFor(this));
66
67     // test adding instance w/o the callback method
68
assertEquals(0, collection.size());
69     try {
70       collection.add(new Bad());
71       fail("allowed to add an object w/o the callback");
72     } catch (ChangeListenerCollection.NoSuchCallbackException nsce) {
73       // expected
74
}
75     assertEquals(0, collection.size());
76   }
77
78   public void testAddAll() {
79     try {
80       collection.addAll(new LinkedList JavaDoc());
81       fail("someone implemented this method but didn't update/implement the test");
82     } catch (UnsupportedOperationException JavaDoc uoe) {
83       // expected
84
}
85   }
86
87   public void testClear() {
88     collection.add(this);
89     ChangeListenerCollectionTest that = new ChangeListenerCollectionTest();
90     collection.add(that);
91     assertEquals(2, collection.size());
92     assertNotNull(collection.getCallbackFor(this));
93     assertNotNull(collection.getCallbackFor(that));
94
95     collection.clear();
96
97     assertEquals(0, collection.size());
98     assertNull(collection.getCallbackFor(this));
99     assertNull(collection.getCallbackFor(that));
100   }
101
102   public void testContains() {
103     assertFalse(collection.contains(this));
104     collection.add(this);
105     assertTrue(collection.contains(this));
106     collection.remove(this);
107     assertFalse(collection.contains(this));
108   }
109
110   public void testContainsAll() {
111     try {
112       collection.containsAll(new LinkedList JavaDoc());
113       fail("someone implemented this method but didn't update/implement the test");
114     } catch (UnsupportedOperationException JavaDoc uoe) {
115       // expected
116
}
117   }
118
119   public void testIsEmpty() {
120     assertTrue(collection.isEmpty());
121     collection.add(this);
122     assertFalse(collection.isEmpty());
123   }
124
125   public void testIterator() {
126     Iterator JavaDoc iter = collection.iterator();
127     assertFalse(iter.hasNext());
128
129     collection.add(this);
130     iter = collection.iterator();
131     assertTrue(iter.hasNext());
132     assertSame(this, iter.next());
133     assertFalse(iter.hasNext());
134
135
136     iter = collection.iterator();
137     iter.next();
138     try {
139       iter.remove();
140       fail("iterator supports remove()");
141     }
142     catch (UnsupportedOperationException JavaDoc uoe) {
143       // expected
144
}
145   }
146
147   public void testRemoveAll() {
148     try {
149       collection.removeAll(new LinkedList JavaDoc());
150       fail("someone implemented this method but didn't update/implement the test");
151     } catch (UnsupportedOperationException JavaDoc uoe) {
152       // expected
153
}
154   }
155
156   public void testRetainAll() {
157     try {
158       collection.retainAll(new LinkedList JavaDoc());
159       fail("someone implemented this method but didn't update/implement the test");
160     } catch (UnsupportedOperationException JavaDoc uoe) {
161       // expected
162
}
163   }
164
165   public void testSize() {
166     assertEquals(0, collection.size());
167     collection.add(this);
168     assertEquals(1, collection.size());
169     collection.remove(this);
170     assertEquals(0, collection.size());
171   }
172
173   public void testToArray() {
174     assertEquals(0, collection.toArray().length);
175     collection.add(this);
176     Object JavaDoc objs[] = collection.toArray();
177     assertEquals(1, objs.length);
178     assertSame(this, objs[0]);
179   }
180
181   public void testToString() {
182     collection.toString();
183     collection.add(this);
184     collection.toString();
185   }
186
187 }
Popular Tags