1package junit.framework;
2
3/**
4 * Thrown when an assert equals for Strings failed.
5 *
6 * Inspired by a patch from Alex Chaffee mailto:alex@purpletech.com
7 */
8public class ComparisonFailure extends AssertionFailedError {
9    private static final int MAX_CONTEXT_LENGTH = 20;
10    private static final long serialVersionUID = 1L;
11
12    private String fExpected;
13    private String fActual;
14
15    /**
16     * Constructs a comparison failure.
17     *
18     * @param message the identifying message or null
19     * @param expected the expected string value
20     * @param actual the actual string value
21     */
22    public ComparisonFailure(String message, String expected, String actual) {
23        super(message);
24        fExpected = expected;
25        fActual = actual;
26    }
27
28    /**
29     * Returns "..." in place of common prefix and "..." in
30     * place of common suffix between expected and actual.
31     *
32     * @see Throwable#getMessage()
33     */
34    @Override
35    public String getMessage() {
36        return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage());
37    }
38
39    /**
40     * Gets the actual string value
41     *
42     * @return the actual string value
43     */
44    public String getActual() {
45        return fActual;
46    }
47
48    /**
49     * Gets the expected string value
50     *
51     * @return the expected string value
52     */
53    public String getExpected() {
54        return fExpected;
55    }
56}