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 SimpleToStringStyleTest extends TestCase { 33 34 private final Integer base = new Integer (5); 35 36 public SimpleToStringStyleTest(String name) { 37 super(name); 38 } 39 40 public static void main(String [] args) { 41 TestRunner.run(suite()); 42 } 43 44 public static Test suite() { 45 TestSuite suite = new TestSuite(SimpleToStringStyleTest.class); 46 suite.setName("SimpleToStringStyle Tests"); 47 return suite; 48 } 49 50 protected void setUp() throws Exception { 51 super.setUp(); 52 ToStringBuilder.setDefaultStyle(ToStringStyle.SIMPLE_STYLE); 53 } 54 55 protected void tearDown() throws Exception { 56 super.tearDown(); 57 ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); 58 } 59 60 62 public void testBlank() { 63 assertEquals("", new ToStringBuilder(base).toString()); 64 } 65 66 public void testAppendSuper() { 67 assertEquals("", new ToStringBuilder(base).appendSuper("").toString()); 68 assertEquals("<null>", new ToStringBuilder(base).appendSuper("<null>").toString()); 69 70 assertEquals("hello", new ToStringBuilder(base).appendSuper("").append("a", "hello").toString()); 71 assertEquals("<null>,hello", new ToStringBuilder(base).appendSuper("<null>").append("a", "hello").toString()); 72 assertEquals("hello", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString()); 73 } 74 75 public void testObject() { 76 Integer i3 = new Integer (3); 77 Integer i4 = new Integer (4); 78 assertEquals("<null>", new ToStringBuilder(base).append((Object ) null).toString()); 79 assertEquals("3", new ToStringBuilder(base).append(i3).toString()); 80 assertEquals("<null>", new ToStringBuilder(base).append("a", (Object ) null).toString()); 81 assertEquals("3", new ToStringBuilder(base).append("a", i3).toString()); 82 assertEquals("3,4", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); 83 assertEquals("<Integer>", new ToStringBuilder(base).append("a", i3, false).toString()); 84 assertEquals("<size=0>", new ToStringBuilder(base).append("a", new ArrayList (), false).toString()); 85 assertEquals("[]", new ToStringBuilder(base).append("a", new ArrayList (), true).toString()); 86 assertEquals("<size=0>", new ToStringBuilder(base).append("a", new HashMap (), false).toString()); 87 assertEquals("{}", new ToStringBuilder(base).append("a", new HashMap (), true).toString()); 88 assertEquals("<size=0>", new ToStringBuilder(base).append("a", (Object ) new String [0], false).toString()); 89 assertEquals("{}", new ToStringBuilder(base).append("a", (Object ) new String [0], true).toString()); 90 } 91 92 public void testLong() { 93 assertEquals("3", new ToStringBuilder(base).append(3L).toString()); 94 assertEquals("3", new ToStringBuilder(base).append("a", 3L).toString()); 95 assertEquals("3,4", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString()); 96 } 97 98 public void testObjectArray() { 99 Object [] array = new Object [] {null, base, new int[] {3, 6}}; 100 assertEquals("{<null>,5,{3,6}}", new ToStringBuilder(base).append(array).toString()); 101 assertEquals("{<null>,5,{3,6}}", new ToStringBuilder(base).append((Object ) array).toString()); 102 array = null; 103 assertEquals("<null>", new ToStringBuilder(base).append(array).toString()); 104 assertEquals("<null>", new ToStringBuilder(base).append((Object ) array).toString()); 105 } 106 107 public void testLongArray() { 108 long[] array = new long[] {1, 2, -3, 4}; 109 assertEquals("{1,2,-3,4}", new ToStringBuilder(base).append(array).toString()); 110 assertEquals("{1,2,-3,4}", new ToStringBuilder(base).append((Object ) array).toString()); 111 array = null; 112 assertEquals("<null>", new ToStringBuilder(base).append(array).toString()); 113 assertEquals("<null>", new ToStringBuilder(base).append((Object ) array).toString()); 114 } 115 116 public void testLongArrayArray() { 117 long[][] array = new long[][] {{1, 2}, null, {5}}; 118 assertEquals("{{1,2},<null>,{5}}", new ToStringBuilder(base).append(array).toString()); 119 assertEquals("{{1,2},<null>,{5}}", new ToStringBuilder(base).append((Object ) array).toString()); 120 array = null; 121 assertEquals("<null>", new ToStringBuilder(base).append(array).toString()); 122 assertEquals("<null>", new ToStringBuilder(base).append((Object ) array).toString()); 123 } 124 125 } 126 | Popular Tags |