KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestMapUtils


1 /*
2  * Copyright 2001-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.commons.collections;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ListResourceBundle JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 import junit.framework.Test;
30
31 import org.apache.commons.collections.map.LazyMap;
32 import org.apache.commons.collections.map.PredicatedMap;
33 import org.apache.commons.collections.map.TestPredicatedMap;
34
35 /**
36  * Tests for MapUtils.
37  *
38  * @version $Revision: 1.23 $ $Date: 2004/04/09 14:55:39 $
39  *
40  * @author Stephen Colebourne
41  * @author Arun Mammen Thomas
42  * @author Max Rydahl Andersen
43  * @author Janek Bogucki
44  */

45 public class TestMapUtils extends BulkTest {
46
47     public TestMapUtils(String JavaDoc name) {
48         super(name);
49     }
50
51
52     public static Test suite() {
53         return BulkTest.makeSuite(TestMapUtils.class);
54     }
55
56     public Predicate getPredicate() {
57         return new Predicate() {
58             public boolean evaluate(Object JavaDoc o) {
59                 return o instanceof String JavaDoc;
60             }
61         };
62     }
63
64     public void testPredicatedMap() {
65         Predicate p = getPredicate();
66         Map JavaDoc map = MapUtils.predicatedMap(new HashMap JavaDoc(), p, p);
67         assertTrue("returned object should be a PredicatedMap",
68             map instanceof PredicatedMap);
69         try {
70             map = MapUtils.predicatedMap(null, p, p);
71             fail("Expecting IllegalArgumentException for null map.");
72         } catch (IllegalArgumentException JavaDoc e) {
73             // expected
74
}
75     }
76
77     // Since a typed map is a predicated map, I copied the tests for predicated map
78
public void testTypedMapIllegalPut() {
79         final Map JavaDoc map = MapUtils.typedMap(new HashMap JavaDoc(), String JavaDoc.class, String JavaDoc.class);
80         
81         try {
82             map.put("Hi", new Integer JavaDoc(3));
83             fail("Illegal value should raise IllegalArgument");
84         } catch (IllegalArgumentException JavaDoc e) {
85             // expected
86
}
87
88         try {
89             map.put(new Integer JavaDoc(3), "Hi");
90             fail("Illegal key should raise IllegalArgument");
91         } catch (IllegalArgumentException JavaDoc e) {
92             // expected
93
}
94
95         assertTrue(!map.containsKey(new Integer JavaDoc(3)));
96         assertTrue(!map.containsValue(new Integer JavaDoc(3)));
97
98         Map JavaDoc map2 = new HashMap JavaDoc();
99         map2.put("A", "a");
100         map2.put("B", "b");
101         map2.put("C", "c");
102         map2.put("c", new Integer JavaDoc(3));
103
104         try {
105             map.putAll(map2);
106             fail("Illegal value should raise IllegalArgument");
107         } catch (IllegalArgumentException JavaDoc e) {
108             // expected
109
}
110
111         map.put("E", "e");
112         Iterator JavaDoc iterator = map.entrySet().iterator();
113         try {
114             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
115             entry.setValue(new Integer JavaDoc(3));
116             fail("Illegal value should raise IllegalArgument");
117         } catch (IllegalArgumentException JavaDoc e) {
118             // expected
119
}
120     
121     }
122     
123     public BulkTest bulkTestTypedMap() {
124         return new TestPredicatedMap("") {
125             public boolean isAllowNullKey() {
126                 return false;
127             }
128
129             public boolean isAllowNullValue() {
130                 return false;
131             }
132
133             public Map JavaDoc makeEmptyMap() {
134                 return MapUtils.typedMap(new HashMap JavaDoc(), String JavaDoc.class, String JavaDoc.class);
135             }
136         };
137     }
138
139     public void testLazyMapFactory() {
140         Factory factory = FactoryUtils.constantFactory(new Integer JavaDoc(5));
141         Map JavaDoc map = MapUtils.lazyMap(new HashMap JavaDoc(), factory);
142         assertTrue(map instanceof LazyMap);
143         try {
144             map = MapUtils.lazyMap(new HashMap JavaDoc(), (Factory) null);
145             fail("Expecting IllegalArgumentException for null factory");
146         } catch (IllegalArgumentException JavaDoc e) {
147             // expected
148
}
149         try {
150             map = MapUtils.lazyMap(null, factory);
151             fail("Expecting IllegalArgumentException for null map");
152         } catch (IllegalArgumentException JavaDoc e) {
153             // expected
154
}
155         Transformer transformer = TransformerUtils.asTransformer(factory);
156         map = MapUtils.lazyMap(new HashMap JavaDoc(), transformer);
157         assertTrue(map instanceof LazyMap);
158          try {
159             map = MapUtils.lazyMap(new HashMap JavaDoc(), (Transformer) null);
160             fail("Expecting IllegalArgumentException for null transformer");
161         } catch (IllegalArgumentException JavaDoc e) {
162             // expected
163
}
164         try {
165             map = MapUtils.lazyMap(null, transformer);
166             fail("Expecting IllegalArgumentException for null map");
167         } catch (IllegalArgumentException JavaDoc e) {
168             // expected
169
}
170     }
171
172     public void testLazyMapTransformer() {
173         Map JavaDoc map = MapUtils.lazyMap(new HashMap JavaDoc(), new Transformer() {
174             public Object JavaDoc transform(Object JavaDoc mapKey) {
175                 if (mapKey instanceof String JavaDoc) {
176                     return new Integer JavaDoc((String JavaDoc) mapKey);
177                 }
178                 return null;
179             }
180         });
181
182         assertEquals(0, map.size());
183         Integer JavaDoc i1 = (Integer JavaDoc) map.get("5");
184         assertEquals(new Integer JavaDoc(5), i1);
185         assertEquals(1, map.size());
186         Integer JavaDoc i2 = (Integer JavaDoc) map.get(new String JavaDoc(new char[] {'5'}));
187         assertEquals(new Integer JavaDoc(5), i2);
188         assertEquals(1, map.size());
189         assertSame(i1, i2);
190     }
191
192     public void testInvertMap() {
193         final Map JavaDoc in = new HashMap JavaDoc( 5 , 1 );
194         in.put( "1" , "A" );
195         in.put( "2" , "B" );
196         in.put( "3" , "C" );
197         in.put( "4" , "D" );
198         in.put( "5" , "E" );
199     
200         final Set JavaDoc inKeySet = new HashSet JavaDoc( in.keySet() );
201         final Set JavaDoc inValSet = new HashSet JavaDoc( in.values() );
202         
203         final Map JavaDoc out = MapUtils.invertMap(in);
204
205         final Set JavaDoc outKeySet = new HashSet JavaDoc( out.keySet() );
206         final Set JavaDoc outValSet = new HashSet JavaDoc( out.values() );
207         
208         assertTrue( inKeySet.equals( outValSet ));
209         assertTrue( inValSet.equals( outKeySet ));
210         
211         assertEquals( out.get("A"), "1" );
212         assertEquals( out.get("B"), "2" );
213         assertEquals( out.get("C"), "3" );
214         assertEquals( out.get("D"), "4" );
215         assertEquals( out.get("E"), "5" );
216     }
217                 
218     public void testConvertResourceBundle() {
219         final Map JavaDoc in = new HashMap JavaDoc( 5 , 1 );
220         in.put( "1" , "A" );
221         in.put( "2" , "B" );
222         in.put( "3" , "C" );
223         in.put( "4" , "D" );
224         in.put( "5" , "E" );
225     
226         ResourceBundle JavaDoc b = new ListResourceBundle JavaDoc() {
227             public Object JavaDoc[][] getContents() {
228                 final Object JavaDoc[][] contents = new Object JavaDoc[ in.size() ][2];
229                 final Iterator JavaDoc i = in.keySet().iterator();
230                 int n = 0;
231                 while ( i.hasNext() ) {
232                     final Object JavaDoc key = i.next();
233                     final Object JavaDoc val = in.get( key );
234                     contents[ n ][ 0 ] = key;
235                     contents[ n ][ 1 ] = val;
236                     ++n;
237                 }
238                 return contents;
239             }
240         };
241         
242         final Map JavaDoc out = MapUtils.toMap(b);
243
244         assertTrue( in.equals(out));
245     }
246
247     public void testDebugAndVerbosePrintCasting() {
248         final Map JavaDoc inner = new HashMap JavaDoc(2, 1);
249         inner.put( new Integer JavaDoc(2) , "B" );
250         inner.put( new Integer JavaDoc(3) , "C" );
251
252         final Map JavaDoc outer = new HashMap JavaDoc(2, 1);
253         outer.put( new Integer JavaDoc(0) , inner );
254         outer.put( new Integer JavaDoc(1) , "A");
255
256  
257         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
258         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
259         
260         try {
261             MapUtils.debugPrint(outPrint, "Print Map", outer);
262         } catch (final ClassCastException JavaDoc e) {
263             fail("No Casting should be occurring!");
264         }
265     }
266
267     public void testDebugAndVerbosePrintNullMap() {
268         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
269         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
270
271         final String JavaDoc LABEL = "Print Map";
272         outPrint.println(LABEL + " = " + String.valueOf((Object JavaDoc) null));
273         final String JavaDoc EXPECTED_OUT = out.toString();
274
275         out.reset();
276
277         MapUtils.debugPrint(outPrint, LABEL, null);
278         assertEquals(EXPECTED_OUT, out.toString());
279         
280         out.reset();
281         
282         MapUtils.verbosePrint(outPrint, LABEL, null);
283         assertEquals(EXPECTED_OUT, out.toString());
284     }
285
286     public void testVerbosePrintNullLabel() {
287         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
288         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
289
290         final String JavaDoc INDENT = " ";
291
292         final Map JavaDoc map = new TreeMap JavaDoc(); // treeMap guarantees order across JDKs for test
293
map.put( new Integer JavaDoc(2) , "B" );
294         map.put( new Integer JavaDoc(3) , "C" );
295         map.put( new Integer JavaDoc(4) , null );
296         
297         outPrint.println("{");
298         outPrint.println(INDENT + "2 = B");
299         outPrint.println(INDENT + "3 = C");
300         outPrint.println(INDENT + "4 = null");
301         outPrint.println("}");
302         final String JavaDoc EXPECTED_OUT = out.toString();
303         out.reset();
304
305         MapUtils.verbosePrint(outPrint, null, map);
306         assertEquals(EXPECTED_OUT, out.toString());
307     }
308             
309     public void testDebugPrintNullLabel() {
310         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
311         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
312
313         final String JavaDoc INDENT = " ";
314
315         final Map JavaDoc map = new TreeMap JavaDoc(); // treeMap guarantees order across JDKs for test
316
map.put( new Integer JavaDoc(2) , "B" );
317         map.put( new Integer JavaDoc(3) , "C" );
318         map.put( new Integer JavaDoc(4) , null );
319     
320         outPrint.println("{");
321         outPrint.println(INDENT + "2 = B " + String JavaDoc.class.getName());
322         outPrint.println(INDENT + "3 = C " + String JavaDoc.class.getName());
323         outPrint.println(INDENT + "4 = null");
324         outPrint.println("} " + TreeMap JavaDoc.class.getName());
325         final String JavaDoc EXPECTED_OUT = out.toString();
326         out.reset();
327         
328         MapUtils.debugPrint(outPrint, null, map);
329         assertEquals(EXPECTED_OUT, out.toString());
330     }
331
332     public void testVerbosePrintNullLabelAndMap() {
333         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
334         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
335         
336         outPrint.println("null");
337         final String JavaDoc EXPECTED_OUT = out.toString();
338         out.reset();
339
340         MapUtils.verbosePrint(outPrint, null, null);
341         assertEquals(EXPECTED_OUT, out.toString());
342     }
343
344     public void testDebugPrintNullLabelAndMap() {
345         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
346         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
347
348         outPrint.println("null");
349         final String JavaDoc EXPECTED_OUT = out.toString();
350         out.reset();
351
352         MapUtils.debugPrint(outPrint, null, null);
353         assertEquals(EXPECTED_OUT, out.toString());
354     }
355
356     public void testVerbosePrintNullStream() {
357         try {
358             MapUtils.verbosePrint(null, "Map", new HashMap JavaDoc());
359             fail("Should generate NullPointerException");
360         } catch (NullPointerException JavaDoc expected) {
361         }
362     }
363     
364     public void testDebugPrintNullStream() {
365         try {
366             MapUtils.debugPrint(null, "Map", new HashMap JavaDoc());
367             fail("Should generate NullPointerException");
368         } catch (NullPointerException JavaDoc expected) {
369         }
370     }
371     
372     public void testDebugPrintNullKey() {
373         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
374         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
375
376         final String JavaDoc INDENT = " ";
377
378         final Map JavaDoc map = new HashMap JavaDoc();
379         map.put( null , "A" );
380     
381         outPrint.println("{");
382         outPrint.println(INDENT + "null = A " + String JavaDoc.class.getName());
383         outPrint.println("} " + HashMap JavaDoc.class.getName());
384         final String JavaDoc EXPECTED_OUT = out.toString();
385         out.reset();
386         
387         MapUtils.debugPrint(outPrint, null, map);
388         assertEquals(EXPECTED_OUT, out.toString());
389     }
390
391     public void testVerbosePrintNullKey() {
392         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
393         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
394
395         final String JavaDoc INDENT = " ";
396
397         final Map JavaDoc map = new HashMap JavaDoc();
398         map.put( null , "A" );
399     
400         outPrint.println("{");
401         outPrint.println(INDENT + "null = A");
402         outPrint.println("}");
403         final String JavaDoc EXPECTED_OUT = out.toString();
404         out.reset();
405         
406         MapUtils.verbosePrint(outPrint, null, map);
407         assertEquals(EXPECTED_OUT, out.toString());
408     }
409
410     public void testDebugPrintNullKeyToMap1() {
411         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
412         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
413
414         final String JavaDoc INDENT = " ";
415
416         final Map JavaDoc map = new HashMap JavaDoc();
417         map.put( null , map );
418     
419         outPrint.println("{");
420         outPrint.println(INDENT + "null = (this Map) " + HashMap JavaDoc.class.getName());
421         outPrint.println("} " + HashMap JavaDoc.class.getName());
422         final String JavaDoc EXPECTED_OUT = out.toString();
423         out.reset();
424         
425         MapUtils.debugPrint(outPrint, null, map);
426         assertEquals(EXPECTED_OUT, out.toString());
427     }
428
429     public void testVerbosePrintNullKeyToMap1() {
430         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
431         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
432
433         final String JavaDoc INDENT = " ";
434
435         final Map JavaDoc map = new HashMap JavaDoc();
436         map.put( null , map );
437     
438         outPrint.println("{");
439         outPrint.println(INDENT + "null = (this Map)");
440         outPrint.println("}");
441         final String JavaDoc EXPECTED_OUT = out.toString();
442         out.reset();
443         
444         MapUtils.verbosePrint(outPrint, null, map);
445         assertEquals(EXPECTED_OUT, out.toString());
446     }
447
448     public void testDebugPrintNullKeyToMap2() {
449         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
450         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
451
452         final String JavaDoc INDENT = " ";
453
454         final Map JavaDoc map = new HashMap JavaDoc();
455         final Map JavaDoc map2= new HashMap JavaDoc();
456         map.put( null , map2 );
457         map2.put( "2", "B" );
458     
459         outPrint.println("{");
460         outPrint.println(INDENT + "null = ");
461         outPrint.println(INDENT + "{");
462         outPrint.println(INDENT + INDENT + "2 = B " + String JavaDoc.class.getName());
463         outPrint.println(INDENT + "} " + HashMap JavaDoc.class.getName());
464         outPrint.println("} " + HashMap JavaDoc.class.getName());
465         final String JavaDoc EXPECTED_OUT = out.toString();
466         out.reset();
467         
468         MapUtils.debugPrint(outPrint, null, map);
469         assertEquals(EXPECTED_OUT, out.toString());
470     }
471
472     public void testVerbosePrintNullKeyToMap2() {
473         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
474         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
475
476         final String JavaDoc INDENT = " ";
477
478         final Map JavaDoc map = new HashMap JavaDoc();
479         final Map JavaDoc map2= new HashMap JavaDoc();
480         map.put( null , map2 );
481         map2.put( "2", "B" );
482     
483         outPrint.println("{");
484         outPrint.println(INDENT + "null = ");
485         outPrint.println(INDENT + "{");
486         outPrint.println(INDENT + INDENT + "2 = B");
487         outPrint.println(INDENT + "}");
488         outPrint.println("}");
489         final String JavaDoc EXPECTED_OUT = out.toString();
490         out.reset();
491         
492         MapUtils.verbosePrint(outPrint, null, map);
493         assertEquals(EXPECTED_OUT, out.toString());
494     }
495
496     public void testVerbosePrint() {
497         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
498         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
499         
500         final String JavaDoc LABEL = "Print Map";
501         final String JavaDoc INDENT = " ";
502         
503         outPrint.println(LABEL + " = ");
504         outPrint.println("{");
505         outPrint.println(INDENT + "0 = A");
506         outPrint.println(INDENT + "1 = ");
507         outPrint.println(INDENT + "{");
508         outPrint.println(INDENT + INDENT + "2 = B");
509         outPrint.println(INDENT + INDENT + "3 = C");
510         outPrint.println(INDENT + "}");
511         outPrint.println(INDENT + "7 = (this Map)");
512         outPrint.println("}");
513         
514         final String JavaDoc EXPECTED_OUT = out.toString();
515
516         out.reset();
517
518         final Map JavaDoc inner = new TreeMap JavaDoc(); // treeMap guarantees order across JDKs for test
519
inner.put( new Integer JavaDoc(2) , "B" );
520         inner.put( new Integer JavaDoc(3) , "C" );
521
522         final Map JavaDoc outer = new TreeMap JavaDoc();
523         outer.put( new Integer JavaDoc(1) , inner );
524         outer.put( new Integer JavaDoc(0) , "A");
525         outer.put( new Integer JavaDoc(7) , outer);
526
527         MapUtils.verbosePrint(outPrint, "Print Map", outer);
528         assertEquals(EXPECTED_OUT, out.toString());
529     }
530
531     public void testDebugPrint() {
532         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
533         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
534         
535         final String JavaDoc LABEL = "Print Map";
536         final String JavaDoc INDENT = " ";
537         
538         outPrint.println(LABEL + " = ");
539         outPrint.println("{");
540         outPrint.println(INDENT + "0 = A " + String JavaDoc.class.getName());
541         outPrint.println(INDENT + "1 = ");
542         outPrint.println(INDENT + "{");
543         outPrint.println(INDENT + INDENT + "2 = B " + String JavaDoc.class.getName());
544         outPrint.println(INDENT + INDENT + "3 = C " + String JavaDoc.class.getName());
545         outPrint.println(INDENT + "} " + TreeMap JavaDoc.class.getName());
546         outPrint.println(INDENT + "7 = (this Map) " + TreeMap JavaDoc.class.getName());
547         outPrint.println("} " + TreeMap JavaDoc.class.getName());
548         
549         final String JavaDoc EXPECTED_OUT = out.toString();
550
551         out.reset();
552
553         final Map JavaDoc inner = new TreeMap JavaDoc(); // treeMap guarantees order across JDKs for test
554
inner.put( new Integer JavaDoc(2) , "B" );
555         inner.put( new Integer JavaDoc(3) , "C" );
556
557         final Map JavaDoc outer = new TreeMap JavaDoc();
558         outer.put( new Integer JavaDoc(1) , inner );
559         outer.put( new Integer JavaDoc(0) , "A");
560         outer.put( new Integer JavaDoc(7) , outer);
561
562         MapUtils.debugPrint(outPrint, "Print Map", outer);
563         assertEquals(EXPECTED_OUT, out.toString());
564     }
565
566     public void testVerbosePrintSelfReference() {
567         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
568         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
569
570         final String JavaDoc LABEL = "Print Map";
571         final String JavaDoc INDENT = " ";
572         
573
574         final Map JavaDoc grandfather = new TreeMap JavaDoc();// treeMap guarantees order across JDKs for test
575
final Map JavaDoc father = new TreeMap JavaDoc();
576         final Map JavaDoc son = new TreeMap JavaDoc();
577         
578         grandfather.put( new Integer JavaDoc(0), "A" );
579         grandfather.put( new Integer JavaDoc(1), father );
580         
581         father.put( new Integer JavaDoc(2), "B" );
582         father.put( new Integer JavaDoc(3), grandfather);
583         father.put( new Integer JavaDoc(4), son);
584         
585         son.put( new Integer JavaDoc(5), "C");
586         son.put( new Integer JavaDoc(6), grandfather);
587         son.put( new Integer JavaDoc(7), father);
588         
589         outPrint.println(LABEL + " = ");
590         outPrint.println("{");
591         outPrint.println(INDENT + "0 = A");
592         outPrint.println(INDENT + "1 = ");
593         outPrint.println(INDENT + "{");
594         outPrint.println(INDENT + INDENT + "2 = B");
595         outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map)");
596         outPrint.println(INDENT + INDENT + "4 = ");
597         outPrint.println(INDENT + INDENT + "{");
598         outPrint.println(INDENT + INDENT + INDENT + "5 = C");
599         outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map)");
600         outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map)");
601         outPrint.println(INDENT + INDENT + "}");
602         outPrint.println(INDENT + "}");
603         outPrint.println("}");
604
605         final String JavaDoc EXPECTED_OUT = out.toString();
606
607         out.reset();
608         MapUtils.verbosePrint(outPrint, "Print Map", grandfather);
609
610         assertEquals(EXPECTED_OUT, out.toString());
611     }
612     
613     public void testDebugPrintSelfReference() {
614         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
615         final PrintStream JavaDoc outPrint = new PrintStream JavaDoc(out);
616     
617         final String JavaDoc LABEL = "Print Map";
618         final String JavaDoc INDENT = " ";
619         
620     
621         final Map JavaDoc grandfather = new TreeMap JavaDoc();// treeMap guarantees order across JDKs for test
622
final Map JavaDoc father = new TreeMap JavaDoc();
623         final Map JavaDoc son = new TreeMap JavaDoc();
624         
625         grandfather.put( new Integer JavaDoc(0), "A" );
626         grandfather.put( new Integer JavaDoc(1), father );
627         
628         father.put( new Integer JavaDoc(2), "B" );
629         father.put( new Integer JavaDoc(3), grandfather);
630         father.put( new Integer JavaDoc(4), son);
631         
632         son.put( new Integer JavaDoc(5), "C");
633         son.put( new Integer JavaDoc(6), grandfather);
634         son.put( new Integer JavaDoc(7), father);
635         
636         outPrint.println(LABEL + " = ");
637         outPrint.println("{");
638         outPrint.println(INDENT + "0 = A " + String JavaDoc.class.getName());
639         outPrint.println(INDENT + "1 = ");
640         outPrint.println(INDENT + "{");
641         outPrint.println(INDENT + INDENT + "2 = B " + String JavaDoc.class.getName());
642         outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map) " + TreeMap JavaDoc.class.getName());
643         outPrint.println(INDENT + INDENT + "4 = ");
644         outPrint.println(INDENT + INDENT + "{");
645         outPrint.println(INDENT + INDENT + INDENT + "5 = C " + String JavaDoc.class.getName());
646         outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map) " + TreeMap JavaDoc.class.getName());
647         outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map) " + TreeMap JavaDoc.class.getName());
648         outPrint.println(INDENT + INDENT + "} " + TreeMap JavaDoc.class.getName());
649         outPrint.println(INDENT + "} " + TreeMap JavaDoc.class.getName());
650         outPrint.println("} " + TreeMap JavaDoc.class.getName());
651     
652         final String JavaDoc EXPECTED_OUT = out.toString();
653     
654         out.reset();
655         MapUtils.debugPrint(outPrint, "Print Map", grandfather);
656     
657         assertEquals(EXPECTED_OUT, out.toString());
658     }
659     
660 }
661
Popular Tags