01: package org.junit.internal;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import org.junit.Assert;
07:
08: /**
09: * Thrown when two array elements differ
10: * @see Assert#assertEquals(String, Object[], Object[])
11: */
12: public class ArrayComparisonFailure extends AssertionError {
13:
14: private static final long serialVersionUID = 1L;
15:
16: private List<Integer> fIndices = new ArrayList<Integer>();
17: private final String fMessage;
18: private final AssertionError fCause;
19:
20: /**
21: * Construct a new <code>ArrayComparisonFailure</code> with an error text and the array's
22: * dimension that was not equal
23: * @param cause the exception that caused the array's content to fail the assertion test
24: * @param index the array position of the objects that are not equal.
25: * @see Assert#assertEquals(String, Object[], Object[])
26: */
27: public ArrayComparisonFailure(String message, AssertionError cause,
28: int index) {
29: fMessage = message;
30: fCause = cause;
31: addDimension(index);
32: }
33:
34: public void addDimension(int index) {
35: fIndices.add(0, index);
36: }
37:
38: @Override
39: public String getMessage() {
40: StringBuilder builder = new StringBuilder();
41: if (fMessage != null)
42: builder.append(fMessage);
43: builder.append("arrays first differed at element ");
44: for (int each : fIndices) {
45: builder.append("[");
46: builder.append(each);
47: builder.append("]");
48: }
49: builder.append("; ");
50: builder.append(fCause.getMessage());
51: return builder.toString();
52: }
53:
54: /**
55: * {@inheritDoc}
56: */
57: @Override
58: public String toString() {
59: return getMessage();
60: }
61: }
|