commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MapUtils.java
Date Mon, 25 Aug 2003 20:38:40 GMT
scolebourne    2003/08/25 13:38:40

  Modified:    collections/src/test/org/apache/commons/collections
                        TestMapUtils.java
               collections/src/java/org/apache/commons/collections
                        MapUtils.java
  Log:
  Update to MapUtils debugPrint processing
  
  Revision  Changes    Path
  1.8       +72 -8     jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java
  
  Index: TestMapUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestMapUtils.java	24 Aug 2003 09:47:19 -0000	1.7
  +++ TestMapUtils.java	25 Aug 2003 20:38:40 -0000	1.8
  @@ -362,10 +362,12 @@
           final Map map = new TreeMap();  // treeMap guarantees order across JDKs for test
           map.put( new Integer(2) , "B" );
           map.put( new Integer(3) , "C" );
  +        map.put( new Integer(4) , null );
           
           outPrint.println("{");
           outPrint.println(INDENT + "2 = B");
           outPrint.println(INDENT + "3 = C");
  +        outPrint.println(INDENT + "4 = null");
           outPrint.println("}");
           final String EXPECTED_OUT = out.toString();
           out.reset();
  @@ -414,19 +416,81 @@
       public void testVerbosePrintNullStream() {
           try {
               MapUtils.verbosePrint(null, "Map", new HashMap());
  -            fail();
  -        } catch (NullPointerException ex) {
  +            fail("Should generate NullPointerException");
  +        } catch (NullPointerException expected) {
           }
       }
       
       public void testDebugPrintNullStream() {
           try {
               MapUtils.debugPrint(null, "Map", new HashMap());
  -            fail();
  -        } catch (NullPointerException ex) {
  +            fail("Should generate NullPointerException");
  +        } catch (NullPointerException expected) {
           }
       }
       
  +    public void testDebugPrintNullKey() {
  +        final ByteArrayOutputStream out = new ByteArrayOutputStream();
  +        final PrintStream outPrint = new PrintStream(out);
  +
  +        final String INDENT = "    ";
  +
  +        final Map map = new HashMap();
  +        map.put( null , "A" );
  +    
  +        outPrint.println("{");
  +        outPrint.println(INDENT + "null = A " + String.class.getName());
  +        outPrint.println("} " + HashMap.class.getName());
  +        final String EXPECTED_OUT = out.toString();
  +        out.reset();
  +        
  +        MapUtils.debugPrint(outPrint, null, map);
  +        assertEquals(EXPECTED_OUT, out.toString());
  +    }
  +
  +    public void testDebugPrintNullKeyToMap1() {
  +        final ByteArrayOutputStream out = new ByteArrayOutputStream();
  +        final PrintStream outPrint = new PrintStream(out);
  +
  +        final String INDENT = "    ";
  +
  +        final Map map = new HashMap();
  +        map.put( null , map );
  +    
  +        outPrint.println("{");
  +        outPrint.println(INDENT + "null = (this Map) " + HashMap.class.getName());
  +        outPrint.println("} " + HashMap.class.getName());
  +        final String EXPECTED_OUT = out.toString();
  +        out.reset();
  +        
  +        MapUtils.debugPrint(outPrint, null, map);
  +        assertEquals(EXPECTED_OUT, out.toString());
  +    }
  +
  +    public void testDebugPrintNullKeyToMap2() {
  +        final ByteArrayOutputStream out = new ByteArrayOutputStream();
  +        final PrintStream outPrint = new PrintStream(out);
  +
  +        final String INDENT = "    ";
  +
  +        final Map map = new HashMap();
  +        final Map map2= new HashMap();
  +        map.put( null , map2 );
  +        map2.put( "2", "B" );
  +    
  +        outPrint.println("{");
  +        outPrint.println(INDENT + "null = ");
  +        outPrint.println(INDENT + "{");
  +        outPrint.println(INDENT + INDENT + "2 = B " + String.class.getName());
  +        outPrint.println(INDENT + "} " + HashMap.class.getName());
  +        outPrint.println("} " + HashMap.class.getName());
  +        final String EXPECTED_OUT = out.toString();
  +        out.reset();
  +        
  +        MapUtils.debugPrint(outPrint, null, map);
  +        assertEquals(EXPECTED_OUT, out.toString());
  +    }
  +
       public void testVerbosePrint() {
           final ByteArrayOutputStream out = new ByteArrayOutputStream();
           final PrintStream outPrint = new PrintStream(out);
  @@ -442,7 +506,7 @@
           outPrint.println(INDENT + INDENT + "2 = B");
           outPrint.println(INDENT + INDENT + "3 = C");
           outPrint.println(INDENT + "}");
  -        outPrint.println(INDENT + "7 = this Map");
  +        outPrint.println(INDENT + "7 = (this Map)");
           outPrint.println("}");
           
           final String EXPECTED_OUT = out.toString();
  @@ -477,7 +541,7 @@
           outPrint.println(INDENT + INDENT + "2 = B " + String.class.getName());
           outPrint.println(INDENT + INDENT + "3 = C " + String.class.getName());
           outPrint.println(INDENT + "} " + TreeMap.class.getName());
  -        outPrint.println(INDENT + "7 = this Map");
  +        outPrint.println(INDENT + "7 = (this Map) " + TreeMap.class.getName());
           outPrint.println("} " + TreeMap.class.getName());
           
           final String EXPECTED_OUT = out.toString();
  
  
  
  1.28      +26 -33    jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java
  
  Index: MapUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- MapUtils.java	24 Aug 2003 09:47:19 -0000	1.27
  +++ MapUtils.java	25 Aug 2003 20:38:40 -0000	1.28
  @@ -689,9 +689,10 @@
        * @param out  the stream to print to, must not be null
        * @param label  the label to be applied to the output generated.  This 
        *              may well be the key associated with this map within a 
  -     *              surrounding map in which this is nested.   
  -     * @param map  the map to print, may be null
  -     * @throws NullPointerException if the stream is null
  +     *              surrounding map in which this is nested.  If the label is 
  +     *              <code>null</code> then no label is output.
  +     * @param map  the map to print, may be <code>null</code>
  +     * @throws NullPointerException if the stream is <code>null</code>
        */
       public static synchronized void verbosePrint(
           final PrintStream out,
  @@ -712,9 +713,10 @@
        * @param out  the stream to print to, must not be null
        * @param label  the label to be applied to the output generated.  This 
        *              may well be the key associated with this map within a 
  -     *              surrounding map in which this is nested.   
  -     * @param map  the map to print, may be null
  -     * @throws NullPointerException if the stream is null
  +     *              surrounding map in which this is nested.  If the label is 
  +     *              <code>null</code> then no label is output.
  +     * @param map  the map to print, may be <code>null</code>
  +     * @throws NullPointerException if the stream is <code>null</code>
        */
       public static synchronized void debugPrint(
           final PrintStream out,
  @@ -757,9 +759,11 @@
        * @param out  the stream to print to
        * @param label  the label to be applied to the output generated.  This 
        *              may well be the key associated with this map within a 
  -     *              surrounding map in which this is nested.   
  -     * @param map  the map to print, may be null
  +     *              surrounding map in which this is nested.  If the label is 
  +     *              <code>null</code>, then it is not output.
  +     * @param map  the map to print, may be <code>null</code>
        * @param debug flag indicating whether type names should be output.
  +     * @throws NullPointerException if the stream is <code>null</code>
        */
       private static void verbosePrintInternal(  // externally synchronized
           final PrintStream out,
  @@ -770,13 +774,9 @@
           printIndent(out);
   
           if (label != null) {
  -            if (map == null) {
  -                // Guard against null map.
  -                out.println(label + " = null");
  -                return;
  -            } else {
  -                out.println(label + " = ");
  -            }            
  +            out.print(label);
  +            out.print(" = ");
  +            out.println(map == null ? "null" : "");
           }
           if (map == null) {
               return;
  @@ -790,29 +790,22 @@
               Map.Entry entry = (Map.Entry) it.next();
               Object childKey = entry.getKey();
               Object childValue = entry.getValue();
  -            if (childValue instanceof Map) {
  -                if (childValue == map) {
  -                    printIndent(out);
  -                    out.println(childKey + " = this Map");  // should have stack really...
  -                } else {
  -                    verbosePrintInternal(out, childKey, (Map) childValue, debug);
  -                }
  +            if (childValue instanceof Map && childValue != map) {
  +                verbosePrintInternal(out, (childKey == null ? "null" : childKey), (Map)
childValue, debug);
               } else {
                   printIndent(out);
  -
  +                out.print(childKey);
  +                out.print(" = ");
  +                out.print(childValue == map ? "(this Map)" : childValue);
                   if (debug && childValue != null) {
  -                    out.println(
  -                        childKey 
  -                        + " = " 
  -                        + childValue 
  -                        + " " 
  -                        + childValue.getClass().getName()
  -                    );
  +                    out.print(' ');
  +                    out.println(childValue.getClass().getName());
                   } else {
  -                    out.println(childKey + " = " + childValue);
  +                    out.println();
                   }
               }
           }
  +        
           indentDepth--;
           printIndent(out);
           out.println(debug ? "} " + map.getClass().getName() : "}");
  
  
  

Mime
View raw message