1 16 package org.apache.myfaces.context.servlet; 17 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.Map.Entry; 25 26 import org.apache.myfaces.MyFacesBaseTest; 27 28 49 public class ApplicationMapTest extends MyFacesBaseTest 50 { 51 private Object _test0 = new Object (); 52 private Object _test1 = new Object (); 53 54 public ApplicationMapTest(String name) 55 { 56 super(name); 57 } 58 59 private void init() 60 { 61 Map appMap = _facesContext.getExternalContext().getApplicationMap(); 62 63 appMap.clear(); 64 65 assertTrue(appMap.isEmpty()); 66 assertEquals(0, appMap.size()); 67 68 assertNull(appMap.put("test1", _test1)); 69 70 assertFalse(appMap.isEmpty()); 71 assertEquals(1, appMap.size()); 72 73 Object test2 = new Object (); 74 assertNull(appMap.put("test2", test2)); 75 76 Object test3 = new Object (); 77 assertNull(appMap.put("test3", test3)); 78 79 Object test4 = new Object (); 80 assertNull(appMap.put("test4", test4)); 81 82 assertEquals(4, appMap.size()); 83 84 assertSame(_test1, appMap.put("test1", test2)); 85 assertSame(test2, appMap.put("test1", _test1)); 86 87 assertEquals(4, appMap.size()); 88 89 assertSame(test4, appMap.remove("test4")); 90 assertEquals(3, appMap.size()); 91 assertNull(appMap.put("test4", test4)); 92 assertEquals(4, appMap.size()); 93 } 94 95 public void testClear() 96 { 97 init(); 99 Map appMap = _facesContext.getExternalContext().getApplicationMap(); 100 101 assertFalse(appMap.isEmpty()); 102 103 appMap.clear(); 104 105 assertTrue(appMap.isEmpty()); 106 assertEquals(0, appMap.size()); 107 108 init(); 110 Set entrySet = appMap.entrySet(); 111 112 assertFalse(entrySet.isEmpty()); 113 114 entrySet.clear(); 115 116 assertTrue(appMap.isEmpty()); 117 assertEquals(0, appMap.size()); 118 assertTrue(entrySet.isEmpty()); 119 assertEquals(0, entrySet.size()); 120 121 init(); 123 Set keySet = appMap.keySet(); 124 125 assertFalse(keySet.isEmpty()); 126 127 keySet.clear(); 128 129 assertTrue(appMap.isEmpty()); 130 assertEquals(0, appMap.size()); 131 assertTrue(keySet.isEmpty()); 132 assertEquals(0, keySet.size()); 133 134 init(); 136 Collection values = appMap.values(); 137 138 assertFalse(values.isEmpty()); 139 140 values.clear(); 141 142 assertTrue(appMap.isEmpty()); 143 assertEquals(0, appMap.size()); 144 assertTrue(values.isEmpty()); 145 assertEquals(0, values.size()); 146 } 147 148 public void testEntrySet() 149 { 150 init(); 151 Map appMap = _facesContext.getExternalContext().getApplicationMap(); 152 Map cache = new HashMap (); 153 154 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 155 { 156 Entry entry = (Entry) it.next(); 157 assertNull(cache.put(entry.getKey(), entry.getValue())); 158 } 159 160 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 161 { 162 Entry entry = (Entry) it.next(); 163 assertSame(entry.getValue(), cache.put(entry.getKey(), entry.getValue())); 164 } 165 166 assertSame(_test1, cache.get("test1")); 167 168 int size = appMap.size(); 169 170 assertNull(appMap.put("test0", _test0)); 171 assertEquals(size + 1, appMap.size()); 172 173 findTest0: { 174 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 175 { 176 Entry entry = (Entry) it.next(); 177 if (entry.getKey().equals("test0")) 178 { 179 assertTrue(appMap.entrySet().contains(entry)); 180 assertTrue(appMap.entrySet().remove(entry)); 181 assertEquals(size, appMap.size()); 182 assertNull(appMap.remove(_test0)); 183 assertFalse(appMap.entrySet().remove(new Object ())); 184 assertEquals(size, appMap.size()); 185 break findTest0; 186 } 187 } 188 throw new IllegalStateException ("Test0 not found"); 189 } 190 191 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 192 { 193 it.next(); 195 it.remove(); 196 break; 197 } 198 199 assertEquals(size - 1, appMap.size()); 200 assertEquals(size - 1, appMap.entrySet().size()); 201 } 202 203 public void testKeySet() 204 { 205 init(); 206 Map appMap = _facesContext.getExternalContext().getApplicationMap(); 207 Map cache = new HashMap (); 208 209 for (Iterator it = appMap.keySet().iterator(); it.hasNext();) 210 { 211 String key = (String ) it.next(); 212 assertNull(cache.put(key, appMap.get(key))); 213 } 214 215 for (Iterator it = appMap.keySet().iterator(); it.hasNext();) 216 { 217 String key = (String ) it.next(); 218 assertSame(appMap.get(key), cache.put(key, appMap.get(key))); 219 } 220 221 assertSame(_test1, cache.get("test1")); 222 223 int size = appMap.size(); 224 225 assertNull(appMap.put("test0", _test0)); 226 assertEquals(size + 1, appMap.size()); 227 228 findTest0: { 229 for (Iterator it = appMap.keySet().iterator(); it.hasNext();) 230 { 231 String key = (String ) it.next(); 232 if (key.equals("test0")) 233 { 234 assertTrue(appMap.keySet().contains(key)); 235 assertTrue(appMap.keySet().remove(key)); 236 assertEquals(size, appMap.size()); 237 assertNull(appMap.remove(_test0)); 238 assertFalse(appMap.keySet().remove(new Object ())); 239 assertEquals(size, appMap.size()); 240 break findTest0; 241 } 242 } 243 throw new IllegalStateException ("Test0 not found"); 244 } 245 246 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 247 { 248 it.next(); 250 it.remove(); 251 break; 252 } 253 254 assertEquals(size - 1, appMap.size()); 255 assertEquals(size - 1, appMap.keySet().size()); 256 } 257 258 public void testValues() 259 { 260 init(); 261 Map appMap = _facesContext.getExternalContext().getApplicationMap(); 262 Set cache = new HashSet (); 263 264 for (Iterator it = appMap.values().iterator(); it.hasNext();) 265 { 266 assertTrue(cache.add(it.next())); 267 } 268 269 for (Iterator it = appMap.values().iterator(); it.hasNext();) 270 { 271 assertFalse(cache.add(it.next())); 272 } 273 274 assertTrue(cache.contains(_test1)); 275 276 int size = appMap.size(); 277 278 assertNull(appMap.put("test0", _test0)); 279 assertEquals(size + 1, appMap.size()); 280 281 findTest0: { 282 for (Iterator it = appMap.values().iterator(); it.hasNext();) 283 { 284 Object value = it.next(); 285 if (value.equals(_test0)) 286 { 287 assertTrue(appMap.values().contains(value)); 288 assertTrue(appMap.values().remove(value)); 289 assertEquals(size, appMap.size()); 290 assertNull(appMap.remove(_test0)); 291 assertFalse(appMap.keySet().remove(new Object ())); 292 assertEquals(size, appMap.size()); 293 break findTest0; 294 } 295 } 296 throw new IllegalStateException ("Test0 not found"); 297 } 298 299 for (Iterator it = appMap.entrySet().iterator(); it.hasNext();) 300 { 301 it.next(); 303 it.remove(); 304 break; 305 } 306 307 assertEquals(size - 1, appMap.size()); 308 assertEquals(size - 1, appMap.values().size()); 309 } 310 } 311 | Popular Tags |