1 package org.junit.internal; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.junit.Assert; 7 8 /** 9 * Thrown when two array elements differ 10 * @see Assert#assertArrayEquals(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#assertArrayEquals(String, Object[], Object[]) 26 */ 27 public ArrayComparisonFailure(String message, AssertionError cause, int index) { 28 fMessage= message; 29 fCause= cause; 30 addDimension(index); 31 } 32 33 public void addDimension(int index) { 34 fIndices.add(0, index); 35 } 36 37 @Override 38 public String getMessage() { 39 StringBuilder builder= new StringBuilder(); 40 if (fMessage != null) 41 builder.append(fMessage); 42 builder.append("arrays first differed at element "); 43 for (int each : fIndices) { 44 builder.append("["); 45 builder.append(each); 46 builder.append("]"); 47 } 48 builder.append("; "); 49 builder.append(fCause.getMessage()); 50 return builder.toString(); 51 } 52 53 /** 54 * {@inheritDoc} 55 */ 56 @Override public String toString() { 57 return getMessage(); 58 } 59 } 60