KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Map.Entry;
25
26 import org.apache.myfaces.MyFacesBaseTest;
27
28 /**
29  * Test for AbstractAttributeMap core functionality through ApplicationMap.
30  *
31  * @author Anton Koinov (latest modification by $Author: matze $)
32  * @version $Revision: 1.6 $ $Date: 2004/10/13 11:50:59 $
33  *
34  * $Log: ApplicationMapTest.java,v $
35  * Revision 1.6 2004/10/13 11:50:59 matze
36  * renamed packages to org.apache
37  *
38  * Revision 1.5 2004/07/01 22:01:09 mwessendorf
39  * ASF switch
40  *
41  * Revision 1.4 2004/04/02 13:57:18 bdudney
42  * cleaned up the tests so they all run and added
43 execution to the build.xml file.
44  *
45  * Revision 1.3 2004/03/30 05:38:50 dave0000
46  * add copyright statement
47  *
48  */

49 public class ApplicationMapTest extends MyFacesBaseTest
50 {
51     private Object JavaDoc _test0 = new Object JavaDoc();
52     private Object JavaDoc _test1 = new Object JavaDoc();
53     
54     public ApplicationMapTest(String JavaDoc name)
55     {
56         super(name);
57     }
58     
59     private void init()
60     {
61         Map JavaDoc 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 JavaDoc test2 = new Object JavaDoc();
74         assertNull(appMap.put("test2", test2));
75
76         Object JavaDoc test3 = new Object JavaDoc();
77         assertNull(appMap.put("test3", test3));
78
79         Object JavaDoc test4 = new Object JavaDoc();
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         // Map
98
init();
99         Map JavaDoc 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         // EntrySet
109
init();
110         Set JavaDoc 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         // KeySet
122
init();
123         Set JavaDoc 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         // Values
135
init();
136         Collection JavaDoc 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 JavaDoc appMap = _facesContext.getExternalContext().getApplicationMap();
152         Map JavaDoc cache = new HashMap JavaDoc();
153         
154         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc()));
184                     assertEquals(size, appMap.size());
185                     break findTest0;
186                 }
187             }
188             throw new IllegalStateException JavaDoc("Test0 not found");
189         }
190         
191         for (Iterator JavaDoc it = appMap.entrySet().iterator(); it.hasNext();)
192         {
193             // Only one remove() will succeed
194
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 JavaDoc appMap = _facesContext.getExternalContext().getApplicationMap();
207         Map JavaDoc cache = new HashMap JavaDoc();
208         
209         for (Iterator JavaDoc it = appMap.keySet().iterator(); it.hasNext();)
210         {
211             String JavaDoc key = (String JavaDoc) it.next();
212             assertNull(cache.put(key, appMap.get(key)));
213         }
214
215         for (Iterator JavaDoc it = appMap.keySet().iterator(); it.hasNext();)
216         {
217             String JavaDoc key = (String JavaDoc) 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 JavaDoc it = appMap.keySet().iterator(); it.hasNext();)
230             {
231                 String JavaDoc key = (String JavaDoc) 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 JavaDoc()));
239                     assertEquals(size, appMap.size());
240                     break findTest0;
241                 }
242             }
243             throw new IllegalStateException JavaDoc("Test0 not found");
244         }
245         
246         for (Iterator JavaDoc it = appMap.entrySet().iterator(); it.hasNext();)
247         {
248             // Only one remove() will succeed
249
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 JavaDoc appMap = _facesContext.getExternalContext().getApplicationMap();
262         Set JavaDoc cache = new HashSet JavaDoc();
263         
264         for (Iterator JavaDoc it = appMap.values().iterator(); it.hasNext();)
265         {
266             assertTrue(cache.add(it.next()));
267         }
268
269         for (Iterator JavaDoc 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 JavaDoc it = appMap.values().iterator(); it.hasNext();)
283             {
284                 Object JavaDoc 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 JavaDoc()));
292                     assertEquals(size, appMap.size());
293                     break findTest0;
294                 }
295             }
296             throw new IllegalStateException JavaDoc("Test0 not found");
297         }
298         
299         for (Iterator JavaDoc it = appMap.entrySet().iterator(); it.hasNext();)
300         {
301             // Only one remove() will succeed
302
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