From ece2f255eed8ad9eff6e236152de65e804c6adb5 Mon Sep 17 00:00:00 2001 From: Basha Date: Sat, 29 Apr 2017 18:11:13 +0530 Subject: [PATCH] Increase test coverage of ToStringBuilder class to 100% --- .../lang3/builder/ToStringBuilderTest.java | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java index eba610afc..b21832945 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java @@ -614,6 +614,231 @@ public class ToStringBuilderTest { assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendToString(null).append("a", "hello").toString()); } + @Test + public void testAppendAsObjectToString() { + String objectToAppend1 = ""; + Boolean objectToAppend2 = Boolean.TRUE; + Object objectToAppend3 = new Object(); + + assertEquals(baseStr + "[" + toBaseString(objectToAppend1) + "]", + new ToStringBuilder(base).appendAsObjectToString(objectToAppend1).toString()); + assertEquals(baseStr + "[" + toBaseString(objectToAppend2) + "]", + new ToStringBuilder(base).appendAsObjectToString(objectToAppend2).toString()); + assertEquals(baseStr + "[" + toBaseString(objectToAppend3) + "]", + new ToStringBuilder(base).appendAsObjectToString(objectToAppend3).toString()); + } + + @Test + public void testAppendBooleanArrayWithFieldName() { + boolean[] array = new boolean[] { true, false, false }; + assertEquals(baseStr + "[flags={true,false,false}]", + new ToStringBuilder(base).append("flags", array).toString()); + assertEquals(baseStr + "[flags=]", + new ToStringBuilder(base).append("flags", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendBooleanArrayWithFieldNameAndFullDetatil() { + boolean[] array = new boolean[] { true, false, false }; + assertEquals(baseStr + "[flags={true,false,false}]", + new ToStringBuilder(base).append("flags", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[flags=]", + new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendCharArrayWithFieldName() { + char[] array = new char[] { 'A', '2', '_', 'D' }; + assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array).toString()); + assertEquals(baseStr + "[letters={A,2,_,D}]", new ToStringBuilder(base).append("letters", array).toString()); + assertEquals(baseStr + "[flags=]", + new ToStringBuilder(base).append("flags", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendCharArrayWithFieldNameAndFullDetatil() { + char[] array = new char[] { 'A', '2', '_', 'D' }; + assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array, true).toString()); + assertEquals(baseStr + "[letters=]", + new ToStringBuilder(base).append("letters", array, false).toString()); + assertEquals(baseStr + "[flags=]", + new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendDoubleArrayWithFieldName() { + double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 }; + assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", + new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendDoubleArrayWithFieldNameAndFullDetatil() { + double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 }; + assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendObjectArrayWithFieldName() { + Object[] array = new Object[] { null, base, new int[] { 3, 6 } }; + assertEquals(baseStr + "[values={,5,{3,6}}]", + new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{,5,{3,6}}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendObjectArrayWithFieldNameAndFullDetatil() { + Object[] array = new Object[] { null, base, new int[] { 3, 6 } }; + assertEquals(baseStr + "[values={,5,{3,6}}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendLongArrayWithFieldName() { + long[] array = new long[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendLongArrayWithFieldNameAndFullDetatil() { + long[] array = new long[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendIntArrayWithFieldName() { + int[] array = new int[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendIntArrayWithFieldNameAndFullDetatil() { + int[] array = new int[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendShortArrayWithFieldName() { + short[] array = new short[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendShortArrayWithFieldNameAndFullDetatil() { + short[] array = new short[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendByteArrayWithFieldName() { + byte[] array = new byte[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendByteArrayWithFieldNameAndFullDetatil() { + byte[] array = new byte[] { 1, 2, -3, 4 }; + assertEquals(baseStr + "[values={1,2,-3,4}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + + @Test + public void testAppendFloatArrayWithFieldName() { + float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f }; + assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", + new ToStringBuilder(base).append("values", array).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null).toString()); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString()); + } + + @Test + public void testAppendFloatArrayWithFieldNameAndFullDetatil() { + float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f }; + assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", + new ToStringBuilder(base).append("values", array, true).toString()); + assertEquals(baseStr + "[length=]", + new ToStringBuilder(base).append("length", array, false).toString()); + assertEquals(baseStr + "[values=]", + new ToStringBuilder(base).append("values", (boolean[]) null, true).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(null, array, false).toString()); + } + @Test public void testObject() { final Integer i3 = Integer.valueOf(3);