1 32 package net.sf.retrotranslator.runtime.java.util; 33 34 import java.util.*; 35 import junit.framework.TestCase; 36 37 40 public class CollectionsTestCase extends TestCase { 41 42 public void testCheckedCollection() throws Exception { 43 Collection strings = Collections.checkedCollection(new ArrayList(), String .class); 44 strings.add("abc"); 45 try { 46 strings.add(Boolean.TRUE); 47 fail(); 48 } catch (ClassCastException e) { 49 } 51 } 52 53 public void testCheckedSet() throws Exception { 54 Set strings = Collections.checkedSet(new HashSet(), String .class); 55 strings.add("abc"); 56 try { 57 strings.add(Boolean.TRUE); 58 fail(); 59 } catch (ClassCastException e) { 60 } 62 } 63 64 public void testCheckedSortedSet() throws Exception { 65 SortedSet strings = Collections.checkedSortedSet(new TreeSet(), String .class); 66 strings.add("abc"); 67 try { 68 strings.add(Boolean.TRUE); 69 fail(); 70 } catch (ClassCastException e) { 71 } 73 } 74 75 public void testCheckedList() throws Exception { 76 List strings = Collections.checkedList(new ArrayList(), String .class); 77 strings.add("abc"); 78 try { 79 strings.add(Boolean.TRUE); 80 fail(); 81 } catch (ClassCastException e) { 82 } 84 } 85 86 public void testCheckedMap() throws Exception { 87 Map strings = Collections.checkedMap(new HashMap(), String .class, Integer .class); 88 strings.put("abc", Integer.MIN_VALUE); 89 try { 90 strings.put(Boolean.TRUE, Integer.MIN_VALUE); 91 fail(); 92 } catch (ClassCastException e) { 93 } 95 try { 96 strings.put("abc", Boolean.TRUE); 97 fail(); 98 } catch (ClassCastException e) { 99 } 101 Set set = strings.entrySet(); 102 Map.Entry entry1 = (Map.Entry) set.iterator().next(); 103 entry1.setValue(Integer.MAX_VALUE); 104 try { 105 entry1.setValue("fail"); 106 fail(); 107 } catch (ClassCastException e) { 108 } 110 Map.Entry entry2 = (Map.Entry) set.toArray()[0]; 111 entry2.setValue(Integer.MAX_VALUE); 112 try { 113 entry2.setValue("fail"); 114 fail(); 115 } catch (ClassCastException e) { 116 } 118 } 119 120 public void testCheckedSortedMap() throws Exception { 121 Map strings = Collections.checkedSortedMap(new TreeMap(), String .class, Integer .class); 122 strings.put("abc", Integer.MIN_VALUE); 123 try { 124 strings.put(Boolean.TRUE, Integer.MIN_VALUE); 125 fail(); 126 } catch (ClassCastException e) { 127 } 129 try { 130 strings.put("abc", Boolean.TRUE); 131 fail(); 132 } catch (ClassCastException e) { 133 } 135 Set set = strings.entrySet(); 136 Map.Entry entry1 = (Map.Entry) set.iterator().next(); 137 entry1.setValue(Integer.MAX_VALUE); 138 try { 139 entry1.setValue("fail"); 140 fail(); 141 } catch (ClassCastException e) { 142 } 144 Map.Entry entry2 = (Map.Entry) set.toArray()[0]; 145 entry2.setValue(Integer.MAX_VALUE); 146 try { 147 entry2.setValue("fail"); 148 fail(); 149 } catch (ClassCastException e) { 150 } 152 } 153 154 public void testEmptySet() throws Exception { 155 assertSame(Collections.EMPTY_SET, Collections.emptySet()); 156 } 157 158 public void testEmptyList() throws Exception { 159 assertSame(Collections.EMPTY_LIST, Collections.emptyList()); 160 } 161 162 public void testEmptyMap() throws Exception { 163 assertSame(Collections.EMPTY_MAP, Collections.emptyMap()); 164 } 165 166 public void testReverseOrder() throws Exception { 167 String [] strings = {"a", "b"}; 168 Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER); 169 assertEquals("a", strings[0]); 170 assertEquals("b", strings[1]); 171 Arrays.sort(strings, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER)); 172 assertEquals("b", strings[0]); 173 assertEquals("a", strings[1]); 174 } 175 176 public void testFrequency() throws Exception { 177 assertEquals(2, Collections.frequency(Arrays.asList("a", "b", "c", "b", "a"), "b")); 178 } 179 180 public void testDisjoint() throws Exception { 181 assertTrue(Collections.disjoint(Arrays.asList("a", "b", "c"), Arrays.asList("x", "y", "z"))); 182 assertFalse(Collections.disjoint(Arrays.asList("a", "b", "c"), Arrays.asList("x", "c", "z"))); 183 } 184 185 public void testAddAll() throws Exception { 186 List<String > list = new ArrayList<String >(Arrays.asList("a", "b", "c")); 187 Collections.addAll(list, "x", "y"); 188 assertEquals(5, list.size()); 189 assertEquals("x", list.get(3)); 190 } 191 192 public void testExisting_singletonList() throws Exception { 193 List<String > list = Collections.singletonList("a"); 194 assertEquals(1, list.size()); 195 assertEquals("a", list.get(0)); 196 } 197 198 public void testExisting_synchronizedMap() throws Exception { 199 Map map = new HashMap(); 200 Map syncMap = Collections.synchronizedMap(map); 201 syncMap.put("a", "b"); 202 assertEquals(1, map.size()); 203 assertEquals("b", map.get("a")); 204 } 205 206 } 207 | Popular Tags |