001package junit.framework; 002 003/** 004 * Thrown when an assert equals for Strings failed. 005 * 006 * Inspired by a patch from Alex Chaffee mailto:alex@purpletech.com 007 */ 008public class ComparisonFailure extends AssertionFailedError { 009 private static final int MAX_CONTEXT_LENGTH = 20; 010 private static final long serialVersionUID = 1L; 011 012 private String fExpected; 013 private String fActual; 014 015 /** 016 * Constructs a comparison failure. 017 * 018 * @param message the identifying message or null 019 * @param expected the expected string value 020 * @param actual the actual string value 021 */ 022 public ComparisonFailure(String message, String expected, String actual) { 023 super(message); 024 fExpected = expected; 025 fActual = actual; 026 } 027 028 /** 029 * Returns "..." in place of common prefix and "..." in 030 * place of common suffix between expected and actual. 031 * 032 * @see Throwable#getMessage() 033 */ 034 @Override 035 public String getMessage() { 036 return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage()); 037 } 038 039 /** 040 * Gets the actual string value 041 * 042 * @return the actual string value 043 */ 044 public String getActual() { 045 return fActual; 046 } 047 048 /** 049 * Gets the expected string value 050 * 051 * @return the expected string value 052 */ 053 public String getExpected() { 054 return fExpected; 055 } 056}