KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > ArrayComparisonFailure


1 package org.junit.internal;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.junit.Assert;
7
8 /**
9  * Thrown when two array elements differ
10  * @see Assert#assertEquals(String, Object[], Object[])
11  */

12 public class ArrayComparisonFailure extends AssertionError JavaDoc {
13
14     private static final long serialVersionUID= 1L;
15     
16     private List JavaDoc<Integer JavaDoc> fIndices= new ArrayList JavaDoc<Integer JavaDoc>();
17     private final String JavaDoc fMessage;
18     private final AssertionError JavaDoc 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 JavaDoc message, AssertionError JavaDoc 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 JavaDoc
38     public String JavaDoc getMessage() {
39         StringBuilder JavaDoc builder= new StringBuilder JavaDoc();
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 JavaDoc public String JavaDoc toString() {
57         return getMessage();
58     }
59 }
60
Popular Tags