1 package junit.framework;
2 
3 // android-changed add @hide
4 /**
5  * @hide not needed for public API
6  */
7 public class ComparisonCompactor {
8 
9 	private static final String ELLIPSIS= "...";
10 	private static final String DELTA_END= "]";
11 	private static final String DELTA_START= "[";
12 
13 	private int fContextLength;
14 	private String fExpected;
15 	private String fActual;
16 	private int fPrefix;
17 	private int fSuffix;
18 
ComparisonCompactor(int contextLength, String expected, String actual)19 	public ComparisonCompactor(int contextLength, String expected, String actual) {
20 		fContextLength= contextLength;
21 		fExpected= expected;
22 		fActual= actual;
23 	}
24 
compact(String message)25 	public String compact(String message) {
26 		if (fExpected == null || fActual == null || areStringsEqual()) {
27 			// android-changed use local method instead of Assert.format, since
28 			// the later is not part of Android API till API 16
29 			return format(message, fExpected, fActual);
30 		}
31 		findCommonPrefix();
32 		findCommonSuffix();
33 		String expected= compactString(fExpected);
34 		String actual= compactString(fActual);
35 		// android-changed use local format method
36 		return format(message, expected, actual);
37 	}
38 
compactString(String source)39 	private String compactString(String source) {
40 		String result= DELTA_START + source.substring(fPrefix, source.length() - fSuffix + 1) + DELTA_END;
41 		if (fPrefix > 0)
42 			result= computeCommonPrefix() + result;
43 		if (fSuffix > 0)
44 			result= result + computeCommonSuffix();
45 		return result;
46 	}
47 
findCommonPrefix()48 	private void findCommonPrefix() {
49 		fPrefix= 0;
50 		int end= Math.min(fExpected.length(), fActual.length());
51 		for (; fPrefix < end; fPrefix++) {
52 			if (fExpected.charAt(fPrefix) != fActual.charAt(fPrefix))
53 				break;
54 		}
55 	}
56 
findCommonSuffix()57 	private void findCommonSuffix() {
58 		int expectedSuffix= fExpected.length() - 1;
59 		int actualSuffix= fActual.length() - 1;
60 		for (; actualSuffix >= fPrefix && expectedSuffix >= fPrefix; actualSuffix--, expectedSuffix--) {
61 			if (fExpected.charAt(expectedSuffix) != fActual.charAt(actualSuffix))
62 				break;
63 		}
64 		fSuffix=  fExpected.length() - expectedSuffix;
65 	}
66 
computeCommonPrefix()67 	private String computeCommonPrefix() {
68 		return (fPrefix > fContextLength ? ELLIPSIS : "") + fExpected.substring(Math.max(0, fPrefix - fContextLength), fPrefix);
69 	}
70 
computeCommonSuffix()71 	private String computeCommonSuffix() {
72 		int end= Math.min(fExpected.length() - fSuffix + 1 + fContextLength, fExpected.length());
73 		return fExpected.substring(fExpected.length() - fSuffix + 1, end) + (fExpected.length() - fSuffix + 1 < fExpected.length() - fContextLength ? ELLIPSIS : "");
74 	}
75 
areStringsEqual()76 	private boolean areStringsEqual() {
77 		return fExpected.equals(fActual);
78 	}
79 
80 	// android-changed copy of Assert.format for reasons described above
format(String message, Object expected, Object actual)81 	private static String format(String message, Object expected, Object actual) {
82         	String formatted= "";
83         	if (message != null && message.length() > 0)
84             		formatted= message+" ";
85         	return formatted+"expected:<"+expected+"> but was:<"+actual+">";
86 	}
87 }
88