1 16 package org.apache.myfaces.context.servlet; 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.Map.Entry; 24 25 import javax.servlet.http.Cookie ; 26 27 import org.apache.myfaces.MyFacesBaseTest; 28 29 30 public class CookieMapTest extends MyFacesBaseTest 31 { 32 public CookieMapTest(String name) 33 { 34 super(name); 35 } 36 37 protected Cookie [] getCookies() 38 { 39 Cookie [] cookies = new Cookie [5]; 40 41 Cookie cookie = new Cookie ("cookie0", "cookie0.value"); 42 cookies[0] = cookie; 43 44 cookie = new Cookie ("cookie1", "cookie1.value"); 45 cookies[1] = cookie; 46 47 cookie = new Cookie ("cookie2", "cookie2.value"); 48 cookies[2] = cookie; 49 50 cookie = new Cookie ("cookie3", "cookie3.value"); 51 cookies[3] = cookie; 52 53 cookie = new Cookie ("cookie4", "cookie4.value"); 54 cookies[4] = cookie; 55 56 return cookies; 57 } 58 59 public void testCookies() 60 { 61 Map appMap = _facesContext.getExternalContext().getRequestCookieMap(); 62 63 assertFalse(appMap.isEmpty()); 64 assertEquals(5, appMap.size()); 65 } 66 67 public void testEntrySet() 68 { 69 Map cookieMap = _facesContext.getExternalContext().getRequestCookieMap(); 70 Map cache = new HashMap (); 71 72 for (Iterator it = cookieMap.entrySet().iterator(); it.hasNext();) 73 { 74 Entry entry = (Entry) it.next(); 75 assertNull(cache.put(entry.getKey(), entry.getValue())); 76 } 77 78 for (Iterator it = cookieMap.entrySet().iterator(); it.hasNext();) 79 { 80 Entry entry = (Entry) it.next(); 81 assertSame(entry.getValue(), cache.put(entry.getKey(), entry.getValue())); 82 } 83 84 assertSame("cookie1.value", cache.get("cookie1")); 85 } 86 87 public void testKeySet() 88 { 89 Map cookieMap = _facesContext.getExternalContext().getRequestCookieMap(); 90 Map cache = new HashMap (); 91 92 for (Iterator it = cookieMap.keySet().iterator(); it.hasNext();) 93 { 94 String key = (String ) it.next(); 95 assertNull(cache.put(key, cookieMap.get(key))); 96 } 97 98 for (Iterator it = cookieMap.keySet().iterator(); it.hasNext();) 99 { 100 String key = (String ) it.next(); 101 assertSame(cookieMap.get(key), cache.put(key, cookieMap.get(key))); 102 } 103 104 assertSame("cookie1.value", cache.get("cookie1")); 105 } 106 107 public void testValues() 108 { 109 Map cookieMap = _facesContext.getExternalContext().getRequestCookieMap(); 110 Set cache = new HashSet (); 111 112 for (Iterator it = cookieMap.values().iterator(); it.hasNext();) 113 { 114 assertTrue(cache.add(it.next())); 115 } 116 117 for (Iterator it = cookieMap.values().iterator(); it.hasNext();) 118 { 119 assertFalse(cache.add(it.next())); 120 } 121 122 assertTrue(cache.contains("cookie1.value")); 123 } 124 } 125 | Popular Tags |