Home | History | Annotate | Download | only in internal
      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  *
     11  * @see Assert#assertArrayEquals(String, Object[], Object[])
     12  */
     13 public class ArrayComparisonFailure extends AssertionError {
     14 
     15     private static final long serialVersionUID = 1L;
     16 
     17     /*
     18      * We have to use the f prefix until the next major release to ensure
     19      * serialization compatibility.
     20      * See https://github.com/junit-team/junit/issues/976
     21      */
     22     private final List<Integer> fIndices = new ArrayList<Integer>();
     23     private final String fMessage;
     24 
     25     /**
     26      * Construct a new <code>ArrayComparisonFailure</code> with an error text and the array's
     27      * dimension that was not equal
     28      *
     29      * @param cause the exception that caused the array's content to fail the assertion test
     30      * @param index the array position of the objects that are not equal.
     31      * @see Assert#assertArrayEquals(String, Object[], Object[])
     32      */
     33     public ArrayComparisonFailure(String message, AssertionError cause, int index) {
     34         this.fMessage = message;
     35         initCause(cause);
     36         addDimension(index);
     37     }
     38 
     39     public void addDimension(int index) {
     40         fIndices.add(0, index);
     41     }
     42 
     43     @Override
     44     public String getMessage() {
     45         StringBuilder sb = new StringBuilder();
     46         if (fMessage != null) {
     47             sb.append(fMessage);
     48         }
     49         sb.append("arrays first differed at element ");
     50         for (int each : fIndices) {
     51             sb.append("[");
     52             sb.append(each);
     53             sb.append("]");
     54         }
     55         sb.append("; ");
     56         sb.append(getCause().getMessage());
     57         return sb.toString();
     58     }
     59 
     60     /**
     61      * {@inheritDoc}
     62      */
     63     @Override
     64     public String toString() {
     65         return getMessage();
     66     }
     67 }
     68