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