0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.commons.lang.builder;
0018:
0019: import java.util.ArrayList;
0020: import java.util.HashMap;
0021: import java.util.List;
0022:
0023: import junit.framework.Test;
0024: import junit.framework.TestCase;
0025: import junit.framework.TestSuite;
0026: import junit.textui.TestRunner;
0027:
0028: /**
0029: * Unit tests for {@link org.apache.commons.lang.builder.ToStringBuilder}.
0030: *
0031: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
0032: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
0033: * @author <a href="mailto:alex@apache.org">Alex Chaffee</a>
0034: * @version $Id: ToStringBuilderTest.java 500495 2007-01-27 07:11:08Z bayard $
0035: */
0036: public class ToStringBuilderTest extends TestCase {
0037:
0038: private final Integer base = new Integer(5);
0039: private final String baseStr = base.getClass().getName() + "@"
0040: + Integer.toHexString(System.identityHashCode(base));
0041:
0042: public ToStringBuilderTest(String name) {
0043: super (name);
0044: }
0045:
0046: public static void main(String[] args) {
0047: TestRunner.run(suite());
0048: }
0049:
0050: public static Test suite() {
0051: TestSuite suite = new TestSuite(ToStringBuilderTest.class);
0052: suite.setName("ToStringBuilder Tests");
0053: return suite;
0054: }
0055:
0056: protected void setUp() throws Exception {
0057: super .setUp();
0058: }
0059:
0060: protected void tearDown() throws Exception {
0061: super .tearDown();
0062: }
0063:
0064: //-----------------------------------------------------------------------
0065:
0066: public void testConstructorEx1() {
0067: assertEquals("<null>", new ToStringBuilder(null).toString());
0068: }
0069:
0070: public void testConstructorEx2() {
0071: assertEquals("<null>", new ToStringBuilder(null, null)
0072: .toString());
0073: new ToStringBuilder(this .base, null).toString();
0074: }
0075:
0076: public void testConstructorEx3() {
0077: assertEquals("<null>", new ToStringBuilder(null, null, null)
0078: .toString());
0079: new ToStringBuilder(this .base, null, null);
0080: new ToStringBuilder(this .base, ToStringStyle.DEFAULT_STYLE,
0081: null);
0082: }
0083:
0084: public void testGetSetDefault() {
0085: try {
0086: ToStringBuilder
0087: .setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE);
0088: assertSame(ToStringStyle.NO_FIELD_NAMES_STYLE,
0089: ToStringBuilder.getDefaultStyle());
0090: } finally {
0091: // reset for other tests
0092: ToStringBuilder
0093: .setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
0094: }
0095: }
0096:
0097: public void testSetDefaultEx() {
0098: try {
0099: ToStringBuilder.setDefaultStyle(null);
0100:
0101: } catch (IllegalArgumentException ex) {
0102: return;
0103: }
0104: fail();
0105: }
0106:
0107: public void testBlank() {
0108: assertEquals(baseStr + "[]", new ToStringBuilder(base)
0109: .toString());
0110: }
0111:
0112: /**
0113: * Test wrapper for int primitive.
0114: */
0115: public void testReflectionInteger() {
0116: assertEquals(baseStr + "[value=5]", ToStringBuilder
0117: .reflectionToString(base));
0118: }
0119:
0120: /**
0121: * Test wrapper for char primitive.
0122: */
0123: public void testReflectionCharacter() {
0124: Character c = new Character('A');
0125: assertEquals(this .toBaseString(c) + "[value=A]",
0126: ToStringBuilder.reflectionToString(c));
0127: }
0128:
0129: /**
0130: * Test wrapper for char boolean.
0131: */
0132: public void testReflectionBoolean() {
0133: Boolean b;
0134: b = Boolean.TRUE;
0135: assertEquals(this .toBaseString(b) + "[value=true]",
0136: ToStringBuilder.reflectionToString(b));
0137: b = Boolean.FALSE;
0138: assertEquals(this .toBaseString(b) + "[value=false]",
0139: ToStringBuilder.reflectionToString(b));
0140: }
0141:
0142: /**
0143: * Create the same toString() as Object.toString().
0144: * @param o the object to create the string for.
0145: * @return a String in the Object.toString format.
0146: */
0147: private String toBaseString(Object o) {
0148: return o.getClass().getName() + "@"
0149: + Integer.toHexString(System.identityHashCode(o));
0150: }
0151:
0152: // Reflection Array tests
0153:
0154: //
0155: // Note on the following line of code repeated in the reflection array tests.
0156: //
0157: // assertReflectionArray("<null>", array);
0158: //
0159: // The expected value is not baseStr + "[<null>]" since array==null and is typed as Object.
0160: // The null array does not carry array type information.
0161: // If we added a primitive array type constructor and pile of associated methods,
0162: // then type declaring type information could be carried forward. IMHO, null is null.
0163: //
0164: // Gary Gregory - 2003-03-12 - ggregory@seagullsw.com
0165: //
0166:
0167: public void assertReflectionArray(String expected, Object actual) {
0168: if (actual == null) {
0169: // Until ToStringBuilder supports null objects.
0170: return;
0171: }
0172: assertEquals(expected, ToStringBuilder
0173: .reflectionToString(actual));
0174: assertEquals(expected, ToStringBuilder.reflectionToString(
0175: actual, null));
0176: assertEquals(expected, ToStringBuilder.reflectionToString(
0177: actual, null, true));
0178: assertEquals(expected, ToStringBuilder.reflectionToString(
0179: actual, null, false));
0180: }
0181:
0182: public void testReflectionObjectArray() {
0183: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
0184: String baseStr = this .toBaseString(array);
0185: assertEquals(baseStr + "[{<null>,5,{3,6}}]", ToStringBuilder
0186: .reflectionToString(array));
0187: array = null;
0188: assertReflectionArray("<null>", array);
0189: this .validateEmptyToStringStyleRegistry();
0190: }
0191:
0192: public void testReflectionLongArray() {
0193: long[] array = new long[] { 1, 2, -3, 4 };
0194: String baseStr = this .toBaseString(array);
0195: assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder
0196: .reflectionToString(array));
0197: array = null;
0198: assertReflectionArray("<null>", array);
0199: this .validateEmptyToStringStyleRegistry();
0200: }
0201:
0202: public void testReflectionIntArray() {
0203: int[] array = new int[] { 1, 2, -3, 4 };
0204: String baseStr = this .toBaseString(array);
0205: assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder
0206: .reflectionToString(array));
0207: array = null;
0208: assertReflectionArray("<null>", array);
0209: this .validateEmptyToStringStyleRegistry();
0210: }
0211:
0212: public void testReflectionShortArray() {
0213: short[] array = new short[] { 1, 2, -3, 4 };
0214: String baseStr = this .toBaseString(array);
0215: assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder
0216: .reflectionToString(array));
0217: array = null;
0218: assertReflectionArray("<null>", array);
0219: this .validateEmptyToStringStyleRegistry();
0220: }
0221:
0222: public void testReflectionyteArray() {
0223: byte[] array = new byte[] { 1, 2, -3, 4 };
0224: String baseStr = this .toBaseString(array);
0225: assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder
0226: .reflectionToString(array));
0227: array = null;
0228: assertReflectionArray("<null>", array);
0229: this .validateEmptyToStringStyleRegistry();
0230: }
0231:
0232: public void testReflectionCharArray() {
0233: char[] array = new char[] { 'A', '2', '_', 'D' };
0234: String baseStr = this .toBaseString(array);
0235: assertEquals(baseStr + "[{A,2,_,D}]", ToStringBuilder
0236: .reflectionToString(array));
0237: array = null;
0238: assertReflectionArray("<null>", array);
0239: this .validateEmptyToStringStyleRegistry();
0240: }
0241:
0242: public void testReflectionDoubleArray() {
0243: double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 };
0244: String baseStr = this .toBaseString(array);
0245: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0246: ToStringBuilder.reflectionToString(array));
0247: array = null;
0248: assertReflectionArray("<null>", array);
0249: this .validateEmptyToStringStyleRegistry();
0250: }
0251:
0252: public void testReflectionFloatArray() {
0253: float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f };
0254: String baseStr = this .toBaseString(array);
0255: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0256: ToStringBuilder.reflectionToString(array));
0257: array = null;
0258: assertReflectionArray("<null>", array);
0259: this .validateEmptyToStringStyleRegistry();
0260: }
0261:
0262: public void testReflectionBooleanArray() {
0263: boolean[] array = new boolean[] { true, false, false };
0264: String baseStr = this .toBaseString(array);
0265: assertEquals(baseStr + "[{true,false,false}]", ToStringBuilder
0266: .reflectionToString(array));
0267: array = null;
0268: assertReflectionArray("<null>", array);
0269: this .validateEmptyToStringStyleRegistry();
0270: }
0271:
0272: // Reflection Array Array tests
0273:
0274: public void testReflectionFloatArrayArray() {
0275: float[][] array = new float[][] { { 1.0f, 2.29686f }, null,
0276: { Float.NaN } };
0277: String baseStr = this .toBaseString(array);
0278: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
0279: ToStringBuilder.reflectionToString(array));
0280: array = null;
0281: assertReflectionArray("<null>", array);
0282: this .validateEmptyToStringStyleRegistry();
0283: }
0284:
0285: public void testReflectionLongArrayArray() {
0286: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
0287: String baseStr = this .toBaseString(array);
0288: assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder
0289: .reflectionToString(array));
0290: array = null;
0291: assertReflectionArray("<null>", array);
0292: this .validateEmptyToStringStyleRegistry();
0293: }
0294:
0295: public void testReflectionIntArrayArray() {
0296: int[][] array = new int[][] { { 1, 2 }, null, { 5 } };
0297: String baseStr = this .toBaseString(array);
0298: assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder
0299: .reflectionToString(array));
0300: array = null;
0301: assertReflectionArray("<null>", array);
0302: this .validateEmptyToStringStyleRegistry();
0303: }
0304:
0305: public void testReflectionhortArrayArray() {
0306: short[][] array = new short[][] { { 1, 2 }, null, { 5 } };
0307: String baseStr = this .toBaseString(array);
0308: assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder
0309: .reflectionToString(array));
0310: array = null;
0311: assertReflectionArray("<null>", array);
0312: this .validateEmptyToStringStyleRegistry();
0313: }
0314:
0315: public void testReflectionByteArrayArray() {
0316: byte[][] array = new byte[][] { { 1, 2 }, null, { 5 } };
0317: String baseStr = this .toBaseString(array);
0318: assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder
0319: .reflectionToString(array));
0320: array = null;
0321: assertReflectionArray("<null>", array);
0322: this .validateEmptyToStringStyleRegistry();
0323: }
0324:
0325: public void testReflectionCharArrayArray() {
0326: char[][] array = new char[][] { { 'A', 'B' }, null, { 'p' } };
0327: String baseStr = this .toBaseString(array);
0328: assertEquals(baseStr + "[{{A,B},<null>,{p}}]", ToStringBuilder
0329: .reflectionToString(array));
0330: array = null;
0331: assertReflectionArray("<null>", array);
0332: this .validateEmptyToStringStyleRegistry();
0333: }
0334:
0335: public void testReflectionDoubleArrayArray() {
0336: double[][] array = new double[][] { { 1.0, 2.29686 }, null,
0337: { Double.NaN } };
0338: String baseStr = this .toBaseString(array);
0339: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
0340: ToStringBuilder.reflectionToString(array));
0341: array = null;
0342: assertReflectionArray("<null>", array);
0343: this .validateEmptyToStringStyleRegistry();
0344: }
0345:
0346: public void testReflectionBooleanArrayArray() {
0347: boolean[][] array = new boolean[][] { { true, false }, null,
0348: { false } };
0349: String baseStr = this .toBaseString(array);
0350: assertEquals(baseStr + "[{{true,false},<null>,{false}}]",
0351: ToStringBuilder.reflectionToString(array));
0352: assertEquals(baseStr + "[{{true,false},<null>,{false}}]",
0353: ToStringBuilder.reflectionToString(array));
0354: array = null;
0355: assertReflectionArray("<null>", array);
0356: this .validateEmptyToStringStyleRegistry();
0357: }
0358:
0359: // Reflection hierarchy tests
0360:
0361: public void testReflectionHierarchyArrayList() {
0362: List base = new ArrayList();
0363: String baseStr = this .toBaseString(base);
0364: assertEquals(
0365: baseStr
0366: + "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]",
0367: ToStringBuilder.reflectionToString(base, null, true));
0368: assertEquals(baseStr + "[size=0]", ToStringBuilder
0369: .reflectionToString(base, null, false));
0370: this .validateEmptyToStringStyleRegistry();
0371: }
0372:
0373: public void testReflectionHierarchy() {
0374: ReflectionTestFixtureA baseA = new ReflectionTestFixtureA();
0375: String baseStr = this .toBaseString(baseA);
0376: assertEquals(baseStr + "[a=a]", ToStringBuilder
0377: .reflectionToString(baseA));
0378: assertEquals(baseStr + "[a=a]", ToStringBuilder
0379: .reflectionToString(baseA, null));
0380: assertEquals(baseStr + "[a=a]", ToStringBuilder
0381: .reflectionToString(baseA, null, false));
0382: assertEquals(baseStr + "[a=a,transientA=t]", ToStringBuilder
0383: .reflectionToString(baseA, null, true));
0384: assertEquals(baseStr + "[a=a]", ToStringBuilder
0385: .reflectionToString(baseA, null, false, null));
0386: assertEquals(baseStr + "[a=a]", ToStringBuilder
0387: .reflectionToString(baseA, null, false, Object.class));
0388: assertEquals(baseStr + "[a=a]", ToStringBuilder
0389: .reflectionToString(baseA, null, false, List.class));
0390: assertEquals(baseStr + "[a=a]", ToStringBuilder
0391: .reflectionToString(baseA, null, false,
0392: ReflectionTestFixtureA.class));
0393:
0394: ReflectionTestFixtureB baseB = new ReflectionTestFixtureB();
0395: baseStr = this .toBaseString(baseB);
0396: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0397: .reflectionToString(baseB));
0398: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0399: .reflectionToString(baseB));
0400: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0401: .reflectionToString(baseB, null));
0402: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0403: .reflectionToString(baseB, null, false));
0404: assertEquals(baseStr + "[b=b,transientB=t,a=a,transientA=t]",
0405: ToStringBuilder.reflectionToString(baseB, null, true));
0406: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0407: .reflectionToString(baseB, null, false, null));
0408: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0409: .reflectionToString(baseB, null, false, Object.class));
0410: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0411: .reflectionToString(baseB, null, false, List.class));
0412: assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder
0413: .reflectionToString(baseB, null, false,
0414: ReflectionTestFixtureA.class));
0415: assertEquals(baseStr + "[b=b]", ToStringBuilder
0416: .reflectionToString(baseB, null, false,
0417: ReflectionTestFixtureB.class));
0418: this .validateEmptyToStringStyleRegistry();
0419: }
0420:
0421: static class ReflectionTestFixtureA {
0422: private char a = 'a';
0423: private transient char transientA = 't';
0424: }
0425:
0426: static class ReflectionTestFixtureB extends ReflectionTestFixtureA {
0427: private char b = 'b';
0428: private transient char transientB = 't';
0429: }
0430:
0431: public void testInnerClassReflection() {
0432: Outer outer = new Outer();
0433: assertEquals(toBaseString(outer) + "[inner="
0434: + toBaseString(outer.inner) + "[]]", outer.toString());
0435: }
0436:
0437: static class Outer {
0438: Inner inner = new Inner();
0439:
0440: class Inner {
0441: public String toString() {
0442: return ToStringBuilder.reflectionToString(this );
0443: }
0444: }
0445:
0446: public String toString() {
0447: return ToStringBuilder.reflectionToString(this );
0448: }
0449: }
0450:
0451: // Reflection cycle tests
0452:
0453: /**
0454: * Test an array element pointing to its container.
0455: */
0456: public void testReflectionArrayCycle() throws Exception {
0457: Object[] objects = new Object[1];
0458: objects[0] = objects;
0459: assertEquals(this .toBaseString(objects) + "[{"
0460: + this .toBaseString(objects) + "}]", ToStringBuilder
0461: .reflectionToString(objects));
0462: this .validateEmptyToStringStyleRegistry();
0463: }
0464:
0465: /**
0466: * Test an array element pointing to its container.
0467: */
0468: public void testReflectionArrayCycleLevel2() throws Exception {
0469: Object[] objects = new Object[1];
0470: Object[] objectsLevel2 = new Object[1];
0471: objects[0] = objectsLevel2;
0472: objectsLevel2[0] = (Object) objects;
0473: assertEquals(this .toBaseString(objects) + "[{{"
0474: + this .toBaseString(objects) + "}}]", ToStringBuilder
0475: .reflectionToString(objects));
0476: assertEquals(this .toBaseString(objectsLevel2) + "[{{"
0477: + this .toBaseString(objectsLevel2) + "}}]",
0478: ToStringBuilder.reflectionToString(objectsLevel2));
0479: this .validateEmptyToStringStyleRegistry();
0480: }
0481:
0482: public void testReflectionArrayArrayCycle() throws Exception {
0483: Object[][] objects = new Object[2][2];
0484: objects[0][0] = objects;
0485: objects[0][1] = objects;
0486: objects[1][0] = objects;
0487: objects[1][1] = objects;
0488: String basicToString = this .toBaseString(objects);
0489: assertEquals(basicToString + "[{{" + basicToString + ","
0490: + basicToString + "},{" + basicToString + ","
0491: + basicToString + "}}]", ToStringBuilder
0492: .reflectionToString(objects));
0493: this .validateEmptyToStringStyleRegistry();
0494: }
0495:
0496: /**
0497: * A reflection test fixture.
0498: */
0499: static class ReflectionTestCycleA {
0500: ReflectionTestCycleB b;
0501:
0502: public String toString() {
0503: return ToStringBuilder.reflectionToString(this );
0504: }
0505: }
0506:
0507: /**
0508: * A reflection test fixture.
0509: */
0510: static class ReflectionTestCycleB {
0511: ReflectionTestCycleA a;
0512:
0513: public String toString() {
0514: return ToStringBuilder.reflectionToString(this );
0515: }
0516: }
0517:
0518: /**
0519: * A reflection test fixture.
0520: */
0521: static class SimpleReflectionTestFixture {
0522: Object o;
0523:
0524: public SimpleReflectionTestFixture() {
0525: }
0526:
0527: public SimpleReflectionTestFixture(Object o) {
0528: this .o = o;
0529: }
0530:
0531: public String toString() {
0532: return ToStringBuilder.reflectionToString(this );
0533: }
0534: }
0535:
0536: private static class SelfInstanceVarReflectionTestFixture {
0537: private SelfInstanceVarReflectionTestFixture typeIsSelf;
0538:
0539: public SelfInstanceVarReflectionTestFixture() {
0540: this .typeIsSelf = this ;
0541: }
0542:
0543: public String toString() {
0544: return ToStringBuilder.reflectionToString(this );
0545: }
0546: }
0547:
0548: private static class SelfInstanceTwoVarsReflectionTestFixture {
0549: private SelfInstanceTwoVarsReflectionTestFixture typeIsSelf;
0550: private String otherType = "The Other Type";
0551:
0552: public SelfInstanceTwoVarsReflectionTestFixture() {
0553: this .typeIsSelf = this ;
0554: }
0555:
0556: public String getOtherType() {
0557: return this .otherType;
0558: }
0559:
0560: public String toString() {
0561: return ToStringBuilder.reflectionToString(this );
0562: }
0563: }
0564:
0565: /**
0566: * Test an Object pointing to itself, the simplest test.
0567: *
0568: * @throws Exception
0569: */
0570: public void testSimpleReflectionObjectCycle() throws Exception {
0571: SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture();
0572: simple.o = simple;
0573: assertTrue(ToStringStyle.getRegistry().isEmpty());
0574: assertEquals(this .toBaseString(simple) + "[o="
0575: + this .toBaseString(simple) + "]", simple.toString());
0576: this .validateEmptyToStringStyleRegistry();
0577: }
0578:
0579: /**
0580: * Test a class that defines an ivar pointing to itself.
0581: *
0582: * @throws Exception
0583: */
0584: public void testSelfInstanceVarReflectionObjectCycle()
0585: throws Exception {
0586: SelfInstanceVarReflectionTestFixture test = new SelfInstanceVarReflectionTestFixture();
0587: assertTrue(ToStringStyle.getRegistry().isEmpty());
0588: assertEquals(this .toBaseString(test) + "[typeIsSelf="
0589: + this .toBaseString(test) + "]", test.toString());
0590: this .validateEmptyToStringStyleRegistry();
0591: }
0592:
0593: /**
0594: * Test a class that defines an ivar pointing to itself. This test was
0595: * created to show that handling cyclical object resulted in a missing endFieldSeparator call.
0596: *
0597: * @throws Exception
0598: */
0599: public void testSelfInstanceTwoVarsReflectionObjectCycle()
0600: throws Exception {
0601: SelfInstanceTwoVarsReflectionTestFixture test = new SelfInstanceTwoVarsReflectionTestFixture();
0602: assertTrue(ToStringStyle.getRegistry().isEmpty());
0603: assertEquals(this .toBaseString(test) + "[typeIsSelf="
0604: + this .toBaseString(test) + ",otherType="
0605: + test.getOtherType().toString() + "]", test.toString());
0606: this .validateEmptyToStringStyleRegistry();
0607: }
0608:
0609: /**
0610: * Test Objects pointing to each other.
0611: *
0612: * @throws Exception
0613: */
0614: public void testReflectionObjectCycle() throws Exception {
0615: ReflectionTestCycleA a = new ReflectionTestCycleA();
0616: ReflectionTestCycleB b = new ReflectionTestCycleB();
0617: a.b = b;
0618: b.a = a;
0619: assertEquals(this .toBaseString(a) + "[b="
0620: + this .toBaseString(b) + "[a=" + this .toBaseString(a)
0621: + "]]", a.toString());
0622: this .validateEmptyToStringStyleRegistry();
0623: }
0624:
0625: /**
0626: * Test a nasty combination of arrays and Objects pointing to each other.
0627: * objects[0] -> SimpleReflectionTestFixture[ o -> objects ]
0628: *
0629: * @throws Exception
0630: */
0631: public void testReflectionArrayAndObjectCycle() throws Exception {
0632: Object[] objects = new Object[1];
0633: SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(
0634: objects);
0635: objects[0] = (Object) simple;
0636: assertEquals(this .toBaseString(objects) + "[{"
0637: + this .toBaseString(simple) + "[o="
0638: + this .toBaseString(objects) + "]" + "}]",
0639: ToStringBuilder.reflectionToString(objects));
0640: assertEquals(this .toBaseString(simple) + "[o={"
0641: + this .toBaseString(simple) + "}]", ToStringBuilder
0642: .reflectionToString(simple));
0643: this .validateEmptyToStringStyleRegistry();
0644: }
0645:
0646: void validateEmptyToStringStyleRegistry() {
0647: if (!ToStringStyle.getRegistry().isEmpty()) {
0648: System.out.println(ToStringStyle.getRegistry());
0649: }
0650:
0651: assertTrue(ToStringStyle.getRegistry().isEmpty());
0652: }
0653:
0654: // End: Reflection cycle tests
0655:
0656: public void testAppendSuper() {
0657: assertEquals(baseStr + "[]", new ToStringBuilder(base)
0658: .appendSuper("Integer@8888[]").toString());
0659: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0660: .appendSuper("Integer@8888[<null>]").toString());
0661:
0662: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
0663: .appendSuper("Integer@8888[]").append("a", "hello")
0664: .toString());
0665: assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(
0666: base).appendSuper("Integer@8888[<null>]").append("a",
0667: "hello").toString());
0668: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
0669: .appendSuper(null).append("a", "hello").toString());
0670: }
0671:
0672: public void testAppendToString() {
0673: assertEquals(baseStr + "[]", new ToStringBuilder(base)
0674: .appendToString("Integer@8888[]").toString());
0675: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0676: .appendToString("Integer@8888[<null>]").toString());
0677:
0678: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
0679: .appendToString("Integer@8888[]").append("a", "hello")
0680: .toString());
0681: assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(
0682: base).appendToString("Integer@8888[<null>]").append(
0683: "a", "hello").toString());
0684: assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base)
0685: .appendToString(null).append("a", "hello").toString());
0686: }
0687:
0688: public void testObject() {
0689: Integer i3 = new Integer(3);
0690: Integer i4 = new Integer(4);
0691: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0692: .append((Object) null).toString());
0693: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
0694: i3).toString());
0695: assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base)
0696: .append("a", (Object) null).toString());
0697: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
0698: .append("a", i3).toString());
0699: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
0700: .append("a", i3).append("b", i4).toString());
0701: assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(
0702: base).append("a", i3, false).toString());
0703: assertEquals(baseStr + "[a=<size=0>]",
0704: new ToStringBuilder(base).append("a", new ArrayList(),
0705: false).toString());
0706: assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base)
0707: .append("a", new ArrayList(), true).toString());
0708: assertEquals(baseStr + "[a=<size=0>]",
0709: new ToStringBuilder(base).append("a", new HashMap(),
0710: false).toString());
0711: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
0712: .append("a", new HashMap(), true).toString());
0713: assertEquals(baseStr + "[a=<size=0>]",
0714: new ToStringBuilder(base).append("a",
0715: (Object) new String[0], false).toString());
0716: assertEquals(baseStr + "[a={}]", new ToStringBuilder(base)
0717: .append("a", (Object) new String[0], true).toString());
0718: }
0719:
0720: public void testLong() {
0721: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
0722: 3L).toString());
0723: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
0724: .append("a", 3L).toString());
0725: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
0726: .append("a", 3L).append("b", 4L).toString());
0727: }
0728:
0729: public void testInt() {
0730: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
0731: (int) 3).toString());
0732: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
0733: .append("a", (int) 3).toString());
0734: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
0735: .append("a", (int) 3).append("b", (int) 4).toString());
0736: }
0737:
0738: public void testShort() {
0739: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
0740: (short) 3).toString());
0741: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
0742: .append("a", (short) 3).toString());
0743: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
0744: .append("a", (short) 3).append("b", (short) 4)
0745: .toString());
0746: }
0747:
0748: public void testChar() {
0749: assertEquals(baseStr + "[A]", new ToStringBuilder(base).append(
0750: (char) 65).toString());
0751: assertEquals(baseStr + "[a=A]", new ToStringBuilder(base)
0752: .append("a", (char) 65).toString());
0753: assertEquals(baseStr + "[a=A,b=B]", new ToStringBuilder(base)
0754: .append("a", (char) 65).append("b", (char) 66)
0755: .toString());
0756: }
0757:
0758: public void testByte() {
0759: assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(
0760: (byte) 3).toString());
0761: assertEquals(baseStr + "[a=3]", new ToStringBuilder(base)
0762: .append("a", (byte) 3).toString());
0763: assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base)
0764: .append("a", (byte) 3).append("b", (byte) 4).toString());
0765: }
0766:
0767: public void testDouble() {
0768: assertEquals(baseStr + "[3.2]", new ToStringBuilder(base)
0769: .append((double) 3.2).toString());
0770: assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base)
0771: .append("a", (double) 3.2).toString());
0772: assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(
0773: base).append("a", (double) 3.2).append("b",
0774: (double) 4.3).toString());
0775: }
0776:
0777: public void testFloat() {
0778: assertEquals(baseStr + "[3.2]", new ToStringBuilder(base)
0779: .append((float) 3.2).toString());
0780: assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base)
0781: .append("a", (float) 3.2).toString());
0782: assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(
0783: base).append("a", (float) 3.2).append("b", (float) 4.3)
0784: .toString());
0785: }
0786:
0787: public void testBoolean() {
0788: assertEquals(baseStr + "[true]", new ToStringBuilder(base)
0789: .append(true).toString());
0790: assertEquals(baseStr + "[a=true]", new ToStringBuilder(base)
0791: .append("a", true).toString());
0792: assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(
0793: base).append("a", true).append("b", false).toString());
0794: }
0795:
0796: public void testObjectArray() {
0797: Object[] array = new Object[] { null, base, new int[] { 3, 6 } };
0798: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
0799: new ToStringBuilder(base).append(array).toString());
0800: assertEquals(baseStr + "[{<null>,5,{3,6}}]",
0801: new ToStringBuilder(base).append((Object) array)
0802: .toString());
0803: array = null;
0804: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0805: .append(array).toString());
0806: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0807: .append((Object) array).toString());
0808: }
0809:
0810: public void testLongArray() {
0811: long[] array = new long[] { 1, 2, -3, 4 };
0812: assertEquals(baseStr + "[{1,2,-3,4}]",
0813: new ToStringBuilder(base).append(array).toString());
0814: assertEquals(baseStr + "[{1,2,-3,4}]",
0815: new ToStringBuilder(base).append((Object) array)
0816: .toString());
0817: array = null;
0818: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0819: .append(array).toString());
0820: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0821: .append((Object) array).toString());
0822: }
0823:
0824: public void testIntArray() {
0825: int[] array = new int[] { 1, 2, -3, 4 };
0826: assertEquals(baseStr + "[{1,2,-3,4}]",
0827: new ToStringBuilder(base).append(array).toString());
0828: assertEquals(baseStr + "[{1,2,-3,4}]",
0829: new ToStringBuilder(base).append((Object) array)
0830: .toString());
0831: array = null;
0832: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0833: .append(array).toString());
0834: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0835: .append((Object) array).toString());
0836: }
0837:
0838: public void testShortArray() {
0839: short[] array = new short[] { 1, 2, -3, 4 };
0840: assertEquals(baseStr + "[{1,2,-3,4}]",
0841: new ToStringBuilder(base).append(array).toString());
0842: assertEquals(baseStr + "[{1,2,-3,4}]",
0843: new ToStringBuilder(base).append((Object) array)
0844: .toString());
0845: array = null;
0846: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0847: .append(array).toString());
0848: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0849: .append((Object) array).toString());
0850: }
0851:
0852: public void testByteArray() {
0853: byte[] array = new byte[] { 1, 2, -3, 4 };
0854: assertEquals(baseStr + "[{1,2,-3,4}]",
0855: new ToStringBuilder(base).append(array).toString());
0856: assertEquals(baseStr + "[{1,2,-3,4}]",
0857: new ToStringBuilder(base).append((Object) array)
0858: .toString());
0859: array = null;
0860: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0861: .append(array).toString());
0862: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0863: .append((Object) array).toString());
0864: }
0865:
0866: public void testCharArray() {
0867: char[] array = new char[] { 'A', '2', '_', 'D' };
0868: assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base)
0869: .append(array).toString());
0870: assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base)
0871: .append((Object) array).toString());
0872: array = null;
0873: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0874: .append(array).toString());
0875: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0876: .append((Object) array).toString());
0877: }
0878:
0879: public void testDoubleArray() {
0880: double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 };
0881: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0882: new ToStringBuilder(base).append(array).toString());
0883: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0884: new ToStringBuilder(base).append((Object) array)
0885: .toString());
0886: array = null;
0887: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0888: .append(array).toString());
0889: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0890: .append((Object) array).toString());
0891: }
0892:
0893: public void testFloatArray() {
0894: float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f };
0895: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0896: new ToStringBuilder(base).append(array).toString());
0897: assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]",
0898: new ToStringBuilder(base).append((Object) array)
0899: .toString());
0900: array = null;
0901: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0902: .append(array).toString());
0903: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0904: .append((Object) array).toString());
0905: }
0906:
0907: public void testBooleanArray() {
0908: boolean[] array = new boolean[] { true, false, false };
0909: assertEquals(baseStr + "[{true,false,false}]",
0910: new ToStringBuilder(base).append(array).toString());
0911: assertEquals(baseStr + "[{true,false,false}]",
0912: new ToStringBuilder(base).append((Object) array)
0913: .toString());
0914: array = null;
0915: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0916: .append(array).toString());
0917: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0918: .append((Object) array).toString());
0919: }
0920:
0921: public void testLongArrayArray() {
0922: long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
0923: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0924: new ToStringBuilder(base).append(array).toString());
0925: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0926: new ToStringBuilder(base).append((Object) array)
0927: .toString());
0928: array = null;
0929: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0930: .append(array).toString());
0931: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0932: .append((Object) array).toString());
0933: }
0934:
0935: public void testIntArrayArray() {
0936: int[][] array = new int[][] { { 1, 2 }, null, { 5 } };
0937: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0938: new ToStringBuilder(base).append(array).toString());
0939: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0940: new ToStringBuilder(base).append((Object) array)
0941: .toString());
0942: array = null;
0943: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0944: .append(array).toString());
0945: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0946: .append((Object) array).toString());
0947: }
0948:
0949: public void testShortArrayArray() {
0950: short[][] array = new short[][] { { 1, 2 }, null, { 5 } };
0951: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0952: new ToStringBuilder(base).append(array).toString());
0953: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0954: new ToStringBuilder(base).append((Object) array)
0955: .toString());
0956: array = null;
0957: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0958: .append(array).toString());
0959: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0960: .append((Object) array).toString());
0961: }
0962:
0963: public void testByteArrayArray() {
0964: byte[][] array = new byte[][] { { 1, 2 }, null, { 5 } };
0965: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0966: new ToStringBuilder(base).append(array).toString());
0967: assertEquals(baseStr + "[{{1,2},<null>,{5}}]",
0968: new ToStringBuilder(base).append((Object) array)
0969: .toString());
0970: array = null;
0971: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0972: .append(array).toString());
0973: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0974: .append((Object) array).toString());
0975: }
0976:
0977: public void testCharArrayArray() {
0978: char[][] array = new char[][] { { 'A', 'B' }, null, { 'p' } };
0979: assertEquals(baseStr + "[{{A,B},<null>,{p}}]",
0980: new ToStringBuilder(base).append(array).toString());
0981: assertEquals(baseStr + "[{{A,B},<null>,{p}}]",
0982: new ToStringBuilder(base).append((Object) array)
0983: .toString());
0984: array = null;
0985: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0986: .append(array).toString());
0987: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
0988: .append((Object) array).toString());
0989: }
0990:
0991: public void testDoubleArrayArray() {
0992: double[][] array = new double[][] { { 1.0, 2.29686 }, null,
0993: { Double.NaN } };
0994: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
0995: new ToStringBuilder(base).append(array).toString());
0996: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
0997: new ToStringBuilder(base).append((Object) array)
0998: .toString());
0999: array = null;
1000: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1001: .append(array).toString());
1002: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1003: .append((Object) array).toString());
1004: }
1005:
1006: public void testFloatArrayArray() {
1007: float[][] array = new float[][] { { 1.0f, 2.29686f }, null,
1008: { Float.NaN } };
1009: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
1010: new ToStringBuilder(base).append(array).toString());
1011: assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]",
1012: new ToStringBuilder(base).append((Object) array)
1013: .toString());
1014: array = null;
1015: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1016: .append(array).toString());
1017: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1018: .append((Object) array).toString());
1019: }
1020:
1021: public void testBooleanArrayArray() {
1022: boolean[][] array = new boolean[][] { { true, false }, null,
1023: { false } };
1024: assertEquals(baseStr + "[{{true,false},<null>,{false}}]",
1025: new ToStringBuilder(base).append(array).toString());
1026: assertEquals(baseStr + "[{{true,false},<null>,{false}}]",
1027: new ToStringBuilder(base).append((Object) array)
1028: .toString());
1029: array = null;
1030: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1031: .append(array).toString());
1032: assertEquals(baseStr + "[<null>]", new ToStringBuilder(base)
1033: .append((Object) array).toString());
1034: }
1035:
1036: public void testObjectCycle() {
1037: ObjectCycle a = new ObjectCycle();
1038: ObjectCycle b = new ObjectCycle();
1039: a.obj = b;
1040: b.obj = a;
1041:
1042: String expected = toBaseString(a) + "[" + toBaseString(b) + "["
1043: + toBaseString(a) + "]]";
1044: assertEquals(expected, a.toString());
1045: validateEmptyToStringStyleRegistry();
1046: }
1047:
1048: static class ObjectCycle {
1049: Object obj;
1050:
1051: public String toString() {
1052: return new ToStringBuilder(this ).append(obj).toString();
1053: }
1054: }
1055:
1056: public void testSimpleReflectionStatics() {
1057: SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture();
1058: assertEquals(this .toBaseString(instance1)
1059: + "[staticString=staticString,staticInt=12345]",
1060: ReflectionToStringBuilder.toString(instance1, null,
1061: false, true,
1062: SimpleReflectionStaticFieldsFixture.class));
1063: assertEquals(this .toBaseString(instance1)
1064: + "[staticString=staticString,staticInt=12345]",
1065: ReflectionToStringBuilder.toString(instance1, null,
1066: true, true,
1067: SimpleReflectionStaticFieldsFixture.class));
1068: assertEquals(this .toBaseString(instance1)
1069: + "[staticString=staticString,staticInt=12345]", this
1070: .toStringWithStatics(instance1, null,
1071: SimpleReflectionStaticFieldsFixture.class));
1072: assertEquals(this .toBaseString(instance1)
1073: + "[staticString=staticString,staticInt=12345]", this
1074: .toStringWithStatics(instance1, null,
1075: SimpleReflectionStaticFieldsFixture.class));
1076: }
1077:
1078: /**
1079: * Tests ReflectionToStringBuilder.toString() for statics.
1080: */
1081: public void testReflectionStatics() {
1082: ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture();
1083: assertEquals(
1084: this .toBaseString(instance1)
1085: + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
1086: ReflectionToStringBuilder.toString(instance1, null,
1087: false, true,
1088: ReflectionStaticFieldsFixture.class));
1089: assertEquals(
1090: this .toBaseString(instance1)
1091: + "[staticString=staticString,staticInt=12345,staticTransientString=staticTransientString,staticTransientInt=54321,instanceString=instanceString,instanceInt=67890,transientString=transientString,transientInt=98765]",
1092: ReflectionToStringBuilder
1093: .toString(instance1, null, true, true,
1094: ReflectionStaticFieldsFixture.class));
1095: assertEquals(
1096: this .toBaseString(instance1)
1097: + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
1098: this .toStringWithStatics(instance1, null,
1099: ReflectionStaticFieldsFixture.class));
1100: assertEquals(
1101: this .toBaseString(instance1)
1102: + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
1103: this .toStringWithStatics(instance1, null,
1104: ReflectionStaticFieldsFixture.class));
1105: }
1106:
1107: /**
1108: * Tests ReflectionToStringBuilder.toString() for statics.
1109: */
1110: public void testInheritedReflectionStatics() {
1111: InheritedReflectionStaticFieldsFixture instance1 = new InheritedReflectionStaticFieldsFixture();
1112: assertEquals(this .toBaseString(instance1)
1113: + "[staticString2=staticString2,staticInt2=67890]",
1114: ReflectionToStringBuilder.toString(instance1, null,
1115: false, true,
1116: InheritedReflectionStaticFieldsFixture.class));
1117: assertEquals(
1118: this .toBaseString(instance1)
1119: + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
1120: ReflectionToStringBuilder.toString(instance1, null,
1121: false, true,
1122: SimpleReflectionStaticFieldsFixture.class));
1123: assertEquals(
1124: this .toBaseString(instance1)
1125: + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
1126: this .toStringWithStatics(instance1, null,
1127: SimpleReflectionStaticFieldsFixture.class));
1128: assertEquals(
1129: this .toBaseString(instance1)
1130: + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
1131: this .toStringWithStatics(instance1, null,
1132: SimpleReflectionStaticFieldsFixture.class));
1133: }
1134:
1135: /**
1136: * <p>This method uses reflection to build a suitable
1137: * <code>toString</code> value which includes static fields.</p>
1138: *
1139: * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
1140: * fields. This means that it will throw a security exception if run
1141: * under a security manager, if the permissions are not set up correctly.
1142: * It is also not as efficient as testing explicitly. </p>
1143: *
1144: * <p>Transient fields are not output.</p>
1145: *
1146: * <p>Superclass fields will be appended up to and including the specified superclass.
1147: * A null superclass is treated as <code>java.lang.Object</code>.</p>
1148: *
1149: * <p>If the style is <code>null</code>, the default
1150: * <code>ToStringStyle</code> is used.</p>
1151: *
1152: * @param object the Object to be output
1153: * @param style the style of the <code>toString</code> to create,
1154: * may be <code>null</code>
1155: * @param reflectUpToClass the superclass to reflect up to (inclusive),
1156: * may be <code>null</code>
1157: * @return the String result
1158: * @throws IllegalArgumentException if the Object is <code>null</code>
1159: */
1160: public Object toStringWithStatics(Object object,
1161: ToStringStyle style, Class reflectUpToClass) {
1162: return ReflectionToStringBuilder.toString(object, style, false,
1163: true, reflectUpToClass);
1164: }
1165:
1166: /**
1167: * Tests ReflectionToStringBuilder.toString() for statics.
1168: */
1169: class ReflectionStaticFieldsFixture {
1170: static final String staticString = "staticString";
1171: static final int staticInt = 12345;
1172: static final transient String staticTransientString = "staticTransientString";
1173: static final transient int staticTransientInt = 54321;
1174: String instanceString = "instanceString";
1175: int instanceInt = 67890;
1176: transient String transientString = "transientString";
1177: transient int transientInt = 98765;
1178: }
1179:
1180: /**
1181: * Test fixture for ReflectionToStringBuilder.toString() for statics.
1182: */
1183: class SimpleReflectionStaticFieldsFixture {
1184: static final String staticString = "staticString";
1185: static final int staticInt = 12345;
1186: }
1187:
1188: /**
1189: * Test fixture for ReflectionToStringBuilder.toString() for statics.
1190: */
1191: class InheritedReflectionStaticFieldsFixture extends
1192: SimpleReflectionStaticFieldsFixture {
1193: static final String staticString2 = "staticString2";
1194: static final int staticInt2 = 67890;
1195: }
1196:
1197: public void testReflectionNull() {
1198: assertEquals("<null>", ReflectionToStringBuilder.toString(null));
1199: }
1200:
1201: }
|