📄 testmaputils.java
字号:
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();
MapUtils.verbosePrint(outPrint, null, map);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testDebugPrintNullLabel() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
final String INDENT = " ";
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 " + String.class.getName());
outPrint.println(INDENT + "3 = C " + String.class.getName());
outPrint.println(INDENT + "4 = null");
outPrint.println("} " + TreeMap.class.getName());
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.debugPrint(outPrint, null, map);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testVerbosePrintNullLabelAndMap() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
outPrint.println("null");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.verbosePrint(outPrint, null, null);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testDebugPrintNullLabelAndMap() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
outPrint.println("null");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.debugPrint(outPrint, null, null);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testVerbosePrintNullStream() {
try {
MapUtils.verbosePrint(null, "Map", new HashMap());
fail("Should generate NullPointerException");
} catch (NullPointerException expected) {
}
}
public void testDebugPrintNullStream() {
try {
MapUtils.debugPrint(null, "Map", new HashMap());
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 testVerbosePrintNullKey() {
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");
outPrint.println("}");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.verbosePrint(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 testVerbosePrintNullKeyToMap1() {
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)");
outPrint.println("}");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.verbosePrint(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 testVerbosePrintNullKeyToMap2() {
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");
outPrint.println(INDENT + "}");
outPrint.println("}");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.verbosePrint(outPrint, null, map);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testVerbosePrint() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
final String LABEL = "Print Map";
final String INDENT = " ";
outPrint.println(LABEL + " = ");
outPrint.println("{");
outPrint.println(INDENT + "0 = A");
outPrint.println(INDENT + "1 = ");
outPrint.println(INDENT + "{");
outPrint.println(INDENT + INDENT + "2 = B");
outPrint.println(INDENT + INDENT + "3 = C");
outPrint.println(INDENT + "}");
outPrint.println(INDENT + "7 = (this Map)");
outPrint.println("}");
final String EXPECTED_OUT = out.toString();
out.reset();
final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
inner.put( new Integer(2) , "B" );
inner.put( new Integer(3) , "C" );
final Map outer = new TreeMap();
outer.put( new Integer(1) , inner );
outer.put( new Integer(0) , "A");
outer.put( new Integer(7) , outer);
MapUtils.verbosePrint(outPrint, "Print Map", outer);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testDebugPrint() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
final String LABEL = "Print Map";
final String INDENT = " ";
outPrint.println(LABEL + " = ");
outPrint.println("{");
outPrint.println(INDENT + "0 = A " + String.class.getName());
outPrint.println(INDENT + "1 = ");
outPrint.println(INDENT + "{");
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) " + TreeMap.class.getName());
outPrint.println("} " + TreeMap.class.getName());
final String EXPECTED_OUT = out.toString();
out.reset();
final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
inner.put( new Integer(2) , "B" );
inner.put( new Integer(3) , "C" );
final Map outer = new TreeMap();
outer.put( new Integer(1) , inner );
outer.put( new Integer(0) , "A");
outer.put( new Integer(7) , outer);
MapUtils.debugPrint(outPrint, "Print Map", outer);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testVerbosePrintSelfReference() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
final String LABEL = "Print Map";
final String INDENT = " ";
final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
final Map father = new TreeMap();
final Map son = new TreeMap();
grandfather.put( new Integer(0), "A" );
grandfather.put( new Integer(1), father );
father.put( new Integer(2), "B" );
father.put( new Integer(3), grandfather);
father.put( new Integer(4), son);
son.put( new Integer(5), "C");
son.put( new Integer(6), grandfather);
son.put( new Integer(7), father);
outPrint.println(LABEL + " = ");
outPrint.println("{");
outPrint.println(INDENT + "0 = A");
outPrint.println(INDENT + "1 = ");
outPrint.println(INDENT + "{");
outPrint.println(INDENT + INDENT + "2 = B");
outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map)");
outPrint.println(INDENT + INDENT + "4 = ");
outPrint.println(INDENT + INDENT + "{");
outPrint.println(INDENT + INDENT + INDENT + "5 = C");
outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map)");
outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map)");
outPrint.println(INDENT + INDENT + "}");
outPrint.println(INDENT + "}");
outPrint.println("}");
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.verbosePrint(outPrint, "Print Map", grandfather);
assertEquals(EXPECTED_OUT, out.toString());
}
public void testDebugPrintSelfReference() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
final String LABEL = "Print Map";
final String INDENT = " ";
final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
final Map father = new TreeMap();
final Map son = new TreeMap();
grandfather.put( new Integer(0), "A" );
grandfather.put( new Integer(1), father );
father.put( new Integer(2), "B" );
father.put( new Integer(3), grandfather);
father.put( new Integer(4), son);
son.put( new Integer(5), "C");
son.put( new Integer(6), grandfather);
son.put( new Integer(7), father);
outPrint.println(LABEL + " = ");
outPrint.println("{");
outPrint.println(INDENT + "0 = A " + String.class.getName());
outPrint.println(INDENT + "1 = ");
outPrint.println(INDENT + "{");
outPrint.println(INDENT + INDENT + "2 = B " + String.class.getName());
outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map) " + TreeMap.class.getName());
outPrint.println(INDENT + INDENT + "4 = ");
outPrint.println(INDENT + INDENT + "{");
outPrint.println(INDENT + INDENT + INDENT + "5 = C " + String.class.getName());
outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map) " + TreeMap.class.getName());
outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map) " + TreeMap.class.getName());
outPrint.println(INDENT + INDENT + "} " + TreeMap.class.getName());
outPrint.println(INDENT + "} " + TreeMap.class.getName());
outPrint.println("} " + TreeMap.class.getName());
final String EXPECTED_OUT = out.toString();
out.reset();
MapUtils.debugPrint(outPrint, "Print Map", grandfather);
assertEquals(EXPECTED_OUT, out.toString());
}
//-----------------------------------------------------------------------
public void testIsEmptyWithEmptyMap() {
Map map = new HashMap();
assertEquals(true, MapUtils.isEmpty(map));
}
public void testIsEmptyWithNonEmptyMap() {
Map map = new HashMap();
map.put("item", "value");
assertEquals(false, MapUtils.isEmpty(map));
}
public void testIsEmptyWithNull() {
Map map = null;
assertEquals(true, MapUtils.isEmpty(map));
}
public void testIsNotEmptyWithEmptyMap() {
Map map = new HashMap();
assertEquals(false, MapUtils.isNotEmpty(map));
}
public void testIsNotEmptyWithNonEmptyMap() {
Map map = new HashMap();
map.put("item", "value");
assertEquals(true, MapUtils.isNotEmpty(map));
}
public void testIsNotEmptyWithNull() {
Map map = null;
assertEquals(false, MapUtils.isNotEmpty(map));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -