1 16 package org.apache.commons.lang.builder; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 32 public class DefaultToStringStyleTest extends TestCase { 33 34 private final Integer base = new Integer (5); 35 private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base)); 36 37 public DefaultToStringStyleTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(suite()); 43 } 44 45 public static Test suite() { 46 TestSuite suite = new TestSuite(DefaultToStringStyleTest.class); 47 suite.setName("DefaultToStringStyle Tests"); 48 return suite; 49 } 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); 54 } 55 56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); 59 } 60 61 63 public void testBlank() { 64 assertEquals(baseStr + "[]", new ToStringBuilder(base).toString()); 65 } 66 67 public void testAppendSuper() { 68 assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString()); 69 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString()); 70 71 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString()); 72 assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString()); 73 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString()); 74 } 75 76 public void testObject() { 77 Integer i3 = new Integer (3); 78 Integer i4 = new Integer (4); 79 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) null).toString()); 80 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString()); 81 assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object ) null).toString()); 82 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", i3).toString()); 83 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); 84 assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString()); 85 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new ArrayList (), false).toString()); 86 assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList (), true).toString()); 87 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap (), false).toString()); 88 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap (), true).toString()); 89 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object ) new String [0], false).toString()); 90 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object ) new String [0], true).toString()); 91 } 92 93 public void testLong() { 94 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); 95 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); 96 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString()); 97 } 98 99 public void testObjectArray() { 100 Object [] array = new Object [] {null, base, new int[] {3, 6}}; 101 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString()); 102 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object ) array).toString()); 103 array = null; 104 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 105 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 106 } 107 108 public void testLongArray() { 109 long[] array = new long[] {1, 2, -3, 4}; 110 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 111 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 112 array = null; 113 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 114 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 115 } 116 117 public void testLongArrayArray() { 118 long[][] array = new long[][] {{1, 2}, null, {5}}; 119 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 120 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 121 array = null; 122 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 123 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 124 } 125 126 } 127 | Popular Tags |