001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang.builder;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: /**
028: * Unit tests {@link org.apache.commons.lang.builder.DefaultToStringStyleTest}.
029: *
030: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
031: * @version $Id: DefaultToStringStyleTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class DefaultToStringStyleTest extends TestCase {
034:
035: private final Integer base = new Integer(5);
036: private final String baseStr = base.getClass().getName() + "@"
037: + Integer.toHexString(System.identityHashCode(base));
038:
039: public DefaultToStringStyleTest(String name) {
040: super (name);
041: }
042:
043: public static void main(String[] args) {
044: TestRunner.run(suite());
045: }
046:
047: public static Test suite() {
048: TestSuite suite = new TestSuite(DefaultToStringStyleTest.class);
049: suite.setName("DefaultToStringStyle Tests");
050: return suite;
051: }
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
056: }
057:
058: protected void tearDown() throws Exception {
059: super .tearDown();
060: ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
061: }
062:
063: //----------------------------------------------------------------
064:
065: public void testBlank() {
066: assertEquals(baseStr + "[]", new ToStringBuilder(base)
067: .toString());
068: }
069:
070: public void testAppendSuper() {
071: assertEquals(baseStr + "[]", new ToStringBuilder(base)
072: .appendSuper("Integer@8888[]").toString());
073: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
074: .appendSuper("Integer@8888[<null>]").toString());
075:
076: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
077: .appendSuper("Integer@8888[]").append("a", "hello")
078: .toString());
079: assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(
080: base).appendSuper("Integer@8888[<null>]").append("a",
081: "hello").toString());
082: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
083: .appendSuper(null).append("a", "hello").toString());
084: }
085:
086: public void testObject() {
087: Integer i3 = new Integer(3);
088: Integer i4 = new Integer(4);
089: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
090: .append((Object) null).toString());
091: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
092: i3).toString());
093: assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base)
094: .append("a", (Object) null).toString());
095: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
096: .append("a", i3).toString());
097: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
098: .append("a", i3).append("b", i4).toString());
099: assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(
100: base).append("a", i3, false).toString());
101: assertEquals(baseStr + "[a=<size=0>]",
102: new ToStringBuilder(base).append("a", new ArrayList(),
103: false).toString());
104: assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base)
105: .append("a", new ArrayList(), true).toString());
106: assertEquals(baseStr + "[a=<size=0>]",
107: new ToStringBuilder(base).append("a", new HashMap(),
108: false).toString());
109: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
110: .append("a", new HashMap(), true).toString());
111: assertEquals(baseStr + "[a=<size=0>]",
112: new ToStringBuilder(base).append("a",
113: (Object) new String[0], false).toString());
114: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
115: .append("a", (Object) new String[0], true).toString());
116: }
117:
118: public void testLong() {
119: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
120: 3L).toString());
121: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
122: .append("a", 3L).toString());
123: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
124: .append("a", 3L).append("b", 4L).toString());
125: }
126:
127: public void testObjectArray() {
128: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
129: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
130: new ToStringBuilder(base).append(array).toString());
131: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
132: new ToStringBuilder(base).append((Object) array)
133: .toString());
134: array = null;
135: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
136: .append(array).toString());
137: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
138: .append((Object) array).toString());
139: }
140:
141: public void testLongArray() {
142: long[] array = new long[] { 1, 2, -3, 4 };
143: assertEquals(baseStr + "[{1,2,-3,4}]",
144: new ToStringBuilder(base).append(array).toString());
145: assertEquals(baseStr + "[{1,2,-3,4}]",
146: new ToStringBuilder(base).append((Object) array)
147: .toString());
148: array = null;
149: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
150: .append(array).toString());
151: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
152: .append((Object) array).toString());
153: }
154:
155: public void testLongArrayArray() {
156: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
157: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
158: new ToStringBuilder(base).append(array).toString());
159: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
160: new ToStringBuilder(base).append((Object) array)
161: .toString());
162: array = null;
163: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
164: .append(array).toString());
165: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
166: .append((Object) array).toString());
167: }
168:
169: }
|