KickJava   Java API By Example, From Geeks To Geeks.

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


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.SimpleToStringStyleTest}.
28  *
29  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
30  * @version $Id: SimpleToStringStyleTest.java 161244 2005-04-14 06:16:36Z ggregory $
31  */

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

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