Skip to content

Commit

Permalink
fixed class cast exception in formatParameter if array is of primitiv…
Browse files Browse the repository at this point in the history
…e component type
  • Loading branch information
Andreas Schmid committed May 2, 2013
1 parent 17f6220 commit 0702c10
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public boolean equals(Object obj) {
* <li>array (e.g. String[]) -&gt; &lt;array&gt;</li>
* <li>other -&gt; Object.toString</li>
* </ul>
*
*
* @param parameters the parameters are converted to a comma-separated string
* @return a string representation of the given parameters
*/
Expand All @@ -101,7 +101,11 @@ private <T> String formatParameters(T[] parameters) {
}

if (param.getClass().isArray()) {
stringBuilder.append('[').append(formatParameters(getArray(param))).append(']');
if (param.getClass().getComponentType().isPrimitive()) {
appendTo(stringBuilder, param);
} else {
stringBuilder.append('[').append(formatParameters(getArray(param))).append(']');
}

} else if (param instanceof String && ((String) param).isEmpty()) {
stringBuilder.append("<empty string>");
Expand All @@ -117,22 +121,30 @@ private <T> String formatParameters(T[] parameters) {
return stringBuilder.toString();
}

private void appendTo(StringBuilder stringBuilder, Object primitiveArray) {
Class<?> componentType = primitiveArray.getClass().getComponentType();
if (boolean.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((boolean[]) primitiveArray));
} else if (byte.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((byte[]) primitiveArray));
} else if (char.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((char[]) primitiveArray));
} else if (short.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((short[]) primitiveArray));
} else if (int.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((int[]) primitiveArray));
} else if (long.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((long[]) primitiveArray));
} else if (float.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((float[]) primitiveArray));
} else if (double.class.equals(componentType)) {
stringBuilder.append(Arrays.toString((double[]) primitiveArray));
}
}

private <T> T[] getArray(Object array) {
@SuppressWarnings("unchecked")
T[] result = (T[]) array;
return result;
}

public static String encodeHTML(String s) {
StringBuffer out = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c > 127 || c == '"' || c == '<' || c == '>') {
out.append("&#" + (int) c + ";");
} else {
out.append(c);
}
}
return out.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public void testGetNameShouldReturnSpecialHandlingForArray() {
assertThat(result).matches(method.getName() + "\\[4: \\[test]]");
}

@Test
public void testGetNameShouldReturnSpecialHandlingForPrimitiveTypeArray() {

// Given:
Method method = anyMethod();
final Object[] parameters = new Object[] { new int[] { 1, 2 } };

DataProviderFrameworkMethod underTest = new DataProviderFrameworkMethod(method, 4, parameters);

// When:
String result = underTest.getName();

// Then:
assertThat(result).matches(method.getName() + "\\[4: \\[1, 2]]");
}

@Test
public void testGetNameShouldReturnSpecialHandlingForArrayInArray() {

Expand Down

0 comments on commit 0702c10

Please sign in to comment.