1 4 package com.tc.object.tx; 5 6 import java.lang.reflect.Method ; 7 import java.util.Iterator ; 8 import java.util.LinkedList ; 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 19 void callback() { 20 } 22 23 void callback(Object arg) { 24 } 26 27 void callback(Iterator iter, Object arg) { 28 } 30 31 } 32 33 private void callback(Iterator changes) { 35 fail("shouldn't be called during test"); 36 } 37 38 void DONT_CALL_ME() { 39 callback(null); 41 } 42 43 public void testAddRemove() throws Exception { 44 try { 45 collection.add(null); 46 fail("allowed to add null"); 47 } catch (NullPointerException npe) { 48 } 50 51 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 callback = collection.getCallbackFor(this); 59 assertNotNull(callback); 60 assertEquals(getClass().getDeclaredMethod("callback", new Class [] { Iterator .class }), callback); 61 62 collection.remove(this); 64 assertEquals(0, collection.size()); 65 assertNull(collection.getCallbackFor(this)); 66 67 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 } 75 assertEquals(0, collection.size()); 76 } 77 78 public void testAddAll() { 79 try { 80 collection.addAll(new LinkedList ()); 81 fail("someone implemented this method but didn't update/implement the test"); 82 } catch (UnsupportedOperationException uoe) { 83 } 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 ()); 113 fail("someone implemented this method but didn't update/implement the test"); 114 } catch (UnsupportedOperationException uoe) { 115 } 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 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 uoe) { 143 } 145 } 146 147 public void testRemoveAll() { 148 try { 149 collection.removeAll(new LinkedList ()); 150 fail("someone implemented this method but didn't update/implement the test"); 151 } catch (UnsupportedOperationException uoe) { 152 } 154 } 155 156 public void testRetainAll() { 157 try { 158 collection.retainAll(new LinkedList ()); 159 fail("someone implemented this method but didn't update/implement the test"); 160 } catch (UnsupportedOperationException uoe) { 161 } 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 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 |