From b2d7fce66be7904246dc2870a7575340acf3d110 Mon Sep 17 00:00:00 2001 From: Tom Anderson Date: Mon, 16 May 2016 00:06:52 +0100 Subject: [PATCH] Produce a more legible failure message when multidimensional arrays are of different lengths Closes #1300 --- .../junit/internal/ComparisonCriteria.java | 40 ++++++++++++++----- .../junit/tests/assertion/AssertionTest.java | 36 +++++++++++++---- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/junit/internal/ComparisonCriteria.java b/src/main/java/org/junit/internal/ComparisonCriteria.java index fa7840cf9b7b..ed1c674907cb 100644 --- a/src/main/java/org/junit/internal/ComparisonCriteria.java +++ b/src/main/java/org/junit/internal/ComparisonCriteria.java @@ -81,8 +81,8 @@ private void arrayEquals(String message, Object expecteds, Object actuals, boole } if (actualsLength != expectedsLength) { - Object expected = getArrayElementOrSentinel(expecteds, expectedsLength, prefixLength); - Object actual = getArrayElementOrSentinel(actuals, actualsLength, prefixLength); + Object expected = getToStringableArrayElement(expecteds, expectedsLength, prefixLength); + Object actual = getToStringableArrayElement(actuals, actualsLength, prefixLength); try { Assert.assertEquals(expected, actual); } catch (AssertionError e) { @@ -91,15 +91,37 @@ private void arrayEquals(String message, Object expecteds, Object actuals, boole } } - private static final Object END_OF_ARRAY_SENTINEL = new Object() { - @Override - public String toString() { - return "end of array"; + private static final Object END_OF_ARRAY_SENTINEL = objectWithToString("end of array"); + + private Object getToStringableArrayElement(Object array, int length, int index) { + if (index < length) { + Object element = Array.get(array, index); + if (isArray(element)) { + return objectWithToString(componentTypeName(element.getClass()) + "[" + Array.getLength(element) + "]"); + } else { + return element; + } + } else { + return END_OF_ARRAY_SENTINEL; } - }; + } - private Object getArrayElementOrSentinel(Object array, int length, int index) { - return index < length ? Array.get(array, index) : END_OF_ARRAY_SENTINEL; + private static Object objectWithToString(final String string) { + return new Object() { + @Override + public String toString() { + return string; + } + }; + } + + private String componentTypeName(Class arrayClass) { + Class componentType = arrayClass.getComponentType(); + if (componentType.isArray()) { + return componentTypeName(componentType) + "[]"; + } else { + return componentType.getName(); + } } private boolean isArray(Object expected) { diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index b13f79eac349..fabda1336761 100755 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -243,27 +243,49 @@ public void multiDimensionalArraysAreNotEqualNoMessage() { @Test public void twoDimensionalArraysDifferentOuterLengthNotEqual() { - Object[] extraArray = {true}; assertArrayEqualsFailure( "not equal", - new Object[][]{extraArray, {}}, + new Object[][]{{true}, {}}, new Object[][]{{}}, "not equal: array lengths differed, expected.length=1 actual.length=0; arrays first differed at element [0][0]; expected: but was:"); assertArrayEqualsFailure( "not equal", - new Object[][]{{}, extraArray}, + new Object[][]{{}, {true}}, new Object[][]{{}}, - "not equal: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected:<" + extraArray.toString() + "> but was:"); + "not equal: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected: but was:"); assertArrayEqualsFailure( "not equal", new Object[][]{{}}, - new Object[][]{extraArray, {}}, + new Object[][]{{true}, {}}, "not equal: array lengths differed, expected.length=0 actual.length=1; arrays first differed at element [0][0]; expected: but was:"); assertArrayEqualsFailure( "not equal", new Object[][]{{}}, - new Object[][]{{}, extraArray}, - "not equal: array lengths differed, expected.length=1 actual.length=2; arrays first differed at element [1]; expected: but was:<" + extraArray.toString() + ">"); + new Object[][]{{}, {true}}, + "not equal: array lengths differed, expected.length=1 actual.length=2; arrays first differed at element [1]; expected: but was:"); + } + + @Test + public void primitiveArraysConvertedToStringCorrectly() { + assertArrayEqualsFailure( + "not equal", + new boolean[][]{{}, {true}}, + new boolean[][]{{}}, + "not equal: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected: but was:"); + assertArrayEqualsFailure( + "not equal", + new int[][]{{}, {23}}, + new int[][]{{}}, + "not equal: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected: but was:"); + } + + @Test + public void twoDimensionalArraysConvertedToStringCorrectly() { + assertArrayEqualsFailure( + "not equal", + new Object[][][]{{}, {{true}}}, + new Object[][][]{{}}, + "not equal: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected: but was:"); } @Test