KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > DefaultToStringStyleTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.builder;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25
26 /**
27  * Unit tests {@link org.apache.commons.lang.builder.DefaultToStringStyleTest}.
28  *
29  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
30  * @version $Id: DefaultToStringStyleTest.java 161244 2005-04-14 06:16:36Z ggregory $
31  */

32 public class DefaultToStringStyleTest extends TestCase {
33
34     private final Integer JavaDoc base = new Integer JavaDoc(5);
35     private final String JavaDoc baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
36     
37     public DefaultToStringStyleTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] 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 JavaDoc {
52         super.setUp();
53         ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58         ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
59     }
60
61     //----------------------------------------------------------------
62

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 JavaDoc i3 = new Integer JavaDoc(3);
78         Integer JavaDoc i4 = new Integer JavaDoc(4);
79         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) null).toString());
80         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
81         assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object JavaDoc) 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 JavaDoc(), false).toString());
86         assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), true).toString());
87         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), false).toString());
88         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), true).toString());
89         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], false).toString());
90         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[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 JavaDoc[] array = new Object JavaDoc[] {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 JavaDoc) array).toString());
103         array = null;
104         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
105         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) 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 JavaDoc) array).toString());
112         array = null;
113         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
114         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) 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 JavaDoc) array).toString());
121         array = null;
122         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
123         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
124     }
125
126 }
127
Popular Tags