From 92e63fb3c87c73359c395406fed9c524b8626595 Mon Sep 17 00:00:00 2001 From: RahulNagekar Date: Mon, 3 Dec 2018 01:45:54 +0530 Subject: [PATCH 1/2] Adding junits for JsonToStringStyle Signed-off-by: RahulNagekar --- .../lang3/builder/JsonToStringStyleTest.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java index 3cf8f5887..c10119c46 100644 --- a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java @@ -237,6 +237,9 @@ public class JsonToStringStyleTest { assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + assertEquals("{\"objectArray\":" + "[null,5,[3,6]]" +"}", new ToStringBuilder(base).append("objectArray", array) + .toString()); + assertThrows( UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); @@ -253,6 +256,142 @@ public class JsonToStringStyleTest { assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + assertEquals("{\"longArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("longArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testIntArray() { + int[] array = new int[]{1, 2, -3, 4}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"intArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("intArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testByteArray() { + byte[] array = new byte[]{1, 2, -3, 4}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"byteArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("byteArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testShortArray() { + short[] array = new short[]{1, 2, -3, 4}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"shortArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("shortArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testDoubleArray() { + double[] array = new double[]{1, 2, -3, 4}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"doubleArray\":" + "[1.0,2.0,-3.0,4.0]" +"}", new ToStringBuilder(base).append("doubleArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testFloatArray() { + float[] array = new float[]{1, 2, -3, 4}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"floatArray\":" + "[1.0,2.0,-3.0,4.0]" +"}", new ToStringBuilder(base).append("floatArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testCharArray() { + char[] array = new char[]{'1', '2', '3', '4'}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"charArray\":" + "[\"1\",\"2\",\"3\",\"4\"]" +"}", new ToStringBuilder(base).append("charArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testBooleanArray() { + boolean[] array = new boolean[]{true, false}; + + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + + assertEquals("{\"booleanArray\":" + "[true,false]" +"}", new ToStringBuilder(base).append("booleanArray", array) + .toString()); + assertThrows( UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); From cee7cc6b6470362b12c134b4ed3f3b9bdcd4e465 Mon Sep 17 00:00:00 2001 From: RahulNagekar Date: Fri, 21 Dec 2018 01:05:50 +0530 Subject: [PATCH 2/2] Refactoring junits of JsonToStringStyle --- .../lang3/builder/JsonToStringStyleTest.java | 128 ++++++++++-------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java index c10119c46..b0ad25462 100644 --- a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java @@ -233,189 +233,199 @@ public class JsonToStringStyleTest { @Test public void testObjectArray() { - Object[] array = new Object[]{null, base, new int[]{3, 6}}; + final Object[] array = new Object[]{null, base, new int[]{3, 6}}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"objectArray\":" + "[null,5,[3,6]]" +"}", new ToStringBuilder(base).append("objectArray", array) + assertEquals("{\"objectArray\":[null,5,[3,6]]}", toStringBuilder.append("objectArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testLongArray() { - long[] array = new long[]{1, 2, -3, 4}; + final long[] array = new long[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"longArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("longArray", array) + assertEquals("{\"longArray\":[1,2,-3,4]}", toStringBuilder.append("longArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testIntArray() { - int[] array = new int[]{1, 2, -3, 4}; + final int[] array = new int[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"intArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("intArray", array) + assertEquals("{\"intArray\":[1,2,-3,4]}", toStringBuilder.append("intArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testByteArray() { - byte[] array = new byte[]{1, 2, -3, 4}; + final byte[] array = new byte[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"byteArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("byteArray", array) + assertEquals("{\"byteArray\":[1,2,-3,4]}", toStringBuilder.append("byteArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testShortArray() { - short[] array = new short[]{1, 2, -3, 4}; + final short[] array = new short[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"shortArray\":" + "[1,2,-3,4]" +"}", new ToStringBuilder(base).append("shortArray", array) + assertEquals("{\"shortArray\":[1,2,-3,4]}", toStringBuilder.append("shortArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testDoubleArray() { - double[] array = new double[]{1, 2, -3, 4}; + final double[] array = new double[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"doubleArray\":" + "[1.0,2.0,-3.0,4.0]" +"}", new ToStringBuilder(base).append("doubleArray", array) + assertEquals("{\"doubleArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("doubleArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testFloatArray() { - float[] array = new float[]{1, 2, -3, 4}; + final float[] array = new float[]{1, 2, -3, 4}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"floatArray\":" + "[1.0,2.0,-3.0,4.0]" +"}", new ToStringBuilder(base).append("floatArray", array) + assertEquals("{\"floatArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("floatArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testCharArray() { - char[] array = new char[]{'1', '2', '3', '4'}; + final char[] array = new char[]{'1', '2', '3', '4'}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"charArray\":" + "[\"1\",\"2\",\"3\",\"4\"]" +"}", new ToStringBuilder(base).append("charArray", array) + assertEquals("{\"charArray\":[\"1\",\"2\",\"3\",\"4\"]}", toStringBuilder.append("charArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testBooleanArray() { - boolean[] array = new boolean[]{true, false}; + final boolean[] array = new boolean[]{true, false}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"booleanArray\":" + "[true,false]" +"}", new ToStringBuilder(base).append("booleanArray", array) + assertEquals("{\"booleanArray\":[true,false]}", toStringBuilder.append("booleanArray", array) .toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test public void testLongArrayArray() { - long[][] array = new long[][]{{1, 2}, null, {5}}; + final long[][] array = new long[][]{{1, 2}, null, {5}}; - assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(array).toString()); + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((long[][]) null).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((long[][]) null).toString()); assertThrows( - UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) array).toString()); + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test