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.TestCase;
023:
024: /**
025: * Unit tests {@link org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE}.
026: *
027: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
028: * @version $Id: ShortPrefixToStringStyleTest.java 437554 2006-08-28 06:21:41Z bayard $
029: */
030: public class ShortPrefixToStringStyleTest extends TestCase {
031:
032: private final Integer base = new Integer(5);
033: private final String baseStr = "Integer";
034:
035: protected void setUp() throws Exception {
036: super .setUp();
037: ToStringBuilder
038: .setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE);
039: }
040:
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
044: }
045:
046: //----------------------------------------------------------------
047:
048: public void testBlank() {
049: assertEquals(baseStr + "[]", new ToStringBuilder(base)
050: .toString());
051: }
052:
053: public void testAppendSuper() {
054: assertEquals(baseStr + "[]", new ToStringBuilder(base)
055: .appendSuper("Integer@8888[]").toString());
056: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
057: .appendSuper("Integer@8888[<null>]").toString());
058:
059: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
060: .appendSuper("Integer@8888[]").append("a", "hello")
061: .toString());
062: assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(
063: base).appendSuper("Integer@8888[<null>]").append("a",
064: "hello").toString());
065: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
066: .appendSuper(null).append("a", "hello").toString());
067: }
068:
069: public void testObject() {
070: Integer i3 = new Integer(3);
071: Integer i4 = new Integer(4);
072: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
073: .append((Object) null).toString());
074: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
075: i3).toString());
076: assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base)
077: .append("a", (Object) null).toString());
078: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
079: .append("a", i3).toString());
080: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
081: .append("a", i3).append("b", i4).toString());
082: assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(
083: base).append("a", i3, false).toString());
084: assertEquals(baseStr + "[a=<size=0>]",
085: new ToStringBuilder(base).append("a", new ArrayList(),
086: false).toString());
087: assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base)
088: .append("a", new ArrayList(), true).toString());
089: assertEquals(baseStr + "[a=<size=0>]",
090: new ToStringBuilder(base).append("a", new HashMap(),
091: false).toString());
092: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
093: .append("a", new HashMap(), true).toString());
094: assertEquals(baseStr + "[a=<size=0>]",
095: new ToStringBuilder(base).append("a",
096: (Object) new String[0], false).toString());
097: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
098: .append("a", (Object) new String[0], true).toString());
099: }
100:
101: public void testLong() {
102: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
103: 3L).toString());
104: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
105: .append("a", 3L).toString());
106: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
107: .append("a", 3L).append("b", 4L).toString());
108: }
109:
110: public void testObjectArray() {
111: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
112: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
113: new ToStringBuilder(base).append(array).toString());
114: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
115: new ToStringBuilder(base).append((Object) array)
116: .toString());
117: array = null;
118: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
119: .append(array).toString());
120: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
121: .append((Object) array).toString());
122: }
123:
124: public void testLongArray() {
125: long[] array = new long[] { 1, 2, -3, 4 };
126: assertEquals(baseStr + "[{1,2,-3,4}]",
127: new ToStringBuilder(base).append(array).toString());
128: assertEquals(baseStr + "[{1,2,-3,4}]",
129: new ToStringBuilder(base).append((Object) array)
130: .toString());
131: array = null;
132: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
133: .append(array).toString());
134: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
135: .append((Object) array).toString());
136: }
137:
138: public void testLongArrayArray() {
139: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
140: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
141: new ToStringBuilder(base).append(array).toString());
142: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
143: new ToStringBuilder(base).append((Object) array)
144: .toString());
145: array = null;
146: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
147: .append(array).toString());
148: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
149: .append((Object) array).toString());
150: }
151:
152: }
|