KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > servlet > CookieMapTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.context.servlet;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.Map.Entry;
24
25 import javax.servlet.http.Cookie JavaDoc;
26
27 import org.apache.myfaces.MyFacesBaseTest;
28
29
30 public class CookieMapTest extends MyFacesBaseTest
31 {
32     public CookieMapTest(String JavaDoc name)
33     {
34         super(name);
35     }
36     
37     protected Cookie JavaDoc[] getCookies()
38     {
39         Cookie JavaDoc[] cookies = new Cookie JavaDoc[5];
40         
41         Cookie JavaDoc cookie = new Cookie JavaDoc("cookie0", "cookie0.value");
42         cookies[0] = cookie;
43         
44         cookie = new Cookie JavaDoc("cookie1", "cookie1.value");
45         cookies[1] = cookie;
46         
47         cookie = new Cookie JavaDoc("cookie2", "cookie2.value");
48         cookies[2] = cookie;
49         
50         cookie = new Cookie JavaDoc("cookie3", "cookie3.value");
51         cookies[3] = cookie;
52         
53         cookie = new Cookie JavaDoc("cookie4", "cookie4.value");
54         cookies[4] = cookie;
55         
56         return cookies;
57     }
58     
59     public void testCookies()
60     {
61         Map JavaDoc appMap = _facesContext.getExternalContext().getRequestCookieMap();
62         
63         assertFalse(appMap.isEmpty());
64         assertEquals(5, appMap.size());
65     }
66
67     public void testEntrySet()
68     {
69         Map JavaDoc cookieMap = _facesContext.getExternalContext().getRequestCookieMap();
70         Map JavaDoc cache = new HashMap JavaDoc();
71         
72         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc cookieMap = _facesContext.getExternalContext().getRequestCookieMap();
90         Map JavaDoc cache = new HashMap JavaDoc();
91         
92         for (Iterator JavaDoc it = cookieMap.keySet().iterator(); it.hasNext();)
93         {
94             String JavaDoc key = (String JavaDoc) it.next();
95             assertNull(cache.put(key, cookieMap.get(key)));
96         }
97
98         for (Iterator JavaDoc it = cookieMap.keySet().iterator(); it.hasNext();)
99         {
100             String JavaDoc key = (String JavaDoc) 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 JavaDoc cookieMap = _facesContext.getExternalContext().getRequestCookieMap();
110         Set JavaDoc cache = new HashSet JavaDoc();
111         
112         for (Iterator JavaDoc it = cookieMap.values().iterator(); it.hasNext();)
113         {
114             assertTrue(cache.add(it.next()));
115         }
116
117         for (Iterator JavaDoc 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