Source Code Cross Referenced for TestMapUtils.java in  » Library » Apache-common-Collections » org » apache » commons » collections » 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 » Library » Apache common Collections » org.apache.commons.collections 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections;
017:
018:        import java.io.ByteArrayOutputStream;
019:        import java.io.PrintStream;
020:        import java.util.HashMap;
021:        import java.util.HashSet;
022:        import java.util.Iterator;
023:        import java.util.ListResourceBundle;
024:        import java.util.Map;
025:        import java.util.ResourceBundle;
026:        import java.util.Set;
027:        import java.util.TreeMap;
028:
029:        import junit.framework.Test;
030:
031:        import org.apache.commons.collections.keyvalue.DefaultKeyValue;
032:        import org.apache.commons.collections.keyvalue.DefaultMapEntry;
033:        import org.apache.commons.collections.map.LazyMap;
034:        import org.apache.commons.collections.map.PredicatedMap;
035:        import org.apache.commons.collections.map.TestPredicatedMap;
036:
037:        /**
038:         * Tests for MapUtils.
039:         *
040:         * @version $Revision: 348629 $ $Date: 2005-11-24 03:40:42 +0000 (Thu, 24 Nov 2005) $
041:         *
042:         * @author Stephen Colebourne
043:         * @author Arun Mammen Thomas
044:         * @author Max Rydahl Andersen
045:         * @author Janek Bogucki
046:         * @author Neil O'Toole
047:         */
048:        public class TestMapUtils extends BulkTest {
049:
050:            public TestMapUtils(String name) {
051:                super (name);
052:            }
053:
054:            public static Test suite() {
055:                return BulkTest.makeSuite(TestMapUtils.class);
056:            }
057:
058:            public Predicate getPredicate() {
059:                return new Predicate() {
060:                    public boolean evaluate(Object o) {
061:                        return o instanceof  String;
062:                    }
063:                };
064:            }
065:
066:            public void testPredicatedMap() {
067:                Predicate p = getPredicate();
068:                Map map = MapUtils.predicatedMap(new HashMap(), p, p);
069:                assertTrue("returned object should be a PredicatedMap",
070:                        map instanceof  PredicatedMap);
071:                try {
072:                    map = MapUtils.predicatedMap(null, p, p);
073:                    fail("Expecting IllegalArgumentException for null map.");
074:                } catch (IllegalArgumentException e) {
075:                    // expected
076:                }
077:            }
078:
079:            // Since a typed map is a predicated map, I copied the tests for predicated map
080:            public void testTypedMapIllegalPut() {
081:                final Map map = MapUtils.typedMap(new HashMap(), String.class,
082:                        String.class);
083:
084:                try {
085:                    map.put("Hi", new Integer(3));
086:                    fail("Illegal value should raise IllegalArgument");
087:                } catch (IllegalArgumentException e) {
088:                    // expected
089:                }
090:
091:                try {
092:                    map.put(new Integer(3), "Hi");
093:                    fail("Illegal key should raise IllegalArgument");
094:                } catch (IllegalArgumentException e) {
095:                    // expected
096:                }
097:
098:                assertTrue(!map.containsKey(new Integer(3)));
099:                assertTrue(!map.containsValue(new Integer(3)));
100:
101:                Map map2 = new HashMap();
102:                map2.put("A", "a");
103:                map2.put("B", "b");
104:                map2.put("C", "c");
105:                map2.put("c", new Integer(3));
106:
107:                try {
108:                    map.putAll(map2);
109:                    fail("Illegal value should raise IllegalArgument");
110:                } catch (IllegalArgumentException e) {
111:                    // expected
112:                }
113:
114:                map.put("E", "e");
115:                Iterator iterator = map.entrySet().iterator();
116:                try {
117:                    Map.Entry entry = (Map.Entry) iterator.next();
118:                    entry.setValue(new Integer(3));
119:                    fail("Illegal value should raise IllegalArgument");
120:                } catch (IllegalArgumentException e) {
121:                    // expected
122:                }
123:
124:            }
125:
126:            public BulkTest bulkTestTypedMap() {
127:                return new TestPredicatedMap("") {
128:                    public boolean isAllowNullKey() {
129:                        return false;
130:                    }
131:
132:                    public boolean isAllowNullValue() {
133:                        return false;
134:                    }
135:
136:                    public Map makeEmptyMap() {
137:                        return MapUtils.typedMap(new HashMap(), String.class,
138:                                String.class);
139:                    }
140:                };
141:            }
142:
143:            public void testLazyMapFactory() {
144:                Factory factory = FactoryUtils.constantFactory(new Integer(5));
145:                Map map = MapUtils.lazyMap(new HashMap(), factory);
146:                assertTrue(map instanceof  LazyMap);
147:                try {
148:                    map = MapUtils.lazyMap(new HashMap(), (Factory) null);
149:                    fail("Expecting IllegalArgumentException for null factory");
150:                } catch (IllegalArgumentException e) {
151:                    // expected
152:                }
153:                try {
154:                    map = MapUtils.lazyMap(null, factory);
155:                    fail("Expecting IllegalArgumentException for null map");
156:                } catch (IllegalArgumentException e) {
157:                    // expected
158:                }
159:                Transformer transformer = TransformerUtils
160:                        .asTransformer(factory);
161:                map = MapUtils.lazyMap(new HashMap(), transformer);
162:                assertTrue(map instanceof  LazyMap);
163:                try {
164:                    map = MapUtils.lazyMap(new HashMap(), (Transformer) null);
165:                    fail("Expecting IllegalArgumentException for null transformer");
166:                } catch (IllegalArgumentException e) {
167:                    // expected
168:                }
169:                try {
170:                    map = MapUtils.lazyMap(null, transformer);
171:                    fail("Expecting IllegalArgumentException for null map");
172:                } catch (IllegalArgumentException e) {
173:                    // expected
174:                }
175:            }
176:
177:            public void testLazyMapTransformer() {
178:                Map map = MapUtils.lazyMap(new HashMap(), new Transformer() {
179:                    public Object transform(Object mapKey) {
180:                        if (mapKey instanceof  String) {
181:                            return new Integer((String) mapKey);
182:                        }
183:                        return null;
184:                    }
185:                });
186:
187:                assertEquals(0, map.size());
188:                Integer i1 = (Integer) map.get("5");
189:                assertEquals(new Integer(5), i1);
190:                assertEquals(1, map.size());
191:                Integer i2 = (Integer) map.get(new String(new char[] { '5' }));
192:                assertEquals(new Integer(5), i2);
193:                assertEquals(1, map.size());
194:                assertSame(i1, i2);
195:            }
196:
197:            public void testInvertMap() {
198:                final Map in = new HashMap(5, 1);
199:                in.put("1", "A");
200:                in.put("2", "B");
201:                in.put("3", "C");
202:                in.put("4", "D");
203:                in.put("5", "E");
204:
205:                final Set inKeySet = new HashSet(in.keySet());
206:                final Set inValSet = new HashSet(in.values());
207:
208:                final Map out = MapUtils.invertMap(in);
209:
210:                final Set outKeySet = new HashSet(out.keySet());
211:                final Set outValSet = new HashSet(out.values());
212:
213:                assertTrue(inKeySet.equals(outValSet));
214:                assertTrue(inValSet.equals(outKeySet));
215:
216:                assertEquals(out.get("A"), "1");
217:                assertEquals(out.get("B"), "2");
218:                assertEquals(out.get("C"), "3");
219:                assertEquals(out.get("D"), "4");
220:                assertEquals(out.get("E"), "5");
221:            }
222:
223:            public void testPutAll_Map_array() {
224:                try {
225:                    MapUtils.putAll(null, null);
226:                    fail();
227:                } catch (NullPointerException ex) {
228:                }
229:                try {
230:                    MapUtils.putAll(null, new Object[0]);
231:                    fail();
232:                } catch (NullPointerException ex) {
233:                }
234:
235:                Map test = MapUtils.putAll(new HashMap(), new String[0]);
236:                assertEquals(0, test.size());
237:
238:                // sub array
239:                test = MapUtils.putAll(new HashMap(), new String[][] {
240:                        { "RED", "#FF0000" }, { "GREEN", "#00FF00" },
241:                        { "BLUE", "#0000FF" } });
242:                assertEquals(true, test.containsKey("RED"));
243:                assertEquals("#FF0000", test.get("RED"));
244:                assertEquals(true, test.containsKey("GREEN"));
245:                assertEquals("#00FF00", test.get("GREEN"));
246:                assertEquals(true, test.containsKey("BLUE"));
247:                assertEquals("#0000FF", test.get("BLUE"));
248:                assertEquals(3, test.size());
249:
250:                try {
251:                    MapUtils
252:                            .putAll(new HashMap(), new String[][] {
253:                                    { "RED", "#FF0000" }, null,
254:                                    { "BLUE", "#0000FF" } });
255:                    fail();
256:                } catch (IllegalArgumentException ex) {
257:                }
258:
259:                try {
260:                    MapUtils.putAll(new HashMap(), new String[][] {
261:                            { "RED", "#FF0000" }, { "GREEN" },
262:                            { "BLUE", "#0000FF" } });
263:                    fail();
264:                } catch (IllegalArgumentException ex) {
265:                }
266:
267:                try {
268:                    MapUtils.putAll(new HashMap(), new String[][] {
269:                            { "RED", "#FF0000" }, {}, { "BLUE", "#0000FF" } });
270:                    fail();
271:                } catch (IllegalArgumentException ex) {
272:                }
273:
274:                // flat array
275:                test = MapUtils.putAll(new HashMap(), new String[] { "RED",
276:                        "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF" });
277:                assertEquals(true, test.containsKey("RED"));
278:                assertEquals("#FF0000", test.get("RED"));
279:                assertEquals(true, test.containsKey("GREEN"));
280:                assertEquals("#00FF00", test.get("GREEN"));
281:                assertEquals(true, test.containsKey("BLUE"));
282:                assertEquals("#0000FF", test.get("BLUE"));
283:                assertEquals(3, test.size());
284:
285:                test = MapUtils.putAll(new HashMap(), new String[] { "RED",
286:                        "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF",
287:                        "PURPLE" // ignored
288:                });
289:                assertEquals(true, test.containsKey("RED"));
290:                assertEquals("#FF0000", test.get("RED"));
291:                assertEquals(true, test.containsKey("GREEN"));
292:                assertEquals("#00FF00", test.get("GREEN"));
293:                assertEquals(true, test.containsKey("BLUE"));
294:                assertEquals("#0000FF", test.get("BLUE"));
295:                assertEquals(3, test.size());
296:
297:                // map entry
298:                test = MapUtils.putAll(new HashMap(), new Object[] {
299:                        new DefaultMapEntry("RED", "#FF0000"),
300:                        new DefaultMapEntry("GREEN", "#00FF00"),
301:                        new DefaultMapEntry("BLUE", "#0000FF") });
302:                assertEquals(true, test.containsKey("RED"));
303:                assertEquals("#FF0000", test.get("RED"));
304:                assertEquals(true, test.containsKey("GREEN"));
305:                assertEquals("#00FF00", test.get("GREEN"));
306:                assertEquals(true, test.containsKey("BLUE"));
307:                assertEquals("#0000FF", test.get("BLUE"));
308:                assertEquals(3, test.size());
309:
310:                // key value
311:                test = MapUtils.putAll(new HashMap(), new Object[] {
312:                        new DefaultKeyValue("RED", "#FF0000"),
313:                        new DefaultKeyValue("GREEN", "#00FF00"),
314:                        new DefaultKeyValue("BLUE", "#0000FF") });
315:                assertEquals(true, test.containsKey("RED"));
316:                assertEquals("#FF0000", test.get("RED"));
317:                assertEquals(true, test.containsKey("GREEN"));
318:                assertEquals("#00FF00", test.get("GREEN"));
319:                assertEquals(true, test.containsKey("BLUE"));
320:                assertEquals("#0000FF", test.get("BLUE"));
321:                assertEquals(3, test.size());
322:            }
323:
324:            public void testConvertResourceBundle() {
325:                final Map in = new HashMap(5, 1);
326:                in.put("1", "A");
327:                in.put("2", "B");
328:                in.put("3", "C");
329:                in.put("4", "D");
330:                in.put("5", "E");
331:
332:                ResourceBundle b = new ListResourceBundle() {
333:                    public Object[][] getContents() {
334:                        final Object[][] contents = new Object[in.size()][2];
335:                        final Iterator i = in.keySet().iterator();
336:                        int n = 0;
337:                        while (i.hasNext()) {
338:                            final Object key = i.next();
339:                            final Object val = in.get(key);
340:                            contents[n][0] = key;
341:                            contents[n][1] = val;
342:                            ++n;
343:                        }
344:                        return contents;
345:                    }
346:                };
347:
348:                final Map out = MapUtils.toMap(b);
349:
350:                assertTrue(in.equals(out));
351:            }
352:
353:            public void testDebugAndVerbosePrintCasting() {
354:                final Map inner = new HashMap(2, 1);
355:                inner.put(new Integer(2), "B");
356:                inner.put(new Integer(3), "C");
357:
358:                final Map outer = new HashMap(2, 1);
359:                outer.put(new Integer(0), inner);
360:                outer.put(new Integer(1), "A");
361:
362:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
363:                final PrintStream outPrint = new PrintStream(out);
364:
365:                try {
366:                    MapUtils.debugPrint(outPrint, "Print Map", outer);
367:                } catch (final ClassCastException e) {
368:                    fail("No Casting should be occurring!");
369:                }
370:            }
371:
372:            public void testDebugAndVerbosePrintNullMap() {
373:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
374:                final PrintStream outPrint = new PrintStream(out);
375:
376:                final String LABEL = "Print Map";
377:                outPrint.println(LABEL + " = " + String.valueOf((Object) null));
378:                final String EXPECTED_OUT = out.toString();
379:
380:                out.reset();
381:
382:                MapUtils.debugPrint(outPrint, LABEL, null);
383:                assertEquals(EXPECTED_OUT, out.toString());
384:
385:                out.reset();
386:
387:                MapUtils.verbosePrint(outPrint, LABEL, null);
388:                assertEquals(EXPECTED_OUT, out.toString());
389:            }
390:
391:            public void testVerbosePrintNullLabel() {
392:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
393:                final PrintStream outPrint = new PrintStream(out);
394:
395:                final String INDENT = "    ";
396:
397:                final Map map = new TreeMap(); // treeMap guarantees order across JDKs for test
398:                map.put(new Integer(2), "B");
399:                map.put(new Integer(3), "C");
400:                map.put(new Integer(4), null);
401:
402:                outPrint.println("{");
403:                outPrint.println(INDENT + "2 = B");
404:                outPrint.println(INDENT + "3 = C");
405:                outPrint.println(INDENT + "4 = null");
406:                outPrint.println("}");
407:                final String EXPECTED_OUT = out.toString();
408:                out.reset();
409:
410:                MapUtils.verbosePrint(outPrint, null, map);
411:                assertEquals(EXPECTED_OUT, out.toString());
412:            }
413:
414:            public void testDebugPrintNullLabel() {
415:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
416:                final PrintStream outPrint = new PrintStream(out);
417:
418:                final String INDENT = "    ";
419:
420:                final Map map = new TreeMap(); // treeMap guarantees order across JDKs for test
421:                map.put(new Integer(2), "B");
422:                map.put(new Integer(3), "C");
423:                map.put(new Integer(4), null);
424:
425:                outPrint.println("{");
426:                outPrint.println(INDENT + "2 = B " + String.class.getName());
427:                outPrint.println(INDENT + "3 = C " + String.class.getName());
428:                outPrint.println(INDENT + "4 = null");
429:                outPrint.println("} " + TreeMap.class.getName());
430:                final String EXPECTED_OUT = out.toString();
431:                out.reset();
432:
433:                MapUtils.debugPrint(outPrint, null, map);
434:                assertEquals(EXPECTED_OUT, out.toString());
435:            }
436:
437:            public void testVerbosePrintNullLabelAndMap() {
438:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
439:                final PrintStream outPrint = new PrintStream(out);
440:
441:                outPrint.println("null");
442:                final String EXPECTED_OUT = out.toString();
443:                out.reset();
444:
445:                MapUtils.verbosePrint(outPrint, null, null);
446:                assertEquals(EXPECTED_OUT, out.toString());
447:            }
448:
449:            public void testDebugPrintNullLabelAndMap() {
450:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
451:                final PrintStream outPrint = new PrintStream(out);
452:
453:                outPrint.println("null");
454:                final String EXPECTED_OUT = out.toString();
455:                out.reset();
456:
457:                MapUtils.debugPrint(outPrint, null, null);
458:                assertEquals(EXPECTED_OUT, out.toString());
459:            }
460:
461:            public void testVerbosePrintNullStream() {
462:                try {
463:                    MapUtils.verbosePrint(null, "Map", new HashMap());
464:                    fail("Should generate NullPointerException");
465:                } catch (NullPointerException expected) {
466:                }
467:            }
468:
469:            public void testDebugPrintNullStream() {
470:                try {
471:                    MapUtils.debugPrint(null, "Map", new HashMap());
472:                    fail("Should generate NullPointerException");
473:                } catch (NullPointerException expected) {
474:                }
475:            }
476:
477:            public void testDebugPrintNullKey() {
478:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
479:                final PrintStream outPrint = new PrintStream(out);
480:
481:                final String INDENT = "    ";
482:
483:                final Map map = new HashMap();
484:                map.put(null, "A");
485:
486:                outPrint.println("{");
487:                outPrint.println(INDENT + "null = A " + String.class.getName());
488:                outPrint.println("} " + HashMap.class.getName());
489:                final String EXPECTED_OUT = out.toString();
490:                out.reset();
491:
492:                MapUtils.debugPrint(outPrint, null, map);
493:                assertEquals(EXPECTED_OUT, out.toString());
494:            }
495:
496:            public void testVerbosePrintNullKey() {
497:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
498:                final PrintStream outPrint = new PrintStream(out);
499:
500:                final String INDENT = "    ";
501:
502:                final Map map = new HashMap();
503:                map.put(null, "A");
504:
505:                outPrint.println("{");
506:                outPrint.println(INDENT + "null = A");
507:                outPrint.println("}");
508:                final String EXPECTED_OUT = out.toString();
509:                out.reset();
510:
511:                MapUtils.verbosePrint(outPrint, null, map);
512:                assertEquals(EXPECTED_OUT, out.toString());
513:            }
514:
515:            public void testDebugPrintNullKeyToMap1() {
516:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
517:                final PrintStream outPrint = new PrintStream(out);
518:
519:                final String INDENT = "    ";
520:
521:                final Map map = new HashMap();
522:                map.put(null, map);
523:
524:                outPrint.println("{");
525:                outPrint.println(INDENT + "null = (this Map) "
526:                        + HashMap.class.getName());
527:                outPrint.println("} " + HashMap.class.getName());
528:                final String EXPECTED_OUT = out.toString();
529:                out.reset();
530:
531:                MapUtils.debugPrint(outPrint, null, map);
532:                assertEquals(EXPECTED_OUT, out.toString());
533:            }
534:
535:            public void testVerbosePrintNullKeyToMap1() {
536:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
537:                final PrintStream outPrint = new PrintStream(out);
538:
539:                final String INDENT = "    ";
540:
541:                final Map map = new HashMap();
542:                map.put(null, map);
543:
544:                outPrint.println("{");
545:                outPrint.println(INDENT + "null = (this Map)");
546:                outPrint.println("}");
547:                final String EXPECTED_OUT = out.toString();
548:                out.reset();
549:
550:                MapUtils.verbosePrint(outPrint, null, map);
551:                assertEquals(EXPECTED_OUT, out.toString());
552:            }
553:
554:            public void testDebugPrintNullKeyToMap2() {
555:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
556:                final PrintStream outPrint = new PrintStream(out);
557:
558:                final String INDENT = "    ";
559:
560:                final Map map = new HashMap();
561:                final Map map2 = new HashMap();
562:                map.put(null, map2);
563:                map2.put("2", "B");
564:
565:                outPrint.println("{");
566:                outPrint.println(INDENT + "null = ");
567:                outPrint.println(INDENT + "{");
568:                outPrint.println(INDENT + INDENT + "2 = B "
569:                        + String.class.getName());
570:                outPrint.println(INDENT + "} " + HashMap.class.getName());
571:                outPrint.println("} " + HashMap.class.getName());
572:                final String EXPECTED_OUT = out.toString();
573:                out.reset();
574:
575:                MapUtils.debugPrint(outPrint, null, map);
576:                assertEquals(EXPECTED_OUT, out.toString());
577:            }
578:
579:            public void testVerbosePrintNullKeyToMap2() {
580:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
581:                final PrintStream outPrint = new PrintStream(out);
582:
583:                final String INDENT = "    ";
584:
585:                final Map map = new HashMap();
586:                final Map map2 = new HashMap();
587:                map.put(null, map2);
588:                map2.put("2", "B");
589:
590:                outPrint.println("{");
591:                outPrint.println(INDENT + "null = ");
592:                outPrint.println(INDENT + "{");
593:                outPrint.println(INDENT + INDENT + "2 = B");
594:                outPrint.println(INDENT + "}");
595:                outPrint.println("}");
596:                final String EXPECTED_OUT = out.toString();
597:                out.reset();
598:
599:                MapUtils.verbosePrint(outPrint, null, map);
600:                assertEquals(EXPECTED_OUT, out.toString());
601:            }
602:
603:            public void testVerbosePrint() {
604:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
605:                final PrintStream outPrint = new PrintStream(out);
606:
607:                final String LABEL = "Print Map";
608:                final String INDENT = "    ";
609:
610:                outPrint.println(LABEL + " = ");
611:                outPrint.println("{");
612:                outPrint.println(INDENT + "0 = A");
613:                outPrint.println(INDENT + "1 = ");
614:                outPrint.println(INDENT + "{");
615:                outPrint.println(INDENT + INDENT + "2 = B");
616:                outPrint.println(INDENT + INDENT + "3 = C");
617:                outPrint.println(INDENT + "}");
618:                outPrint.println(INDENT + "7 = (this Map)");
619:                outPrint.println("}");
620:
621:                final String EXPECTED_OUT = out.toString();
622:
623:                out.reset();
624:
625:                final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
626:                inner.put(new Integer(2), "B");
627:                inner.put(new Integer(3), "C");
628:
629:                final Map outer = new TreeMap();
630:                outer.put(new Integer(1), inner);
631:                outer.put(new Integer(0), "A");
632:                outer.put(new Integer(7), outer);
633:
634:                MapUtils.verbosePrint(outPrint, "Print Map", outer);
635:                assertEquals(EXPECTED_OUT, out.toString());
636:            }
637:
638:            public void testDebugPrint() {
639:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
640:                final PrintStream outPrint = new PrintStream(out);
641:
642:                final String LABEL = "Print Map";
643:                final String INDENT = "    ";
644:
645:                outPrint.println(LABEL + " = ");
646:                outPrint.println("{");
647:                outPrint.println(INDENT + "0 = A " + String.class.getName());
648:                outPrint.println(INDENT + "1 = ");
649:                outPrint.println(INDENT + "{");
650:                outPrint.println(INDENT + INDENT + "2 = B "
651:                        + String.class.getName());
652:                outPrint.println(INDENT + INDENT + "3 = C "
653:                        + String.class.getName());
654:                outPrint.println(INDENT + "} " + TreeMap.class.getName());
655:                outPrint.println(INDENT + "7 = (this Map) "
656:                        + TreeMap.class.getName());
657:                outPrint.println("} " + TreeMap.class.getName());
658:
659:                final String EXPECTED_OUT = out.toString();
660:
661:                out.reset();
662:
663:                final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
664:                inner.put(new Integer(2), "B");
665:                inner.put(new Integer(3), "C");
666:
667:                final Map outer = new TreeMap();
668:                outer.put(new Integer(1), inner);
669:                outer.put(new Integer(0), "A");
670:                outer.put(new Integer(7), outer);
671:
672:                MapUtils.debugPrint(outPrint, "Print Map", outer);
673:                assertEquals(EXPECTED_OUT, out.toString());
674:            }
675:
676:            public void testVerbosePrintSelfReference() {
677:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
678:                final PrintStream outPrint = new PrintStream(out);
679:
680:                final String LABEL = "Print Map";
681:                final String INDENT = "    ";
682:
683:                final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
684:                final Map father = new TreeMap();
685:                final Map son = new TreeMap();
686:
687:                grandfather.put(new Integer(0), "A");
688:                grandfather.put(new Integer(1), father);
689:
690:                father.put(new Integer(2), "B");
691:                father.put(new Integer(3), grandfather);
692:                father.put(new Integer(4), son);
693:
694:                son.put(new Integer(5), "C");
695:                son.put(new Integer(6), grandfather);
696:                son.put(new Integer(7), father);
697:
698:                outPrint.println(LABEL + " = ");
699:                outPrint.println("{");
700:                outPrint.println(INDENT + "0 = A");
701:                outPrint.println(INDENT + "1 = ");
702:                outPrint.println(INDENT + "{");
703:                outPrint.println(INDENT + INDENT + "2 = B");
704:                outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map)");
705:                outPrint.println(INDENT + INDENT + "4 = ");
706:                outPrint.println(INDENT + INDENT + "{");
707:                outPrint.println(INDENT + INDENT + INDENT + "5 = C");
708:                outPrint.println(INDENT + INDENT + INDENT
709:                        + "6 = (ancestor[1] Map)");
710:                outPrint.println(INDENT + INDENT + INDENT
711:                        + "7 = (ancestor[0] Map)");
712:                outPrint.println(INDENT + INDENT + "}");
713:                outPrint.println(INDENT + "}");
714:                outPrint.println("}");
715:
716:                final String EXPECTED_OUT = out.toString();
717:
718:                out.reset();
719:                MapUtils.verbosePrint(outPrint, "Print Map", grandfather);
720:
721:                assertEquals(EXPECTED_OUT, out.toString());
722:            }
723:
724:            public void testDebugPrintSelfReference() {
725:                final ByteArrayOutputStream out = new ByteArrayOutputStream();
726:                final PrintStream outPrint = new PrintStream(out);
727:
728:                final String LABEL = "Print Map";
729:                final String INDENT = "    ";
730:
731:                final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
732:                final Map father = new TreeMap();
733:                final Map son = new TreeMap();
734:
735:                grandfather.put(new Integer(0), "A");
736:                grandfather.put(new Integer(1), father);
737:
738:                father.put(new Integer(2), "B");
739:                father.put(new Integer(3), grandfather);
740:                father.put(new Integer(4), son);
741:
742:                son.put(new Integer(5), "C");
743:                son.put(new Integer(6), grandfather);
744:                son.put(new Integer(7), father);
745:
746:                outPrint.println(LABEL + " = ");
747:                outPrint.println("{");
748:                outPrint.println(INDENT + "0 = A " + String.class.getName());
749:                outPrint.println(INDENT + "1 = ");
750:                outPrint.println(INDENT + "{");
751:                outPrint.println(INDENT + INDENT + "2 = B "
752:                        + String.class.getName());
753:                outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map) "
754:                        + TreeMap.class.getName());
755:                outPrint.println(INDENT + INDENT + "4 = ");
756:                outPrint.println(INDENT + INDENT + "{");
757:                outPrint.println(INDENT + INDENT + INDENT + "5 = C "
758:                        + String.class.getName());
759:                outPrint.println(INDENT + INDENT + INDENT
760:                        + "6 = (ancestor[1] Map) " + TreeMap.class.getName());
761:                outPrint.println(INDENT + INDENT + INDENT
762:                        + "7 = (ancestor[0] Map) " + TreeMap.class.getName());
763:                outPrint.println(INDENT + INDENT + "} "
764:                        + TreeMap.class.getName());
765:                outPrint.println(INDENT + "} " + TreeMap.class.getName());
766:                outPrint.println("} " + TreeMap.class.getName());
767:
768:                final String EXPECTED_OUT = out.toString();
769:
770:                out.reset();
771:                MapUtils.debugPrint(outPrint, "Print Map", grandfather);
772:
773:                assertEquals(EXPECTED_OUT, out.toString());
774:            }
775:
776:            //-----------------------------------------------------------------------
777:            public void testIsEmptyWithEmptyMap() {
778:                Map map = new HashMap();
779:                assertEquals(true, MapUtils.isEmpty(map));
780:            }
781:
782:            public void testIsEmptyWithNonEmptyMap() {
783:                Map map = new HashMap();
784:                map.put("item", "value");
785:                assertEquals(false, MapUtils.isEmpty(map));
786:            }
787:
788:            public void testIsEmptyWithNull() {
789:                Map map = null;
790:                assertEquals(true, MapUtils.isEmpty(map));
791:            }
792:
793:            public void testIsNotEmptyWithEmptyMap() {
794:                Map map = new HashMap();
795:                assertEquals(false, MapUtils.isNotEmpty(map));
796:            }
797:
798:            public void testIsNotEmptyWithNonEmptyMap() {
799:                Map map = new HashMap();
800:                map.put("item", "value");
801:                assertEquals(true, MapUtils.isNotEmpty(map));
802:            }
803:
804:            public void testIsNotEmptyWithNull() {
805:                Map map = null;
806:                assertEquals(false, MapUtils.isNotEmpty(map));
807:            }
808:
809:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.