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: import org.apache.commons.lang.SystemUtils;
027:
028: /**
029: * Unit tests {@link org.apache.commons.lang.builder.MultiLineToStringStyleTest}.
030: *
031: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
032: * @version $Id: MultiLineToStringStyleTest.java 437554 2006-08-28 06:21:41Z bayard $
033: */
034: public class MultiLineToStringStyleTest extends TestCase {
035:
036: private final Integer base = new Integer(5);
037: private final String baseStr = base.getClass().getName() + "@"
038: + Integer.toHexString(System.identityHashCode(base));
039:
040: public MultiLineToStringStyleTest(String name) {
041: super (name);
042: }
043:
044: public static void main(String[] args) {
045: TestRunner.run(suite());
046: }
047:
048: public static Test suite() {
049: TestSuite suite = new TestSuite(
050: MultiLineToStringStyleTest.class);
051: suite.setName("MultiLineToStringStyle Tests");
052: return suite;
053: }
054:
055: protected void setUp() throws Exception {
056: super .setUp();
057: ToStringBuilder.setDefaultStyle(ToStringStyle.MULTI_LINE_STYLE);
058: }
059:
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
063: }
064:
065: //----------------------------------------------------------------
066:
067: public void testBlank() {
068: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + "]",
069: new ToStringBuilder(base).toString());
070: }
071:
072: public void testAppendSuper() {
073: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + "]",
074: new ToStringBuilder(base).appendSuper(
075: "Integer@8888[" + SystemUtils.LINE_SEPARATOR
076: + "]").toString());
077: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
078: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
079: new ToStringBuilder(base).appendSuper(
080: "Integer@8888[" + SystemUtils.LINE_SEPARATOR
081: + " <null>"
082: + SystemUtils.LINE_SEPARATOR + "]")
083: .toString());
084:
085: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
086: + " a=hello" + SystemUtils.LINE_SEPARATOR + "]",
087: new ToStringBuilder(base).appendSuper(
088: "Integer@8888[" + SystemUtils.LINE_SEPARATOR
089: + "]").append("a", "hello").toString());
090: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
091: + " <null>" + SystemUtils.LINE_SEPARATOR + " a=hello"
092: + SystemUtils.LINE_SEPARATOR + "]",
093: new ToStringBuilder(base).appendSuper(
094: "Integer@8888[" + SystemUtils.LINE_SEPARATOR
095: + " <null>"
096: + SystemUtils.LINE_SEPARATOR + "]")
097: .append("a", "hello").toString());
098: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
099: + " a=hello" + SystemUtils.LINE_SEPARATOR + "]",
100: new ToStringBuilder(base).appendSuper(null).append("a",
101: "hello").toString());
102: }
103:
104: public void testObject() {
105: Integer i3 = new Integer(3);
106: Integer i4 = new Integer(4);
107: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
108: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
109: new ToStringBuilder(base).append((Object) null)
110: .toString());
111: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " 3"
112: + SystemUtils.LINE_SEPARATOR + "]",
113: new ToStringBuilder(base).append(i3).toString());
114: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
115: + " a=<null>" + SystemUtils.LINE_SEPARATOR + "]",
116: new ToStringBuilder(base).append("a", (Object) null)
117: .toString());
118: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
119: + " a=3" + SystemUtils.LINE_SEPARATOR + "]",
120: new ToStringBuilder(base).append("a", i3).toString());
121: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
122: + " a=3" + SystemUtils.LINE_SEPARATOR + " b=4"
123: + SystemUtils.LINE_SEPARATOR + "]",
124: new ToStringBuilder(base).append("a", i3).append("b",
125: i4).toString());
126: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
127: + " a=<Integer>" + SystemUtils.LINE_SEPARATOR + "]",
128: new ToStringBuilder(base).append("a", i3, false)
129: .toString());
130: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
131: + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]",
132: new ToStringBuilder(base).append("a", new ArrayList(),
133: false).toString());
134: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
135: + " a=[]" + SystemUtils.LINE_SEPARATOR + "]",
136: new ToStringBuilder(base).append("a", new ArrayList(),
137: true).toString());
138: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
139: + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]",
140: new ToStringBuilder(base).append("a", new HashMap(),
141: false).toString());
142: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
143: + " a={}" + SystemUtils.LINE_SEPARATOR + "]",
144: new ToStringBuilder(base).append("a", new HashMap(),
145: true).toString());
146: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
147: + " a=<size=0>" + SystemUtils.LINE_SEPARATOR + "]",
148: new ToStringBuilder(base).append("a",
149: (Object) new String[0], false).toString());
150: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
151: + " a={}" + SystemUtils.LINE_SEPARATOR + "]",
152: new ToStringBuilder(base).append("a",
153: (Object) new String[0], true).toString());
154: }
155:
156: public void testLong() {
157: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " 3"
158: + SystemUtils.LINE_SEPARATOR + "]",
159: new ToStringBuilder(base).append(3L).toString());
160: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
161: + " a=3" + SystemUtils.LINE_SEPARATOR + "]",
162: new ToStringBuilder(base).append("a", 3L).toString());
163: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
164: + " a=3" + SystemUtils.LINE_SEPARATOR + " b=4"
165: + SystemUtils.LINE_SEPARATOR + "]",
166: new ToStringBuilder(base).append("a", 3L).append("b",
167: 4L).toString());
168: }
169:
170: public void testObjectArray() {
171: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
172: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
173: + " {<null>,5,{3,6}}" + SystemUtils.LINE_SEPARATOR
174: + "]", new ToStringBuilder(base).append(array)
175: .toString());
176: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
177: + " {<null>,5,{3,6}}" + SystemUtils.LINE_SEPARATOR
178: + "]", new ToStringBuilder(base).append((Object) array)
179: .toString());
180: array = null;
181: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
182: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
183: new ToStringBuilder(base).append(array).toString());
184: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
185: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
186: new ToStringBuilder(base).append((Object) array)
187: .toString());
188: }
189:
190: public void testLongArray() {
191: long[] array = new long[] { 1, 2, -3, 4 };
192: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
193: + " {1,2,-3,4}" + SystemUtils.LINE_SEPARATOR + "]",
194: new ToStringBuilder(base).append(array).toString());
195: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
196: + " {1,2,-3,4}" + SystemUtils.LINE_SEPARATOR + "]",
197: new ToStringBuilder(base).append((Object) array)
198: .toString());
199: array = null;
200: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
201: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
202: new ToStringBuilder(base).append(array).toString());
203: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
204: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
205: new ToStringBuilder(base).append((Object) array)
206: .toString());
207: }
208:
209: public void testLongArrayArray() {
210: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
211: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
212: + " {{1,2},<null>,{5}}" + SystemUtils.LINE_SEPARATOR
213: + "]", new ToStringBuilder(base).append(array)
214: .toString());
215: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
216: + " {{1,2},<null>,{5}}" + SystemUtils.LINE_SEPARATOR
217: + "]", new ToStringBuilder(base).append((Object) array)
218: .toString());
219: array = null;
220: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
221: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
222: new ToStringBuilder(base).append(array).toString());
223: assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR
224: + " <null>" + SystemUtils.LINE_SEPARATOR + "]",
225: new ToStringBuilder(base).append((Object) array)
226: .toString());
227: }
228:
229: }
|