KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > ToStringBuilderTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.builder;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 /**
28  * Unit tests for {@link org.apache.commons.lang.builder.ToStringBuilder}.
29  *
30  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
31  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
32  * @author <a HREF="mailto:alex@apache.org">Alex Chaffee</a>
33  * @version $Id: ToStringBuilderTest.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35 public class ToStringBuilderTest extends TestCase {
36
37     private final Integer JavaDoc base = new Integer JavaDoc(5);
38     private final String JavaDoc baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
39     
40     public ToStringBuilderTest(String JavaDoc name) {
41         super(name);
42     }
43
44     public static void main(String JavaDoc[] args) {
45         TestRunner.run(suite());
46     }
47
48     public static Test suite() {
49         TestSuite suite = new TestSuite(ToStringBuilderTest.class);
50         suite.setName("ToStringBuilder Tests");
51         return suite;
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56     }
57
58     protected void tearDown() throws Exception JavaDoc {
59         super.tearDown();
60     }
61
62     //-----------------------------------------------------------------------
63

64     public void testConstructorEx1() {
65         assertEquals("<null>", new ToStringBuilder(null).toString());
66     }
67
68     public void testConstructorEx2() {
69         assertEquals("<null>", new ToStringBuilder(null, null).toString());
70         new ToStringBuilder(this.base, null).toString();
71     }
72
73     public void testConstructorEx3() {
74         assertEquals("<null>", new ToStringBuilder(null, null, null).toString());
75         new ToStringBuilder(this.base, null, null);
76         new ToStringBuilder(this.base, ToStringStyle.DEFAULT_STYLE, null);
77     }
78
79     public void testGetSetDefault() {
80         try {
81             ToStringBuilder.setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE);
82             assertSame(ToStringStyle.NO_FIELD_NAMES_STYLE, ToStringBuilder.getDefaultStyle());
83         } finally {
84             // reset for other tests
85
ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
86         }
87     }
88
89     public void testSetDefaultEx() {
90         try {
91             ToStringBuilder.setDefaultStyle(null);
92             
93         } catch (IllegalArgumentException JavaDoc ex) {
94             return;
95         }
96         fail();
97     }
98
99     public void testBlank() {
100         assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
101     }
102     
103     /**
104      * Test wrapper for int primitive.
105      */

106     public void testReflectionInteger() {
107         assertEquals(baseStr + "[value=5]", ToStringBuilder.reflectionToString(base));
108     }
109
110     /**
111      * Test wrapper for char primitive.
112      */

113     public void testReflectionCharacter() {
114         Character JavaDoc c = new Character JavaDoc('A');
115         assertEquals(this.toBaseString(c) + "[value=A]", ToStringBuilder.reflectionToString(c));
116     }
117
118     /**
119      * Test wrapper for char boolean.
120      */

121     public void testReflectionBoolean() {
122         Boolean JavaDoc b;
123         b = Boolean.TRUE;
124         assertEquals(this.toBaseString(b) + "[value=true]", ToStringBuilder.reflectionToString(b));
125         b = Boolean.FALSE;
126         assertEquals(this.toBaseString(b) + "[value=false]", ToStringBuilder.reflectionToString(b));
127     }
128
129     /**
130      * Create the same toString() as Object.toString().
131      * @param o the object to create the string for.
132      * @return a String in the Object.toString format.
133      */

134     private String JavaDoc toBaseString(Object JavaDoc o) {
135         return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
136     }
137     
138     // Reflection Array tests
139

140     //
141
// Note on the following line of code repeated in the reflection array tests.
142
//
143
// assertReflectionArray("<null>", array);
144
//
145
// The expected value is not baseStr + "[<null>]" since array==null and is typed as Object.
146
// The null array does not carry array type information.
147
// If we added a primitive array type constructor and pile of associated methods,
148
// then type declaring type information could be carried forward. IMHO, null is null.
149
//
150
// Gary Gregory - 2003-03-12 - ggregory@seagullsw.com
151
//
152

153     public void assertReflectionArray(String JavaDoc expected, Object JavaDoc actual) {
154         if (actual == null) {
155             // Until ToStringBuilder supports null objects.
156
return;
157         }
158         assertEquals(expected, ToStringBuilder.reflectionToString(actual));
159         assertEquals(expected, ToStringBuilder.reflectionToString(actual, null));
160         assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, true));
161         assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, false));
162     }
163
164     public void testReflectionObjectArray() {
165         Object JavaDoc[] array = new Object JavaDoc[] { null, base, new int[] { 3, 6 } };
166         String JavaDoc baseStr = this.toBaseString(array);
167         assertEquals(baseStr + "[{<null>,5,{3,6}}]", ToStringBuilder.reflectionToString(array));
168         array = null;
169         assertReflectionArray("<null>", array);
170         this.validateEmptyReflectionRegistry();
171     }
172
173     public void testReflectionLongArray() {
174         long[] array = new long[] { 1, 2, -3, 4 };
175         String JavaDoc baseStr = this.toBaseString(array);
176         assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array));
177         array = null;
178         assertReflectionArray("<null>", array);
179         this.validateEmptyReflectionRegistry();
180     }
181
182     public void testReflectionIntArray() {
183         int[] array = new int[] { 1, 2, -3, 4 };
184         String JavaDoc baseStr = this.toBaseString(array);
185         assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array));
186         array = null;
187         assertReflectionArray("<null>", array);
188         this.validateEmptyReflectionRegistry();
189     }
190
191     public void testReflectionShortArray() {
192         short[] array = new short[] { 1, 2, -3, 4 };
193         String JavaDoc baseStr = this.toBaseString(array);
194         assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array));
195         array = null;
196         assertReflectionArray("<null>", array);
197         this.validateEmptyReflectionRegistry();
198     }
199
200     public void testReflectionyteArray() {
201         byte[] array = new byte[] { 1, 2, -3, 4 };
202         String JavaDoc baseStr = this.toBaseString(array);
203         assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array));
204         array = null;
205         assertReflectionArray("<null>", array);
206         this.validateEmptyReflectionRegistry();
207     }
208
209     public void testReflectionCharArray() {
210         char[] array = new char[] { 'A', '2', '_', 'D' };
211         String JavaDoc baseStr = this.toBaseString(array);
212         assertEquals(baseStr + "[{A,2,_,D}]", ToStringBuilder.reflectionToString(array));
213         array = null;
214         assertReflectionArray("<null>", array);
215         this.validateEmptyReflectionRegistry();
216     }
217
218     public void testReflectionDoubleArray() {
219         double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 };
220         String JavaDoc baseStr = this.toBaseString(array);
221         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array));
222         array = null;
223         assertReflectionArray("<null>", array);
224         this.validateEmptyReflectionRegistry();
225     }
226
227     public void testReflectionFloatArray() {
228         float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f };
229         String JavaDoc baseStr = this.toBaseString(array);
230         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array));
231         array = null;
232         assertReflectionArray("<null>", array);
233         this.validateEmptyReflectionRegistry();
234     }
235
236     public void testReflectionBooleanArray() {
237         boolean[] array = new boolean[] { true, false, false };
238         String JavaDoc baseStr = this.toBaseString(array);
239         assertEquals(baseStr + "[{true,false,false}]", ToStringBuilder.reflectionToString(array));
240         array = null;
241         assertReflectionArray("<null>", array);
242         this.validateEmptyReflectionRegistry();
243     }
244     
245     // Reflection Array Array tests
246

247     public void testReflectionFloatArrayArray() {
248         float[][] array = new float[][] { { 1.0f, 2.29686f }, null, { Float.NaN } };
249         String JavaDoc baseStr = this.toBaseString(array);
250         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", ToStringBuilder.reflectionToString(array));
251         array = null;
252         assertReflectionArray("<null>", array);
253         this.validateEmptyReflectionRegistry();
254     }
255
256
257     public void testReflectionLongArrayArray() {
258         long[][] array = new long[][] { { 1, 2 }, null, { 5 } };
259         String JavaDoc baseStr = this.toBaseString(array);
260         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array));
261         array = null;
262         assertReflectionArray("<null>", array);
263         this.validateEmptyReflectionRegistry();
264     }
265
266     public void testReflectionIntArrayArray() {
267         int[][] array = new int[][] { { 1, 2 }, null, { 5 } };
268         String JavaDoc baseStr = this.toBaseString(array);
269         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array));
270         array = null;
271         assertReflectionArray("<null>", array);
272         this.validateEmptyReflectionRegistry();
273     }
274
275     public void testReflectionhortArrayArray() {
276         short[][] array = new short[][] { { 1, 2 }, null, { 5 } };
277         String JavaDoc baseStr = this.toBaseString(array);
278         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array));
279         array = null;
280         assertReflectionArray("<null>", array);
281         this.validateEmptyReflectionRegistry();
282     }
283
284     public void testReflectionByteArrayArray() {
285         byte[][] array = new byte[][] { { 1, 2 }, null, { 5 } };
286         String JavaDoc baseStr = this.toBaseString(array);
287         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array));
288         array = null;
289         assertReflectionArray("<null>", array);
290         this.validateEmptyReflectionRegistry();
291     }
292
293     public void testReflectionCharArrayArray() {
294         char[][] array = new char[][] { { 'A', 'B' }, null, { 'p' } };
295         String JavaDoc baseStr = this.toBaseString(array);
296         assertEquals(baseStr + "[{{A,B},<null>,{p}}]", ToStringBuilder.reflectionToString(array));
297         array = null;
298         assertReflectionArray("<null>", array);
299         this.validateEmptyReflectionRegistry();
300     }
301
302     public void testReflectionDoubleArrayArray() {
303         double[][] array = new double[][] { { 1.0, 2.29686 }, null, { Double.NaN } };
304         String JavaDoc baseStr = this.toBaseString(array);
305         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", ToStringBuilder.reflectionToString(array));
306         array = null;
307         assertReflectionArray("<null>", array);
308         this.validateEmptyReflectionRegistry();
309     }
310
311     public void testReflectionBooleanArrayArray() {
312         boolean[][] array = new boolean[][] { { true, false }, null, { false } };
313         String JavaDoc baseStr = this.toBaseString(array);
314         assertEquals(baseStr + "[{{true,false},<null>,{false}}]", ToStringBuilder.reflectionToString(array));
315         assertEquals(baseStr + "[{{true,false},<null>,{false}}]", ToStringBuilder.reflectionToString(array));
316         array = null;
317         assertReflectionArray("<null>", array);
318         this.validateEmptyReflectionRegistry();
319     }
320     
321     // Reflection hierarchy tests
322

323     public void testReflectionHierarchyArrayList() {
324         List JavaDoc base = new ArrayList JavaDoc();
325         String JavaDoc baseStr = this.toBaseString(base);
326         assertEquals(baseStr + "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]", ToStringBuilder.reflectionToString(base, null, true));
327         assertEquals(baseStr + "[size=0]", ToStringBuilder.reflectionToString(base, null, false));
328         this.validateEmptyReflectionRegistry();
329     }
330
331     public void testReflectionHierarchy() {
332         ReflectionTestFixtureA baseA = new ReflectionTestFixtureA();
333         String JavaDoc baseStr = this.toBaseString(baseA);
334         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA));
335         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null));
336         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false));
337         assertEquals(baseStr + "[a=a,transientA=t]", ToStringBuilder.reflectionToString(baseA, null, true));
338         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, null));
339         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, Object JavaDoc.class));
340         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, List JavaDoc.class));
341         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, ReflectionTestFixtureA.class));
342         
343         ReflectionTestFixtureB baseB = new ReflectionTestFixtureB();
344         baseStr = this.toBaseString(baseB);
345         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB));
346         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB));
347         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null));
348         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false));
349         assertEquals(baseStr + "[b=b,transientB=t,a=a,transientA=t]", ToStringBuilder.reflectionToString(baseB, null, true));
350         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, null));
351         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, Object JavaDoc.class));
352         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, List JavaDoc.class));
353         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureA.class));
354         assertEquals(baseStr + "[b=b]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureB.class));
355         this.validateEmptyReflectionRegistry();
356     }
357
358     static class ReflectionTestFixtureA {
359         private char a='a';
360         private transient char transientA='t';
361     }
362
363     static class ReflectionTestFixtureB extends ReflectionTestFixtureA {
364         private char b='b';
365         private transient char transientB='t';
366     }
367
368     public void testInnerClassReflection() {
369         Outer outer = new Outer();
370         assertEquals(toBaseString(outer) + "[inner=" + toBaseString(outer.inner) + "[]]", outer.toString());
371     }
372     
373     static class Outer {
374         Inner inner = new Inner();
375         class Inner {
376             public String JavaDoc toString() {
377                 return ToStringBuilder.reflectionToString(this);
378             }
379         }
380         public String JavaDoc toString() {
381             return ToStringBuilder.reflectionToString(this);
382         }
383     }
384     
385     // Reflection cycle tests
386

387     /**
388      * Test an array element pointing to its container.
389      */

390     public void testReflectionArrayCycle() throws Exception JavaDoc {
391         Object JavaDoc[] objects = new Object JavaDoc[1];
392         objects[0] = objects;
393         assertEquals(
394             this.toBaseString(objects) + "[{" + this.toBaseString(objects) + "}]",
395             ToStringBuilder.reflectionToString(objects));
396         this.validateEmptyReflectionRegistry();
397     }
398
399     /**
400      * Test an array element pointing to its container.
401      */

402     public void testReflectionArrayCycleLevel2() throws Exception JavaDoc {
403         Object JavaDoc[] objects = new Object JavaDoc[1];
404         Object JavaDoc[] objectsLevel2 = new Object JavaDoc[1];
405         objects[0] = objectsLevel2;
406         objectsLevel2[0] = (Object JavaDoc) objects;
407         assertEquals(
408             this.toBaseString(objects) + "[{{" + this.toBaseString(objects) + "}}]",
409             ToStringBuilder.reflectionToString(objects));
410         assertEquals(
411             this.toBaseString(objectsLevel2) + "[{{" + this.toBaseString(objectsLevel2) + "}}]",
412             ToStringBuilder.reflectionToString(objectsLevel2));
413         this.validateEmptyReflectionRegistry();
414     }
415
416     public void testReflectionArrayArrayCycle() throws Exception JavaDoc {
417         Object JavaDoc[][] objects = new Object JavaDoc[2][2];
418         objects[0][0] = objects;
419         objects[0][1] = objects;
420         objects[1][0] = objects;
421         objects[1][1] = objects;
422         String JavaDoc basicToString = this.toBaseString(objects);
423         assertEquals(
424             basicToString
425                 + "[{{"
426                 + basicToString
427                 + ","
428                 + basicToString
429                 + "},{"
430                 + basicToString
431                 + ","
432                 + basicToString
433                 + "}}]",
434             ToStringBuilder.reflectionToString(objects));
435         this.validateEmptyReflectionRegistry();
436     }
437
438     /**
439      * A reflection test fixture.
440      */

441     static class ReflectionTestCycleA {
442         ReflectionTestCycleB b;
443
444         public String JavaDoc toString() {
445             return ToStringBuilder.reflectionToString(this);
446         }
447     }
448
449     /**
450      * A reflection test fixture.
451      */

452     static class ReflectionTestCycleB {
453         ReflectionTestCycleA a;
454
455         public String JavaDoc toString() {
456             return ToStringBuilder.reflectionToString(this);
457         }
458     }
459
460     /**
461      * A reflection test fixture.
462      */

463     static class SimpleReflectionTestFixture {
464         Object JavaDoc o;
465
466         public SimpleReflectionTestFixture() {
467         }
468
469         public SimpleReflectionTestFixture(Object JavaDoc o) {
470             this.o = o;
471         }
472
473         public String JavaDoc toString() {
474             return ToStringBuilder.reflectionToString(this);
475         }
476     }
477
478     private static class SelfInstanceVarReflectionTestFixture {
479         private SelfInstanceVarReflectionTestFixture typeIsSelf;
480
481         public SelfInstanceVarReflectionTestFixture() {
482             this.typeIsSelf = this;
483         }
484
485         public String JavaDoc toString() {
486             return ToStringBuilder.reflectionToString(this);
487         }
488       }
489     
490     private static class SelfInstanceTwoVarsReflectionTestFixture {
491         private SelfInstanceTwoVarsReflectionTestFixture typeIsSelf;
492         private String JavaDoc otherType = "The Other Type";
493
494         public SelfInstanceTwoVarsReflectionTestFixture() {
495             this.typeIsSelf = this;
496         }
497         
498         public String JavaDoc getOtherType(){
499             return this.otherType;
500         }
501
502         public String JavaDoc toString() {
503             return ToStringBuilder.reflectionToString(this);
504         }
505       }
506     
507     
508     /**
509      * Test an Object pointing to itself, the simplest test.
510      *
511      * @throws Exception
512      */

513     public void testSimpleReflectionObjectCycle() throws Exception JavaDoc {
514         SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture();
515         simple.o = simple;
516         assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty());
517         assertEquals(this.toBaseString(simple) + "[o=" + this.toBaseString(simple) + "]", simple.toString());
518         this.validateEmptyReflectionRegistry();
519     }
520
521     /**
522      * Test a class that defines an ivar pointing to itself.
523      *
524      * @throws Exception
525      */

526     public void testSelfInstanceVarReflectionObjectCycle() throws Exception JavaDoc {
527         SelfInstanceVarReflectionTestFixture test = new SelfInstanceVarReflectionTestFixture();
528         assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty());
529         assertEquals(this.toBaseString(test) + "[typeIsSelf=" + this.toBaseString(test) + "]", test.toString());
530         this.validateEmptyReflectionRegistry();
531     }
532     
533     /**
534      * Test a class that defines an ivar pointing to itself. This test was
535      * created to show that handling cyclical object resulted in a missing endFieldSeparator call.
536      *
537      * @throws Exception
538      */

539     public void testSelfInstanceTwoVarsReflectionObjectCycle() throws Exception JavaDoc {
540         SelfInstanceTwoVarsReflectionTestFixture test = new SelfInstanceTwoVarsReflectionTestFixture();
541         assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty());
542         assertEquals(this.toBaseString(test) + "[typeIsSelf=" + this.toBaseString(test) + ",otherType=" + test.getOtherType().toString() + "]", test.toString());
543         this.validateEmptyReflectionRegistry();
544     }
545     
546
547     /**
548      * Test Objects pointing to each other.
549      *
550      * @throws Exception
551      */

552     public void testReflectionObjectCycle() throws Exception JavaDoc {
553         ReflectionTestCycleA a = new ReflectionTestCycleA();
554         ReflectionTestCycleB b = new ReflectionTestCycleB();
555         a.b = b;
556         b.a = a;
557         assertEquals(
558             this.toBaseString(a) + "[b=" + this.toBaseString(b) + "[a=" + this.toBaseString(a) + "]]",
559             a.toString());
560         this.validateEmptyReflectionRegistry();
561     }
562
563     /**
564      * Test a nasty combination of arrays and Objects pointing to each other.
565      * objects[0] -> SimpleReflectionTestFixture[ o -> objects ]
566      *
567      * @throws Exception
568      */

569     public void testReflectionArrayAndObjectCycle() throws Exception JavaDoc {
570         Object JavaDoc[] objects = new Object JavaDoc[1];
571         SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(objects);
572         objects[0] = (Object JavaDoc) simple;
573         assertEquals(
574             this.toBaseString(objects)
575                 + "[{"
576                 + this.toBaseString(simple)
577                 + "[o="
578                 + this.toBaseString(objects)
579                 + "]"
580                 + "}]",
581             ToStringBuilder.reflectionToString(objects));
582         assertEquals(
583             this.toBaseString(simple)
584                 + "[o={"
585                 + this.toBaseString(simple)
586                 + "}]",
587             ToStringBuilder.reflectionToString(simple));
588         this.validateEmptyReflectionRegistry();
589     }
590         
591     void validateEmptyReflectionRegistry() {
592         assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty());
593     }
594     // End: Reflection cycle tests
595

596     public void testAppendSuper() {
597         assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
598         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
599         
600         assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString());
601         assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString());
602         assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
603     }
604     
605     public void testAppendToString() {
606         assertEquals(baseStr + "[]", new ToStringBuilder(base).appendToString("Integer@8888[]").toString());
607         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendToString("Integer@8888[<null>]").toString());
608         
609         assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendToString("Integer@8888[]").append("a", "hello").toString());
610         assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendToString("Integer@8888[<null>]").append("a", "hello").toString());
611         assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendToString(null).append("a", "hello").toString());
612     }
613     
614     public void testObject() {
615         Integer JavaDoc i3 = new Integer JavaDoc(3);
616         Integer JavaDoc i4 = new Integer JavaDoc(4);
617         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) null).toString());
618         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
619         assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object JavaDoc) null).toString());
620         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", i3).toString());
621         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
622         assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString());
623         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), false).toString());
624         assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList JavaDoc(), true).toString());
625         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), false).toString());
626         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap JavaDoc(), true).toString());
627         assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], false).toString());
628         assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object JavaDoc) new String JavaDoc[0], true).toString());
629     }
630
631     public void testLong() {
632         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString());
633         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString());
634         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString());
635     }
636
637     public void testInt() {
638         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((int) 3).toString());
639         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (int) 3).toString());
640         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (int) 3).append("b", (int) 4).toString());
641     }
642
643     public void testShort() {
644         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((short) 3).toString());
645         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (short) 3).toString());
646         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (short) 3).append("b", (short) 4).toString());
647     }
648
649     public void testChar() {
650         assertEquals(baseStr + "[A]", new ToStringBuilder(base).append((char) 65).toString());
651         assertEquals(baseStr + "[a=A]", new ToStringBuilder(base).append("a", (char) 65).toString());
652         assertEquals(baseStr + "[a=A,b=B]", new ToStringBuilder(base).append("a", (char) 65).append("b", (char) 66).toString());
653     }
654
655     public void testByte() {
656         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((byte) 3).toString());
657         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (byte) 3).toString());
658         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (byte) 3).append("b", (byte) 4).toString());
659     }
660
661     public void testDouble() {
662         assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((double) 3.2).toString());
663         assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (double) 3.2).toString());
664         assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (double) 3.2).append("b", (double) 4.3).toString());
665     }
666
667     public void testFloat() {
668         assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((float) 3.2).toString());
669         assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (float) 3.2).toString());
670         assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (float) 3.2).append("b", (float) 4.3).toString());
671     }
672
673     public void testBoolean() {
674         assertEquals(baseStr + "[true]", new ToStringBuilder(base).append(true).toString());
675         assertEquals(baseStr + "[a=true]", new ToStringBuilder(base).append("a", true).toString());
676         assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(base).append("a", true).append("b", false).toString());
677     }
678
679
680     public void testObjectArray() {
681         Object JavaDoc[] array = new Object JavaDoc[] {null, base, new int[] {3, 6}};
682         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString());
683         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
684         array = null;
685         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
686         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
687     }
688
689     public void testLongArray() {
690         long[] array = new long[] {1, 2, -3, 4};
691         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
692         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
693         array = null;
694         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
695         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
696     }
697
698     public void testIntArray() {
699         int[] array = new int[] {1, 2, -3, 4};
700         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
701         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
702         array = null;
703         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
704         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
705     }
706
707     public void testShortArray() {
708         short[] array = new short[] {1, 2, -3, 4};
709         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
710         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
711         array = null;
712         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
713         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
714     }
715     
716     public void testByteArray() {
717         byte[] array = new byte[] {1, 2, -3, 4};
718         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
719         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
720         array = null;
721         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
722         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
723     }
724
725     public void testCharArray() {
726         char[] array = new char[] {'A', '2', '_', 'D'};
727         assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(array).toString());
728         assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
729         array = null;
730         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
731         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
732     }
733
734     public void testDoubleArray() {
735         double[] array = new double[] {1.0, 2.9876, -3.00001, 4.3};
736         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString());
737         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
738         array = null;
739         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
740         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
741     }
742
743     public void testFloatArray() {
744         float[] array = new float[] {1.0f, 2.9876f, -3.00001f, 4.3f};
745         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString());
746         assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
747         array = null;
748         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
749         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
750     }
751     
752     public void testBooleanArray() {
753         boolean[] array = new boolean[] {true, false, false};
754         assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(array).toString());
755         assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
756         array = null;
757         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
758         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
759     }
760
761     public void testLongArrayArray() {
762         long[][] array = new long[][] {{1, 2}, null, {5}};
763         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
764         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
765         array = null;
766         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
767         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
768     }
769
770     public void testIntArrayArray() {
771         int[][] array = new int[][] {{1, 2}, null, {5}};
772         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
773         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
774         array = null;
775         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
776         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
777     }
778
779     public void testShortArrayArray() {
780         short[][] array = new short[][] {{1, 2}, null, {5}};
781         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
782         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
783         array = null;
784         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
785         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
786     }
787
788     public void testByteArrayArray() {
789         byte[][] array = new byte[][] {{1, 2}, null, {5}};
790         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
791         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
792         array = null;
793         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
794         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
795     }
796
797     public void testCharArrayArray() {
798         char[][] array = new char[][] {{'A', 'B'}, null, {'p'}};
799         assertEquals(baseStr + "[{{A,B},<null>,{p}}]", new ToStringBuilder(base).append(array).toString());
800         assertEquals(baseStr + "[{{A,B},<null>,{p}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
801         array = null;
802         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
803         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
804     }
805
806     public void testDoubleArrayArray() {
807         double[][] array = new double[][] {{1.0, 2.29686}, null, {Double.NaN}};
808         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append(array).toString());
809         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
810         array = null;
811         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
812         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
813     }
814
815     public void testFloatArrayArray() {
816         float[][] array = new float[][] {{1.0f, 2.29686f}, null, {Float.NaN}};
817         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append(array).toString());
818         assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
819         array = null;
820         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
821         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
822     }
823
824     public void testBooleanArrayArray() {
825         boolean[][] array = new boolean[][] {{true, false}, null, {false}};
826         assertEquals(baseStr + "[{{true,false},<null>,{false}}]", new ToStringBuilder(base).append(array).toString());
827         assertEquals(baseStr + "[{{true,false},<null>,{false}}]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
828         array = null;
829         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
830         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object JavaDoc) array).toString());
831     }
832
833     public void testSimpleReflectionStatics() {
834         SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture();
835         assertEquals(
836             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]",
837             ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
838         assertEquals(
839             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]",
840             ReflectionToStringBuilder.toString(instance1, null, true, true, SimpleReflectionStaticFieldsFixture.class));
841         assertEquals(
842             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]",
843             this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
844         assertEquals(
845             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]",
846             this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
847     }
848
849     /**
850      * Tests ReflectionToStringBuilder.toString() for statics.
851      */

852     public void testReflectionStatics() {
853         ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture();
854         assertEquals(
855             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
856             ReflectionToStringBuilder.toString(instance1, null, false, true, ReflectionStaticFieldsFixture.class));
857         assertEquals(
858             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,staticTransientString=staticTransientString,staticTransientInt=54321,instanceString=instanceString,instanceInt=67890,transientString=transientString,transientInt=98765]",
859             ReflectionToStringBuilder.toString(instance1, null, true, true, ReflectionStaticFieldsFixture.class));
860         assertEquals(
861             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
862             this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
863         assertEquals(
864             this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]",
865             this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
866     }
867
868     /**
869      * Tests ReflectionToStringBuilder.toString() for statics.
870      */

871     public void testInheritedReflectionStatics() {
872         InheritedReflectionStaticFieldsFixture instance1 = new InheritedReflectionStaticFieldsFixture();
873         assertEquals(
874             this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890]",
875             ReflectionToStringBuilder.toString(instance1, null, false, true, InheritedReflectionStaticFieldsFixture.class));
876         assertEquals(
877             this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
878             ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
879         assertEquals(
880             this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
881             this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
882         assertEquals(
883             this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]",
884             this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
885     }
886
887     /**
888      * <p>This method uses reflection to build a suitable
889      * <code>toString</code> value which includes static fields.</p>
890      *
891      * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
892      * fields. This means that it will throw a security exception if run
893      * under a security manager, if the permissions are not set up correctly.
894      * It is also not as efficient as testing explicitly. </p>
895      *
896      * <p>Transient fields are not output.</p>
897      *
898      * <p>Superclass fields will be appended up to and including the specified superclass.
899      * A null superclass is treated as <code>java.lang.Object</code>.</p>
900      *
901      * <p>If the style is <code>null</code>, the default
902      * <code>ToStringStyle</code> is used.</p>
903      *
904      * @param object the Object to be output
905      * @param style the style of the <code>toString</code> to create,
906      * may be <code>null</code>
907      * @param reflectUpToClass the superclass to reflect up to (inclusive),
908      * may be <code>null</code>
909      * @return the String result
910      * @throws IllegalArgumentException if the Object is <code>null</code>
911      */

912     public Object JavaDoc toStringWithStatics(Object JavaDoc object, ToStringStyle style, Class JavaDoc reflectUpToClass) {
913         return ReflectionToStringBuilder.toString(object, style, false, true, reflectUpToClass);
914     }
915
916     /**
917      * Tests ReflectionToStringBuilder.toString() for statics.
918      */

919     class ReflectionStaticFieldsFixture {
920         static final String JavaDoc staticString = "staticString";
921         static final int staticInt = 12345;
922         static final transient String JavaDoc staticTransientString = "staticTransientString";
923         static final transient int staticTransientInt = 54321;
924         String JavaDoc instanceString = "instanceString";
925         int instanceInt = 67890;
926         transient String JavaDoc transientString = "transientString";
927         transient int transientInt = 98765;
928     }
929
930     /**
931      * Test fixture for ReflectionToStringBuilder.toString() for statics.
932      */

933     class SimpleReflectionStaticFieldsFixture {
934         static final String JavaDoc staticString = "staticString";
935         static final int staticInt = 12345;
936     }
937
938     /**
939      * Test fixture for ReflectionToStringBuilder.toString() for statics.
940      */

941     class InheritedReflectionStaticFieldsFixture extends SimpleReflectionStaticFieldsFixture {
942         static final String JavaDoc staticString2 = "staticString2";
943         static final int staticInt2 = 67890;
944     }
945
946     public void testReflectionNull() {
947         assertEquals("<null>", ReflectionToStringBuilder.toString(null));
948     }
949 }
950
Popular Tags