Paul Duffin | cd7c34d | 2016-12-12 16:35:36 +0000 | [diff] [blame] | 1 | package 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 | */ |
| 8 | public 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 | * @param message the identifying message or null |
| 18 | * @param expected the expected string value |
| 19 | * @param actual the actual string value |
| 20 | */ |
| 21 | public ComparisonFailure (String message, String expected, String actual) { |
| 22 | super (message); |
| 23 | fExpected= expected; |
| 24 | fActual= actual; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns "..." in place of common prefix and "..." in |
| 29 | * place of common suffix between expected and actual. |
| 30 | * |
| 31 | * @see Throwable#getMessage() |
| 32 | */ |
| 33 | @Override |
| 34 | public String getMessage() { |
| 35 | return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage()); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Gets the actual string value |
| 40 | * @return the actual string value |
| 41 | */ |
| 42 | public String getActual() { |
| 43 | return fActual; |
| 44 | } |
| 45 | /** |
| 46 | * Gets the expected string value |
| 47 | * @return the expected string value |
| 48 | */ |
| 49 | public String getExpected() { |
| 50 | return fExpected; |
| 51 | } |
| 52 | } |