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