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