KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.lang.SystemUtils;
26
27 /**
28  * Unit tests {@link org.apache.commons.lang.builder.MultiLineToStringStyleTest}.
29  *
30  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
31  * @version $Id: MultiLineToStringStyleTest.java 161244 2005-04-14 06:16:36Z ggregory $
32  */

33 public class MultiLineToStringStyleTest extends TestCase {
34
35     private final Integer JavaDoc base = new Integer JavaDoc(5);
36     private final String JavaDoc baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
37     
38     public MultiLineToStringStyleTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(suite());
44     }
45
46     public static Test suite() {
47         TestSuite suite = new TestSuite(MultiLineToStringStyleTest.class);
48         suite.setName("MultiLineToStringStyle Tests");
49         return suite;
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54         ToStringBuilder.setDefaultStyle(ToStringStyle.MULTI_LINE_STYLE);
55     }
56
57     protected void tearDown() throws Exception JavaDoc {
58         super.tearDown();
59         ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
60     }
61
62     //----------------------------------------------------------------
63

64     public void testBlank() {
65         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).toString());
66     }
67
68     public void testAppendSuper() {
69         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).appendSuper("Integer@8888[" + SystemUtils.LINE_SEPARATOR + "]").toString());
70         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).appendSuper("Integer@8888[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]").toString());
71         
72         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=hello" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).appendSuper("Integer@8888[" + SystemUtils.LINE_SEPARATOR + "]").append("a", "hello").toString());
73         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + " a=hello" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).appendSuper("Integer@8888[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]").append("a", "hello").toString());
74         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=hello" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
75     }
76     
77     public void testObject() {
78         Integer JavaDoc i3 = new Integer JavaDoc(3);
79         Integer JavaDoc i4 = new Integer JavaDoc(4);
80         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) null).toString());
81         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " 3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(i3).toString());
82         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=<null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", (Object JavaDoc) null).toString());
83         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", i3).toString());
84         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=3" + SystemUtils.LINE_SEPARATOR + " b=4" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
85         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=<Integer>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", i3, false).toString());
86         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), false).toString());
87         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=[]" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), true).toString());
88         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), false).toString());
89         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a={}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), true).toString());
90         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], false).toString());
91         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a={}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], true).toString());
92     }
93
94     public void testLong() {
95         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " 3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(3L).toString());
96         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", 3L).toString());
97         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=3" + SystemUtils.LINE_SEPARATOR + " b=4" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString());
98     }
99
100     public void testObjectArray() {
101         Object JavaDoc[] array = new Object JavaDoc[] {null, base, new int[] {3, 6}};
102         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {<null>,5,{3,6}}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
103         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {<null>,5,{3,6}}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
104         array = null;
105         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
106         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
107     }
108
109     public void testLongArray() {
110         long[] array = new long[] {1, 2, -3, 4};
111         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {1,2,-3,4}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
112         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {1,2,-3,4}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
113         array = null;
114         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
115         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
116     }
117
118     public void testLongArrayArray() {
119         long[][] array = new long[][] {{1, 2}, null, {5}};
120         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {{1,2},<null>,{5}}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
121         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " {{1,2},<null>,{5}}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
122         array = null;
123         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(array).toString());
124         assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " <null>" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
125     }
126
127 }
128
Popular Tags