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.ToStringStyle}.
029: *
030: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
031: * @version $Id: StandardToStringStyleTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class StandardToStringStyleTest extends TestCase {
034:
035: private final Integer base = new Integer(5);
036: private final String baseStr = "Integer";
037:
038: private static final StandardToStringStyle STYLE = new StandardToStringStyle();
039:
040: static {
041: STYLE.setUseShortClassName(true);
042: STYLE.setUseIdentityHashCode(false);
043: STYLE.setArrayStart("[");
044: STYLE.setArraySeparator(", ");
045: STYLE.setArrayEnd("]");
046: STYLE.setNullText("%NULL%");
047: STYLE.setSizeStartText("%SIZE=");
048: STYLE.setSizeEndText("%");
049: STYLE.setSummaryObjectStartText("%");
050: STYLE.setSummaryObjectEndText("%");
051: }
052:
053: public StandardToStringStyleTest(String name) {
054: super (name);
055: }
056:
057: public static void main(String[] args) {
058: TestRunner.run(suite());
059: }
060:
061: public static Test suite() {
062: TestSuite suite = new TestSuite(StandardToStringStyleTest.class);
063: suite.setName("StandardToStringStyle Tests");
064: return suite;
065: }
066:
067: protected void setUp() throws Exception {
068: super .setUp();
069: ToStringBuilder.setDefaultStyle(STYLE);
070: }
071:
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: ToStringBuilder.setDefaultStyle(STYLE);
075: }
076:
077: //----------------------------------------------------------------
078:
079: public void testBlank() {
080: assertEquals(baseStr + "[]", new ToStringBuilder(base)
081: .toString());
082: }
083:
084: public void testAppendSuper() {
085: assertEquals(baseStr + "[]", new ToStringBuilder(base)
086: .appendSuper("Integer@8888[]").toString());
087: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
088: .appendSuper("Integer@8888[%NULL%]").toString());
089:
090: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
091: .appendSuper("Integer@8888[]").append("a", "hello")
092: .toString());
093: assertEquals(baseStr + "[%NULL%,a=hello]", new ToStringBuilder(
094: base).appendSuper("Integer@8888[%NULL%]").append("a",
095: "hello").toString());
096: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
097: .appendSuper(null).append("a", "hello").toString());
098: }
099:
100: public void testObject() {
101: Integer i3 = new Integer(3);
102: Integer i4 = new Integer(4);
103: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
104: .append((Object) null).toString());
105: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
106: i3).toString());
107: assertEquals(baseStr + "[a=%NULL%]", new ToStringBuilder(base)
108: .append("a", (Object) null).toString());
109: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
110: .append("a", i3).toString());
111: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
112: .append("a", i3).append("b", i4).toString());
113: assertEquals(baseStr + "[a=%Integer%]", new ToStringBuilder(
114: base).append("a", i3, false).toString());
115: assertEquals(baseStr + "[a=%SIZE=0%]",
116: new ToStringBuilder(base).append("a", new ArrayList(),
117: false).toString());
118: assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base)
119: .append("a", new ArrayList(), true).toString());
120: assertEquals(baseStr + "[a=%SIZE=0%]",
121: new ToStringBuilder(base).append("a", new HashMap(),
122: false).toString());
123: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
124: .append("a", new HashMap(), true).toString());
125: assertEquals(baseStr + "[a=%SIZE=0%]",
126: new ToStringBuilder(base).append("a",
127: (Object) new String[0], false).toString());
128: assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base)
129: .append("a", (Object) new String[0], true).toString());
130: }
131:
132: public void testLong() {
133: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
134: 3L).toString());
135: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
136: .append("a", 3L).toString());
137: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
138: .append("a", 3L).append("b", 4L).toString());
139: }
140:
141: public void testObjectArray() {
142: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
143: assertEquals(baseStr + "[[%NULL%, 5, [3, 6]]]",
144: new ToStringBuilder(base).append(array).toString());
145: assertEquals(baseStr + "[[%NULL%, 5, [3, 6]]]",
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 testLongArray() {
156: long[] array = new long[] { 1, 2, -3, 4 };
157: assertEquals(baseStr + "[[1, 2, -3, 4]]", new ToStringBuilder(
158: base).append(array).toString());
159: assertEquals(baseStr + "[[1, 2, -3, 4]]", new ToStringBuilder(
160: base).append((Object) array).toString());
161: array = null;
162: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
163: .append(array).toString());
164: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
165: .append((Object) array).toString());
166: }
167:
168: public void testLongArrayArray() {
169: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
170: assertEquals(baseStr + "[[[1, 2], %NULL%, [5]]]",
171: new ToStringBuilder(base).append(array).toString());
172: assertEquals(baseStr + "[[[1, 2], %NULL%, [5]]]",
173: new ToStringBuilder(base).append((Object) array)
174: .toString());
175: array = null;
176: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
177: .append(array).toString());
178: assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base)
179: .append((Object) array).toString());
180: }
181:
182: }
|