1 16 package org.apache.commons.collections.set; 17 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 37 public class TestListOrderedSet extends AbstractTestSet { 38 39 public TestListOrderedSet(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestListOrderedSet.class); 45 } 46 47 public static void main(String args[]) { 48 String [] testCaseName = { TestListOrderedSet.class.getName()}; 49 junit.textui.TestRunner.main(testCaseName); 50 } 51 52 public Set makeEmptySet() { 53 return ListOrderedSet.decorate(new HashSet ()); 54 } 55 56 protected Set setupSet() { 57 Set set = makeEmptySet(); 58 59 for (int i = 0; i < 10; i++) { 60 set.add(Integer.toString(i)); 61 } 62 return set; 63 } 64 65 public void testOrdering() { 66 Set set = setupSet(); 67 Iterator it = set.iterator(); 68 69 for (int i = 0; i < 10; i++) { 70 assertEquals("Sequence is wrong", Integer.toString(i), it.next()); 71 } 72 73 for (int i = 0; i < 10; i += 2) { 74 assertTrue("Must be able to remove int", set.remove(Integer.toString(i))); 75 } 76 77 it = set.iterator(); 78 for (int i = 1; i < 10; i += 2) { 79 assertEquals("Sequence is wrong after remove ", Integer.toString(i), it.next()); 80 } 81 82 for (int i = 0; i < 10; i++) { 83 set.add(Integer.toString(i)); 84 } 85 86 assertEquals("Size of set is wrong!", 10, set.size()); 87 88 it = set.iterator(); 89 for (int i = 1; i < 10; i += 2) { 90 assertEquals("Sequence is wrong", Integer.toString(i), it.next()); 91 } 92 for (int i = 0; i < 10; i += 2) { 93 assertEquals("Sequence is wrong", Integer.toString(i), it.next()); 94 } 95 } 96 97 private static final Integer ZERO = new Integer (0); 98 private static final Integer ONE = new Integer (1); 99 private static final Integer TWO = new Integer (2); 100 private static final Integer THREE = new Integer (3); 101 102 public void testListAddRemove() { 103 ListOrderedSet set = (ListOrderedSet) makeEmptySet(); 104 List view = set.asList(); 105 set.add(ZERO); 106 set.add(ONE); 107 set.add(TWO); 108 109 assertEquals(3, set.size()); 110 assertSame(ZERO, set.get(0)); 111 assertSame(ONE, set.get(1)); 112 assertSame(TWO, set.get(2)); 113 assertEquals(3, view.size()); 114 assertSame(ZERO, view.get(0)); 115 assertSame(ONE, view.get(1)); 116 assertSame(TWO, view.get(2)); 117 118 assertEquals(0, set.indexOf(ZERO)); 119 assertEquals(1, set.indexOf(ONE)); 120 assertEquals(2, set.indexOf(TWO)); 121 122 set.remove(1); 123 assertEquals(2, set.size()); 124 assertSame(ZERO, set.get(0)); 125 assertSame(TWO, set.get(1)); 126 assertEquals(2, view.size()); 127 assertSame(ZERO, view.get(0)); 128 assertSame(TWO, view.get(1)); 129 } 130 131 public void testListAddIndexed() { 132 ListOrderedSet set = (ListOrderedSet) makeEmptySet(); 133 List view = set.asList(); 134 set.add(ZERO); 135 set.add(TWO); 136 137 set.add(1, ONE); 138 assertEquals(3, set.size()); 139 assertSame(ZERO, set.get(0)); 140 assertSame(ONE, set.get(1)); 141 assertSame(TWO, set.get(2)); 142 143 set.add(0, ONE); 144 assertEquals(3, set.size()); 145 assertSame(ZERO, set.get(0)); 146 assertSame(ONE, set.get(1)); 147 assertSame(TWO, set.get(2)); 148 149 List list = new ArrayList (); 150 list.add(ZERO); 151 list.add(TWO); 152 153 set.addAll(0, list); 154 assertEquals(3, set.size()); 155 assertSame(ZERO, set.get(0)); 156 assertSame(ONE, set.get(1)); 157 assertSame(TWO, set.get(2)); 158 159 list.add(0, THREE); set.remove(TWO); set.addAll(1, list); 162 assertEquals(4, set.size()); 163 assertSame(ZERO, set.get(0)); 164 assertSame(THREE, set.get(1)); 165 assertSame(TWO, set.get(2)); 166 assertSame(ONE, set.get(3)); 167 } 168 169 public void testDecorator() { 170 try { 171 ListOrderedSet.decorate((List ) null); 172 fail(); 173 } catch (IllegalArgumentException ex) {} 174 try { 175 ListOrderedSet.decorate((Set ) null); 176 fail(); 177 } catch (IllegalArgumentException ex) {} 178 try { 179 ListOrderedSet.decorate(null, null); 180 fail(); 181 } catch (IllegalArgumentException ex) {} 182 try { 183 ListOrderedSet.decorate(new HashSet (), null); 184 fail(); 185 } catch (IllegalArgumentException ex) {} 186 try { 187 ListOrderedSet.decorate(null, new ArrayList ()); 188 fail(); 189 } catch (IllegalArgumentException ex) {} 190 } 191 192 public String getCompatibilityVersion() { 193 return "3.1"; 194 } 195 196 203 } 204 | Popular Tags |