15d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopackage junit.framework;
25d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
35d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao/**
45d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Thrown when an assert equals for Strings failed.
51ef4ce87e54a595a67263e550916b97a1b468b99Alex Light *
65d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Inspired by a patch from Alex Chaffee mailto:alex@purpletech.com
75d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */
85d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class ComparisonFailure extends AssertionFailedError {
91ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	private static final int MAX_CONTEXT_LENGTH= 20;
101ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	private static final long serialVersionUID= 1L;
111ef4ce87e54a595a67263e550916b97a1b468b99Alex Light
121ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	private String fExpected;
131ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	private String fActual;
145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
151ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	/**
161ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * Constructs a comparison failure.
171ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @param message the identifying message or null
181ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @param expected the expected string value
191ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @param actual the actual string value
201ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 */
211ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	public ComparisonFailure (String message, String expected, String actual) {
221ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		super (message);
231ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		fExpected= expected;
241ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		fActual= actual;
251ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	}
261ef4ce87e54a595a67263e550916b97a1b468b99Alex Light
271ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	/**
281ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * Returns "..." in place of common prefix and "..." in
291ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * place of common suffix between expected and actual.
301ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 *
311ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @see Throwable#getMessage()
321ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 */
331ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	@Override
341ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	public String getMessage() {
351ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage());
361ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	}
371ef4ce87e54a595a67263e550916b97a1b468b99Alex Light
381ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	/**
391ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * Gets the actual string value
401ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @return the actual string value
411ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 */
421ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	public String getActual() {
431ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		return fActual;
441ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	}
451ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	/**
461ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * Gets the expected string value
471ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 * @return the expected string value
481ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	 */
491ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	public String getExpected() {
501ef4ce87e54a595a67263e550916b97a1b468b99Alex Light		return fExpected;
511ef4ce87e54a595a67263e550916b97a1b468b99Alex Light	}
521ef4ce87e54a595a67263e550916b97a1b468b99Alex Light}