Update to MapUtils debugPrint processing

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131117 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-25 20:38:40 +00:00
parent bd5708f488
commit 858f6564be
2 changed files with 98 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.27 2003/08/24 09:47:19 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.28 2003/08/25 20:38:40 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -105,7 +105,7 @@ import org.apache.commons.collections.decorators.TypedSortedMap;
* </ul>
*
* @since Commons Collections 1.0
* @version $Revision: 1.27 $ $Date: 2003/08/24 09:47:19 $
* @version $Revision: 1.28 $ $Date: 2003/08/25 20:38:40 $
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
@ -689,9 +689,10 @@ public class MapUtils {
* @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 @@ public class MapUtils {
* @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 @@ public class MapUtils {
* @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 @@ public class MapUtils {
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 @@ public class MapUtils {
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() : "}");

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.7 2003/08/24 09:47:19 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.8 2003/08/25 20:38:40 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -73,7 +73,7 @@ import junit.framework.Test;
/**
* Tests for MapUtils.
*
* @version $Revision: 1.7 $ $Date: 2003/08/24 09:47:19 $
* @version $Revision: 1.8 $ $Date: 2003/08/25 20:38:40 $
*
* @author Stephen Colebourne
* @author Arun Mammen Thomas
@ -362,10 +362,12 @@ public class TestMapUtils extends BulkTest {
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 class TestMapUtils extends BulkTest {
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 @@ public class TestMapUtils extends BulkTest {
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 @@ public class TestMapUtils extends BulkTest {
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();