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.NoFieldNamesToStringStyleTest}.
029: *
030: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
031: * @version $Id: NoFieldNamesToStringStyleTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class NoFieldNamesToStringStyleTest 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 NoFieldNamesToStringStyleTest(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(
049: NoFieldNamesToStringStyleTest.class);
050: suite.setName("NoFieldNamesToStringStyle Tests");
051: return suite;
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: ToStringBuilder
057: .setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_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 + "[]", new ToStringBuilder(base)
069: .toString());
070: }
071:
072: public void testAppendSuper() {
073: assertEquals(baseStr + "[]", new ToStringBuilder(base)
074: .appendSuper("Integer@8888[]").toString());
075: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
076: .appendSuper("Integer@8888[<null>]").toString());
077:
078: assertEquals(baseStr + "[hello]", new ToStringBuilder(base)
079: .appendSuper("Integer@8888[]").append("a", "hello")
080: .toString());
081: assertEquals(baseStr + "[<null>,hello]", new ToStringBuilder(
082: base).appendSuper("Integer@8888[<null>]").append("a",
083: "hello").toString());
084: assertEquals(baseStr + "[hello]", new ToStringBuilder(base)
085: .appendSuper(null).append("a", "hello").toString());
086: }
087:
088: public void testObject() {
089: Integer i3 = new Integer(3);
090: Integer i4 = new Integer(4);
091: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
092: .append((Object) null).toString());
093: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
094: i3).toString());
095: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
096: .append("a", (Object) null).toString());
097: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
098: "a", i3).toString());
099: assertEquals(baseStr + "[3,4]", new ToStringBuilder(base)
100: .append("a", i3).append("b", i4).toString());
101: assertEquals(baseStr + "[<Integer>]", new ToStringBuilder(base)
102: .append("a", i3, false).toString());
103: assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base)
104: .append("a", new ArrayList(), false).toString());
105: assertEquals(baseStr + "[[]]", new ToStringBuilder(base)
106: .append("a", new ArrayList(), true).toString());
107: assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base)
108: .append("a", new HashMap(), false).toString());
109: assertEquals(baseStr + "[{}]", new ToStringBuilder(base)
110: .append("a", new HashMap(), true).toString());
111: assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base)
112: .append("a", (Object) new String[0], false).toString());
113: assertEquals(baseStr + "[{}]", new ToStringBuilder(base)
114: .append("a", (Object) new String[0], true).toString());
115: }
116:
117: public void testLong() {
118: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
119: 3L).toString());
120: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
121: "a", 3L).toString());
122: assertEquals(baseStr + "[3,4]", new ToStringBuilder(base)
123: .append("a", 3L).append("b", 4L).toString());
124: }
125:
126: public void testObjectArray() {
127: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
128: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
129: new ToStringBuilder(base).append(array).toString());
130: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
131: new ToStringBuilder(base).append((Object) array)
132: .toString());
133: array = null;
134: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
135: .append(array).toString());
136: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
137: .append((Object) array).toString());
138: }
139:
140: public void testLongArray() {
141: long[] array = new long[] { 1, 2, -3, 4 };
142: assertEquals(baseStr + "[{1,2,-3,4}]",
143: new ToStringBuilder(base).append(array).toString());
144: assertEquals(baseStr + "[{1,2,-3,4}]",
145: new ToStringBuilder(base).append((Object) array)
146: .toString());
147: array = null;
148: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
149: .append(array).toString());
150: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
151: .append((Object) array).toString());
152: }
153:
154: public void testLongArrayArray() {
155: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
156: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
157: new ToStringBuilder(base).append(array).toString());
158: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
159: new ToStringBuilder(base).append((Object) array)
160: .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: }
|