Source Code Cross Referenced for IdentityHashMapTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.io.Serializable;
021:        import java.util.Collection;
022:        import java.util.HashSet;
023:        import java.util.IdentityHashMap;
024:        import java.util.Iterator;
025:        import java.util.Map;
026:        import java.util.Set;
027:        import java.util.TreeSet;
028:
029:        import org.apache.harmony.testframework.serialization.SerializationTest;
030:        import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
031:
032:        public class IdentityHashMapTest extends junit.framework.TestCase {
033:
034:            /**
035:             * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
036:             * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
037:             * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
038:             * @tests java.util.IdentityHashMap#get(java.lang.Object)
039:             */
040:            public void test_null_Keys_and_Values() {
041:                // tests with null keys and values
042:                IdentityHashMap map = new IdentityHashMap();
043:                Object result;
044:
045:                // null key and null value
046:                result = map.put(null, null);
047:                assertTrue("testA can not find null key", map.containsKey(null));
048:                assertTrue("testA can not find null value", map
049:                        .containsValue(null));
050:                assertNull("testA can not get null value for null key", map
051:                        .get(null));
052:                assertNull("testA put returned wrong value", result);
053:
054:                // null value
055:                String value = "a value";
056:                result = map.put(null, value);
057:                assertTrue("testB can not find null key", map.containsKey(null));
058:                assertTrue("testB can not find a value with null key", map
059:                        .containsValue(value));
060:                assertTrue("testB can not get value for null key", map
061:                        .get(null) == value);
062:                assertNull("testB put returned wrong value", result);
063:
064:                // a null key
065:                String key = "a key";
066:                result = map.put(key, null);
067:                assertTrue("testC can not find a key with null value", map
068:                        .containsKey(key));
069:                assertTrue("testC can not find null value", map
070:                        .containsValue(null));
071:                assertNull("testC can not get null value for key", map.get(key));
072:                assertNull("testC put returned wrong value", result);
073:
074:                // another null key
075:                String anothervalue = "another value";
076:                result = map.put(null, anothervalue);
077:                assertTrue("testD can not find null key", map.containsKey(null));
078:                assertTrue("testD can not find a value with null key", map
079:                        .containsValue(anothervalue));
080:                assertTrue("testD can not get value for null key", map
081:                        .get(null) == anothervalue);
082:                assertTrue("testD put returned wrong value", result == value);
083:
084:                // remove a null key
085:                result = map.remove(null);
086:                assertTrue("testE remove returned wrong value",
087:                        result == anothervalue);
088:                assertTrue("testE should not find null key", !map
089:                        .containsKey(null));
090:                assertTrue("testE should not find a value with null key", !map
091:                        .containsValue(anothervalue));
092:                assertNull("testE should not get value for null key", map
093:                        .get(null));
094:            }
095:
096:            /**
097:             * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
098:             */
099:            public void test_putLjava_lang_ObjectLjava_lang_Object() {
100:                IdentityHashMap<Object, Object> map = new IdentityHashMap<Object, Object>();
101:
102:                // Test null as a key.
103:                Object value = "Some value";
104:                map.put(null, value);
105:                assertSame("Assert 0: Failure getting null key", value, map
106:                        .get(null));
107:
108:                // Test null as a value
109:                Object key = "Some key";
110:                map.put(key, null);
111:                assertNull("Assert 1: Failure getting null value", map.get(key));
112:            }
113:
114:            /**
115:             * @tests java.util.IdentityHashMap#remove(java.lang.Object)
116:             * @tests java.util.IdentityHashMap#keySet()
117:             */
118:            public void test_remove() {
119:                IdentityHashMap map = new IdentityHashMap();
120:                map.put(null, null);
121:                map.put("key1", "value1");
122:                map.put("key2", "value2");
123:                map.remove("key1");
124:
125:                assertTrue("Did not remove key1", !map.containsKey("key1"));
126:                assertTrue("Did not remove the value for key1", !map
127:                        .containsValue("value1"));
128:
129:                assertTrue("Modified key2", map.get("key2") != null
130:                        && map.get("key2") == "value2");
131:                assertNull("Modified null entry", map.get(null));
132:            }
133:
134:            /**
135:             * @tests java.util.IdentityHashMapTest#remove(java.lang.Object)
136:             */
137:            public void test_removeLjava_lang_Object() {
138:                // Regression for HARMONY-37
139:                IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>();
140:                hashMap.remove("absent");
141:                assertEquals("Assert 0: Size is incorrect", 0, hashMap.size());
142:
143:                hashMap.put("key", "value");
144:                hashMap.remove("key");
145:                assertEquals(
146:                        "Assert 1: After removing non-null element size is incorrect",
147:                        0, hashMap.size());
148:
149:                hashMap.put(null, null);
150:                assertEquals("Assert 2: adding literal null failed", 1, hashMap
151:                        .size());
152:                hashMap.remove(null);
153:                assertEquals(
154:                        "Assert 3: After removing null element size is incorrect",
155:                        0, hashMap.size());
156:            }
157:
158:            /**
159:             * @tests java.util.IdentityHashMap#entrySet()
160:             * @tests java.util.IdentityHashMap#keySet()
161:             * @tests java.util.IdentityHashMap#values()
162:             */
163:            public void test_sets() {
164:                // tests with null keys and values
165:                IdentityHashMap map = new IdentityHashMap();
166:
167:                // null key and null value
168:                map.put("key", "value");
169:                map.put(null, null);
170:                map.put("a key", null);
171:                map.put("another key", null);
172:
173:                Set keyset = map.keySet();
174:                Collection valueset = map.values();
175:                Set entries = map.entrySet();
176:                Iterator it = entries.iterator();
177:                while (it.hasNext()) {
178:                    Map.Entry entry = (Map.Entry) it.next();
179:                    assertTrue("EntrySetIterator can not find entry ", entries
180:                            .contains(entry));
181:
182:                    assertTrue("entry key not found in map", map
183:                            .containsKey(entry.getKey()));
184:                    assertTrue("entry value not found in map", map
185:                            .containsValue(entry.getValue()));
186:
187:                    assertTrue("entry key not found in the keyset", keyset
188:                            .contains(entry.getKey()));
189:                    assertTrue("entry value not found in the valueset",
190:                            valueset.contains(entry.getValue()));
191:                }
192:            }
193:
194:            /**
195:             * @tests java.util.IdentityHashMap#entrySet()
196:             * @tests java.util.IdentityHashMap#remove(java.lang.Object)
197:             */
198:            public void test_entrySet_removeAll() {
199:                IdentityHashMap map = new IdentityHashMap();
200:                for (int i = 0; i < 1000; i++) {
201:                    map.put(new Integer(i), new Integer(i));
202:                }
203:                Set set = map.entrySet();
204:
205:                set.removeAll(set);
206:                assertEquals("did not remove all elements in the map", 0, map
207:                        .size());
208:                assertTrue("did not remove all elements in the entryset", set
209:                        .isEmpty());
210:
211:                Iterator it = set.iterator();
212:                assertTrue("entrySet iterator still has elements", !it
213:                        .hasNext());
214:            }
215:
216:            /**
217:             * @tests java.util.IdentityHashMap#keySet()
218:             * @tests java.util.IdentityHashMap#clear()
219:             */
220:            public void test_keySet_clear() {
221:                IdentityHashMap map = new IdentityHashMap();
222:                for (int i = 0; i < 1000; i++) {
223:                    map.put(new Integer(i), new Integer(i));
224:                }
225:                Set set = map.keySet();
226:                set.clear();
227:
228:                assertEquals("did not remove all elements in the map", 0, map
229:                        .size());
230:                assertTrue("did not remove all elements in the keyset", set
231:                        .isEmpty());
232:
233:                Iterator it = set.iterator();
234:                assertTrue("keySet iterator still has elements", !it.hasNext());
235:            }
236:
237:            /**
238:             * @tests java.util.IdentityHashMap#values()
239:             */
240:            public void test_values() {
241:
242:                IdentityHashMap map = new IdentityHashMap();
243:                for (int i = 0; i < 10; i++) {
244:                    map.put(new Integer(i), new Integer(i));
245:                }
246:
247:                Integer key = new Integer(20);
248:                Integer value = new Integer(40);
249:                map.put(key, value);
250:
251:                Collection vals = map.values();
252:                boolean result = vals.remove(key);
253:                assertTrue("removed entries incorrectly", map.size() == 11
254:                        && !result);
255:                assertTrue("removed key incorrectly", map.containsKey(key));
256:                assertTrue("removed value incorrectly", map
257:                        .containsValue(value));
258:
259:                result = vals.remove(value);
260:                assertTrue("Did not remove entry as expected", map.size() == 10
261:                        && result);
262:                assertTrue("Did not remove key as expected", !map
263:                        .containsKey(key));
264:                assertTrue("Did not remove value as expected", !map
265:                        .containsValue(value));
266:
267:                // put an equivalent key to a value
268:                key = new Integer(1);
269:                value = new Integer(100);
270:                map.put(key, value);
271:
272:                result = vals.remove(key);
273:                assertTrue("TestB. removed entries incorrectly",
274:                        map.size() == 11 && !result);
275:                assertTrue("TestB. removed key incorrectly", map
276:                        .containsKey(key));
277:                assertTrue("TestB. removed value incorrectly", map
278:                        .containsValue(value));
279:
280:                result = vals.remove(value);
281:                assertTrue("TestB. Did not remove entry as expected", map
282:                        .size() == 10
283:                        && result);
284:                assertTrue("TestB. Did not remove key as expected", !map
285:                        .containsKey(key));
286:                assertTrue("TestB. Did not remove value as expected", !map
287:                        .containsValue(value));
288:
289:                vals.clear();
290:                assertEquals("Did not remove all entries as expected", 0, map
291:                        .size());
292:            }
293:
294:            /**
295:             * @tests java.util.IdentityHashMap#keySet()
296:             * @tests java.util.IdentityHashMap#remove(java.lang.Object)
297:             */
298:            public void test_keySet_removeAll() {
299:                IdentityHashMap map = new IdentityHashMap();
300:                for (int i = 0; i < 1000; i++) {
301:                    map.put(new Integer(i), new Integer(i));
302:                }
303:                Set set = map.keySet();
304:                set.removeAll(set);
305:
306:                assertEquals("did not remove all elements in the map", 0, map
307:                        .size());
308:                assertTrue("did not remove all elements in the keyset", set
309:                        .isEmpty());
310:
311:                Iterator it = set.iterator();
312:                assertTrue("keySet iterator still has elements", !it.hasNext());
313:            }
314:
315:            /**
316:             * @tests java.util.IdentityHashMap#keySet()
317:             */
318:            public void test_keySet_retainAll() {
319:                IdentityHashMap map = new IdentityHashMap();
320:                for (int i = 0; i < 1000; i++) {
321:                    map.put(new Integer(i), new Integer(i));
322:                }
323:                Set set = map.keySet();
324:
325:                // retain all the elements
326:                boolean result = set.retainAll(set);
327:                assertTrue("retain all should return false", !result);
328:                assertEquals("did not retain all", 1000, set.size());
329:
330:                // send empty set to retainAll
331:                result = set.retainAll(new TreeSet());
332:                assertTrue("retain all should return true", result);
333:                assertEquals("did not remove all elements in the map", 0, map
334:                        .size());
335:                assertTrue("did not remove all elements in the keyset", set
336:                        .isEmpty());
337:
338:                Iterator it = set.iterator();
339:                assertTrue("keySet iterator still has elements", !it.hasNext());
340:            }
341:
342:            /**
343:             * @tests java.util.IdentityHashMap#keySet()
344:             * @tests java.util.IdentityHashMap#remove(java.lang.Object)
345:             */
346:            public void test_keyset_remove() {
347:                IdentityHashMap map = new IdentityHashMap();
348:
349:                Integer key = new Integer(21);
350:
351:                map.put(new Integer(1), null);
352:                map.put(new Integer(11), null);
353:                map.put(key, null);
354:                map.put(new Integer(31), null);
355:                map.put(new Integer(41), null);
356:                map.put(new Integer(51), null);
357:                map.put(new Integer(61), null);
358:                map.put(new Integer(71), null);
359:                map.put(new Integer(81), null);
360:                map.put(new Integer(91), null);
361:
362:                Set set = map.keySet();
363:
364:                Set newset = new HashSet();
365:                Iterator it = set.iterator();
366:                while (it.hasNext()) {
367:                    Object element = it.next();
368:                    if (element == key) {
369:                        it.remove();
370:                    } else
371:                        newset.add(element);
372:                }
373:                int size = newset.size();
374:                assertTrue("keyset and newset don't have same size", newset
375:                        .size() == size);
376:                assertTrue("element is in newset ", !newset.contains(key));
377:                assertTrue("element not removed from keyset", !set
378:                        .contains(key));
379:                assertTrue("element not removed from map", !map
380:                        .containsKey(key));
381:
382:                assertTrue("newset and keyset do not have same elements 1",
383:                        newset.equals(set));
384:                assertTrue("newset and keyset do not have same elements 2", set
385:                        .equals(newset));
386:            }
387:
388:            // comparator for IdentityHashMap objects
389:            private static final SerializableAssert COMPARATOR = new SerializableAssert() {
390:                public void assertDeserialized(Serializable initial,
391:                        Serializable deserialized) {
392:
393:                    IdentityHashMap init = (IdentityHashMap) initial;
394:                    IdentityHashMap desr = (IdentityHashMap) deserialized;
395:
396:                    assertEquals("Size", init.size(), desr.size());
397:                }
398:            };
399:
400:            /**
401:             * @tests serialization/deserialization compatibility with RI.
402:             */
403:            public void testSerializationCompatibility() throws Exception {
404:                IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>();
405:                identityHashMap.put("key1", "value1");
406:                identityHashMap.put("key2", "value2");
407:                identityHashMap.put("key3", "value3");
408:
409:                SerializationTest.verifyGolden(this, identityHashMap,
410:                        COMPARATOR);
411:            }
412:        }
w_w___w.__j__a__v___a2___s__._co__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.