From 2ba51c13fbbaa539c8db5448fc0ca2429d65c239 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 20 Mar 2003 05:33:35 +0000 Subject: [PATCH] Apply patch for bug 17250 (http://issues.apache.org/bugzilla/show_bug.cgi?id=17250) with some additional minor improvements. Added tests for reflection methods with array arguments. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137263 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang/builder/ToStringBuilderTest.java | 167 +++++++++++++++++- 1 file changed, 165 insertions(+), 2 deletions(-) diff --git a/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java b/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java index d4d7df9e5..8e0e56cc2 100644 --- a/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java +++ b/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java @@ -66,7 +66,7 @@ import junit.textui.TestRunner; * Unit tests {@link org.apache.commons.lang.ToStringBuilder}. * * @author Stephen Colebourne - * @version $Id: ToStringBuilderTest.java,v 1.4 2003/01/19 18:49:05 scolebourne Exp $ + * @version $Id: ToStringBuilderTest.java,v 1.5 2003/03/20 05:33:35 ggregory Exp $ */ public class ToStringBuilderTest extends TestCase { @@ -171,6 +171,170 @@ public class ToStringBuilderTest extends TestCase { private String toBaseString(Object o) { return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o)); } + + // Reflection Array tests + + // + // Note on the following line of code repeated in the reflection array tests. + // + // assertReflectionArray("", array); + // + // The expected value is not baseStr + "[]" since array==null and is typed as Object. + // The null array does not carry array type information. + // If we added a primitive array type constructor and pile of associated methods, + // then type declaring type information could be carried forward. IMHO, null is null. + // + // Gary Gregory - 2003-03-12 - ggregory@seagullsw.com + // + + public void assertReflectionArray(String expected, Object actual) { + assertEquals(expected, ToStringBuilder.reflectionToString(actual)); + assertEquals(expected, ToStringBuilder.reflectionToString(actual, null)); + assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, true)); + assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, false)); + } + + public void testReflectionObjectArray() { + Object[] array = new Object[] { null, base, new int[] { 3, 6 } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{,5,{3,6}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionLongArray() { + long[] array = new long[] { 1, 2, -3, 4 }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionIntArray() { + int[] array = new int[] { 1, 2, -3, 4 }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionShortArray() { + short[] array = new short[] { 1, 2, -3, 4 }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionyteArray() { + byte[] array = new byte[] { 1, 2, -3, 4 }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionCharArray() { + char[] array = new char[] { 'A', '2', '_', 'D' }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{A,2,_,D}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionDoubleArray() { + double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionFloatArray() { + float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionBooleanArray() { + boolean[] array = new boolean[] { true, false, false }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{true,false,false}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + // Reflection Array Array tests + + public void testReflectionFloatArrayArray() { + float[][] array = new float[][] { { 1.0f, 2.29686f }, null, { Float.NaN } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + + public void testReflectionLongArrayArray() { + long[][] array = new long[][] { { 1, 2 }, null, { 5 } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1,2},,{5}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionIntArrayArray() { + int[][] array = new int[][] { { 1, 2 }, null, { 5 } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1,2},,{5}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionhortArrayArray() { + short[][] array = new short[][] { { 1, 2 }, null, { 5 } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1,2},,{5}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionByteArrayArray() { + byte[][] array = new byte[][] { { 1, 2 }, null, { 5 } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1,2},,{5}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionCharArrayArray() { + char[][] array = new char[][] { { 'A', 'B' }, null, { 'p' } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{A,B},,{p}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionDoubleArrayArray() { + double[][] array = new double[][] { { 1.0, 2.29686 }, null, { Double.NaN } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + public void testReflectionBooleanArrayArray() { + boolean[][] array = new boolean[][] { { true, false }, null, { false } }; + String baseStr = this.toBaseString(array); + assertEquals(baseStr + "[{{true,false},,{false}}]", ToStringBuilder.reflectionToString(array)); + assertEquals(baseStr + "[{{true,false},,{false}}]", ToStringBuilder.reflectionToString(array)); + array = null; + assertReflectionArray("", array); + } + + // Reflection hierarchy tests public void testReflectionHierarchyArrayList() { List base = new ArrayList(); @@ -397,7 +561,6 @@ public class ToStringBuilderTest extends TestCase { assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); } - public void testLongArrayArray() { long[][] array = new long[][] {{1, 2}, null, {5}}; assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append(array).toString());