1 19 package org.fjank.jcache.collection; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import javax.util.jcache.CacheAccessFactory; 27 import junit.framework.TestCase; 28 29 33 public class SetProxyTest extends TestCase { 34 35 private Map map; 36 37 41 public SetProxyTest(String arg0) { 42 super(arg0); 43 } 44 45 public static void main(String [] args) { 46 junit.textui.TestRunner.run(SetProxyTest.class); 47 } 48 49 52 protected void setUp() throws Exception { 53 this.map = CacheAccessFactory.getInstance().getMapAccess(); 54 } 55 56 final public void testAdd() { 57 try { 58 map.keySet().add(new Object ()); 59 fail("Should throw UnsupportedOperationException"); 60 }catch (UnsupportedOperationException e) { 61 } 63 } 64 65 66 final public void testAddAll() { 67 try { 68 map.keySet().addAll(new ArrayList ()); 69 fail("Should throw UnsupportedOperationException"); 70 }catch (UnsupportedOperationException e) { 71 } 73 } 74 75 final public void testClear() { 76 map.clear(); 77 map.put("1", "1"); 78 map.put("2", "1"); 79 map.put("3", "1"); 80 Set set = map.keySet(); 81 set.clear(); 82 assertEquals(true, map.isEmpty()); 83 } 84 85 final public void testContains() { 86 map.clear(); 87 map.put("jhgfd", "mnbv"); 88 assertEquals(true, map.keySet().contains("jhgfd")); 89 map.clear(); 90 assertEquals(false, map.keySet().contains("jhgfd")); 91 92 } 93 94 final public void testContainsAll() { 95 map.clear(); 96 map.put("1", "1"); 97 map.put("2", "1"); 98 map.put("3", "1"); 99 List coll = new ArrayList (); 100 coll.add("1"); 101 coll.add("2"); 102 assertEquals(true, map.keySet().containsAll(coll)); 103 coll.add("3"); 104 assertEquals(true, map.keySet().containsAll(coll)); 105 coll.add("4"); 106 assertEquals(false, map.keySet().containsAll(coll)); 107 108 } 109 110 final public void testIsEmpty() { 111 map.clear(); 112 assertEquals(true, map.keySet().isEmpty()); 113 map.put("1", "2"); 114 assertEquals(false, map.keySet().isEmpty()); 115 116 } 117 118 final public void testIterator() { 119 map.clear(); 120 map.put("1", "2"); 121 map.put("2", "2"); 122 Iterator iter = map.keySet().iterator(); 123 while(iter.hasNext()) { 124 Object obj = iter.next(); 125 if(obj.equals("1")) iter.remove(); 126 } 127 assertEquals(true, map.containsKey("2")); 128 assertEquals(false, map.containsKey("1")); 129 } 130 131 final public void testRemove() { 132 map.clear(); 133 map.put("1", "2"); 134 map.put("2", "2"); 135 map.put("3", "2"); 136 map.keySet().remove("2"); 137 assertEquals(false, map.containsKey("2")); 138 } 139 140 final public void testRemoveAll() { 141 map.clear(); 142 map.put("1", "2"); 143 map.put("2", "2"); 144 map.put("3", "2"); 145 List list = new ArrayList (); 146 list.add("1"); 147 list.add("2"); 148 map.keySet().removeAll(list); 149 assertEquals(false, map.containsKey("1")); 150 assertEquals(false, map.containsKey("2")); 151 assertEquals(true, map.containsKey("3")); 152 } 153 154 final public void testRetainAll() { 155 map.clear(); 156 map.put("1", "2"); 157 map.put("2", "2"); 158 map.put("3", "2"); 159 List list = new ArrayList (); 160 list.add("1"); 161 list.add("2"); 162 map.keySet().retainAll(list); 163 assertEquals(true, map.containsKey("1")); 164 assertEquals(true, map.containsKey("2")); 165 assertEquals(false, map.containsKey("3")); 166 } 167 168 final public void testSize() { 169 map.clear(); 170 assertEquals(0, map.keySet().size()); 171 map.put("1","1"); 172 map.put("2","1"); 173 map.put("3","1"); 174 assertEquals(3, map.keySet().size()); 175 map.clear(); 176 assertEquals(0, map.keySet().size()); 177 } 178 179 182 final public void testToArray() { 183 map.clear(); 184 String onoe = "1"; 185 map.put(onoe,onoe); 186 String two = "2"; 187 map.put(two,onoe); 188 String three = "3"; 189 map.put(three,onoe); 190 Object [] objArr = map.keySet().toArray(); 191 List test = new ArrayList (); 192 test.add(onoe); 193 test.add(two); 194 test.add(three); 195 for (int i = 0; i < objArr.length; i++) { 196 Object object = objArr[i]; 197 assertEquals(true, test.contains(object)); 198 test.remove(object); 199 } 200 assertEquals(true, test.isEmpty()); 201 } 202 203 206 final public void testToArrayObjectArray() { 207 String [] res = new String [4]; 208 String one = "1"; 209 map.put(one,one); 210 String two = "2"; 211 map.put(two,one); 212 String three = "3"; 213 map.put(three,one); 214 String four = "4"; 215 map.put(four, one); 216 Object [] objArr = map.keySet().toArray(res); 217 List test = new ArrayList (); 218 test.add(one); 219 test.add(two); 220 test.add(three); 221 test.add(four); 222 for (int i = 0; i < objArr.length; i++) { 223 Object object = objArr[i]; 224 assertEquals(true, test.contains(object)); 225 test.remove(object); 226 } 227 assertEquals(true, test.isEmpty()); 228 assertSame(objArr, res); 229 230 } 231 232 } 233 | Popular Tags |