1 16 package org.apache.commons.lang.builder; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 21 import junit.framework.TestCase; 22 23 29 public class ShortPrefixToStringStyleTest extends TestCase { 30 31 private final Integer base = new Integer (5); 32 private final String baseStr = "Integer"; 33 34 protected void setUp() throws Exception { 35 super.setUp(); 36 ToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE); 37 } 38 39 protected void tearDown() throws Exception { 40 super.tearDown(); 41 ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); 42 } 43 44 46 public void testBlank() { 47 assertEquals(baseStr + "[]", new ToStringBuilder(base).toString()); 48 } 49 50 public void testAppendSuper() { 51 assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString()); 52 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString()); 53 54 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString()); 55 assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString()); 56 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString()); 57 } 58 59 public void testObject() { 60 Integer i3 = new Integer (3); 61 Integer i4 = new Integer (4); 62 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) null).toString()); 63 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString()); 64 assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object ) null).toString()); 65 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", i3).toString()); 66 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); 67 assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString()); 68 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new ArrayList (), false).toString()); 69 assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList (), true).toString()); 70 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap (), false).toString()); 71 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap (), true).toString()); 72 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object ) new String [0], false).toString()); 73 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object ) new String [0], true).toString()); 74 } 75 76 public void testLong() { 77 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); 78 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); 79 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString()); 80 } 81 82 public void testObjectArray() { 83 Object [] array = new Object [] {null, base, new int[] {3, 6}}; 84 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString()); 85 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object ) array).toString()); 86 array = null; 87 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 88 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 89 } 90 91 public void testLongArray() { 92 long[] array = new long[] {1, 2, -3, 4}; 93 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 94 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 95 array = null; 96 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 97 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 98 } 99 100 public void testLongArrayArray() { 101 long[][] array = new long[][] {{1, 2}, null, {5}}; 102 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 103 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 104 array = null; 105 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 106 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 107 } 108 109 } 110 | Popular Tags |