1 16 package org.apache.commons.collections.list; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.HashSet ; 21 import java.util.List ; 22 import java.util.ListIterator ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.textui.TestRunner; 27 28 36 public class TestSetUniqueList extends AbstractTestList { 37 38 public static void main(String [] args) { 39 TestRunner.run(suite()); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestSetUniqueList.class); 44 } 45 46 public TestSetUniqueList(String testName) { 47 super(testName); 48 } 49 50 public List makeEmptyList() { 52 return new SetUniqueList(new ArrayList (), new HashSet ()); 53 } 54 55 public void testListIteratorSet() { 56 resetFull(); 58 ListIterator it = getList().listIterator(); 59 it.next(); 60 try { 61 it.set(null); 62 fail(); 63 } catch (UnsupportedOperationException ex) {} 64 } 65 66 public Object [] getFullNonNullElements() { 67 return new Object [] { 69 new String (""), 70 new String ("One"), 71 new Integer (2), 72 "Three", 73 new Integer (4), 74 new Double (5), 75 new Float (6), 76 "Seven", 77 "Eight", 78 new String ("Nine"), 79 new Integer (10), 80 new Short ((short)11), 81 new Long (12), 82 "Thirteen", 83 "14", 84 "15", 85 new Byte ((byte)16) 86 }; 87 } 88 89 public void testListIteratorAdd() { 90 resetEmpty(); 92 List list1 = getList(); 93 List list2 = getConfirmedList(); 94 95 Object [] elements = getOtherElements(); ListIterator iter1 = list1.listIterator(); 97 ListIterator iter2 = list2.listIterator(); 98 99 for (int i = 0; i < elements.length; i++) { 100 iter1.add(elements[i]); 101 iter2.add(elements[i]); 102 super.verify(); } 104 105 resetFull(); 106 iter1 = getList().listIterator(); 107 iter2 = getConfirmedList().listIterator(); 108 for (int i = 0; i < elements.length; i++) { 109 iter1.next(); 110 iter2.next(); 111 iter1.add(elements[i]); 112 iter2.add(elements[i]); 113 super.verify(); } 115 } 116 117 public void testCollectionAddAll() { 118 resetEmpty(); 120 Object [] elements = getFullElements(); 121 boolean r = collection.addAll(Arrays.asList(elements)); 122 confirmed.addAll(Arrays.asList(elements)); 123 verify(); 124 assertTrue("Empty collection should change after addAll", r); 125 for (int i = 0; i < elements.length; i++) { 126 assertTrue("Collection should contain added element", 127 collection.contains(elements[i])); 128 } 129 130 resetFull(); 131 int size = collection.size(); 132 elements = getOtherElements(); 133 r = collection.addAll(Arrays.asList(elements)); 134 confirmed.addAll(Arrays.asList(elements)); 135 verify(); 136 assertTrue("Full collection should change after addAll", r); 137 for (int i = 0; i < elements.length; i++) { 138 assertTrue("Full collection should contain added element " + i, 139 collection.contains(elements[i])); 140 } 141 assertEquals("Size should increase after addAll", 142 size + elements.length, collection.size()); 143 } 144 145 public void testListSetByIndex() { 146 resetFull(); 148 int size = collection.size(); 149 getList().set(0, new Long (1000)); 150 assertEquals(size, collection.size()); 151 152 getList().set(2, new Long (1000)); 153 assertEquals(size - 1, collection.size()); 154 assertEquals(new Long (1000), getList().get(1)); } 156 157 boolean extraVerify = true; 158 public void testCollectionIteratorRemove() { 159 try { 160 extraVerify = false; 161 super.testCollectionIteratorRemove(); 162 } finally { 163 extraVerify = true; 164 } 165 } 166 167 public void verify() { 168 super.verify(); 169 170 if (extraVerify) { 171 int size = collection.size(); 172 getList().add(new Long (1000)); 173 assertEquals(size + 1, collection.size()); 174 175 getList().add(new Long (1000)); 176 assertEquals(size + 1, collection.size()); 177 assertEquals(new Long (1000), getList().get(size)); 178 179 getList().remove(size); 180 } 181 } 182 183 public void testFactory() { 185 Integer [] array = new Integer [] {new Integer (1), new Integer (2), new Integer (1)}; 186 ArrayList list = new ArrayList (Arrays.asList(array)); 187 final SetUniqueList lset = SetUniqueList.decorate(list); 188 189 assertEquals("Duplicate element was added.", 2, lset.size()); 190 assertEquals(new Integer (1), lset.get(0)); 191 assertEquals(new Integer (2), lset.get(1)); 192 assertEquals(new Integer (1), list.get(0)); 193 assertEquals(new Integer (2), list.get(1)); 194 } 195 196 public void testAdd() { 197 final SetUniqueList lset = new SetUniqueList(new ArrayList (), new HashSet ()); 198 199 final Object obj = new Integer (1); 201 lset.add(obj); 202 lset.add(obj); 203 assertEquals("Duplicate element was added.", 1, lset.size()); 204 205 lset.add(new Integer (2)); 207 assertEquals("Unique element was not added.", 2, lset.size()); 208 } 209 210 public void testAddAll() { 211 final SetUniqueList lset = new SetUniqueList(new ArrayList (), new HashSet ()); 212 213 lset.addAll( 214 Arrays.asList(new Integer [] { new Integer (1), new Integer (1)})); 215 216 assertEquals("Duplicate element was added.", 1, lset.size()); 217 } 218 219 public void testSet() { 220 final SetUniqueList lset = new SetUniqueList(new ArrayList (), new HashSet ()); 221 222 final Object obj1 = new Integer (1); 224 final Object obj2 = new Integer (2); 225 final Object obj3 = new Integer (3); 226 227 lset.add(obj1); 228 lset.add(obj2); 229 lset.set(0, obj1); 230 assertEquals(2, lset.size()); 231 assertSame(obj1, lset.get(0)); 232 assertSame(obj2, lset.get(1)); 233 234 lset.clear(); 235 lset.add(obj1); 236 lset.add(obj2); 237 lset.set(0, obj2); 238 assertEquals(1, lset.size()); 239 assertSame(obj2, lset.get(0)); 240 241 lset.clear(); 242 lset.add(obj1); 243 lset.add(obj2); 244 lset.set(0, obj3); 245 assertEquals(2, lset.size()); 246 assertSame(obj3, lset.get(0)); 247 assertSame(obj2, lset.get(1)); 248 249 lset.clear(); 250 lset.add(obj1); 251 lset.add(obj2); 252 lset.set(1, obj1); 253 assertEquals(1, lset.size()); 254 assertSame(obj1, lset.get(0)); 255 } 256 257 public void testListIterator() { 258 final SetUniqueList lset = new SetUniqueList(new ArrayList (), new HashSet ()); 259 260 final Object obj1 = new Integer (1); 261 final Object obj2 = new Integer (2); 262 lset.add(obj1); 263 lset.add(obj2); 264 265 for (final ListIterator it = lset.listIterator(); it.hasNext();) { 267 it.next(); 268 269 if (!it.hasNext()) { 270 it.add(obj1); 271 break; 272 } 273 } 274 275 assertEquals("Duplicate element was added", 2, lset.size()); 276 } 277 278 public String getCompatibilityVersion() { 279 return "3.1"; 280 } 281 282 289 } 290 | Popular Tags |