KickJava   Java API By Example, From Geeks To Geeks.

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


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.TestCase;
22
23 /**
24  * Unit tests {@link org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE}.
25  *
26  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
27  * @version $Id: ShortPrefixToStringStyleTest.java 161244 2005-04-14 06:16:36Z ggregory $
28  */

29 public class ShortPrefixToStringStyleTest extends TestCase {
30
31     private final Integer JavaDoc base = new Integer JavaDoc(5);
32     private final String JavaDoc baseStr = "Integer";
33     
34     protected void setUp() throws Exception JavaDoc {
35         super.setUp();
36         ToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE);
37     }
38
39     protected void tearDown() throws Exception JavaDoc {
40         super.tearDown();
41         ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
42     }
43
44     //----------------------------------------------------------------
45

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 JavaDoc i3 = new Integer JavaDoc(3);
61         Integer JavaDoc i4 = new Integer JavaDoc(4);
62         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) null).toString());
63         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
64         assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object JavaDoc) 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 JavaDoc(), false).toString());
69         assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), true).toString());
70         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), false).toString());
71         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), true).toString());
72         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], false).toString());
73         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[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 JavaDoc[] array = new Object JavaDoc[] {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 JavaDoc) array).toString());
86         array = null;
87         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
88         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) 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 JavaDoc) array).toString());
95         array = null;
96         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
97         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) 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 JavaDoc) array).toString());
104         array = null;
105         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
106         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
107     }
108     
109 }
110
Popular Tags