1 54 55 package junitx.framework; 56 57 import junit.framework.AssertionFailedError; 58 59 74 public class ComparisonFailure extends AssertionFailedError { 75 76 private static final int MINIMAL_LENGTH = 10; 77 78 private String expected; 79 private String actual; 80 private String message = ""; 81 82 85 public ComparisonFailure(String message, 86 String expected, 87 String actual) { 88 super(message); 89 this.expected = expected; 90 this.actual = actual; 91 this.message = createMessage(super.getMessage(), this.expected, this.actual); 92 } 93 94 public String getMessage() { 95 return this.message; 96 } 97 98 106 protected static String createMessage(String message, 107 String expected, 108 String actual) { 109 if ((expected == null || actual == null) || 110 (actual.equals(expected))) { 111 return format(message, expected, actual, null); 112 } 113 114 int end = Math.min(expected.length(), actual.length()); 115 int beginDiff = 0; 116 while ((beginDiff < end) && 117 (expected.charAt(beginDiff) == actual.charAt(beginDiff))) { 118 beginDiff++; 119 } 120 121 int endDiffExp = expected.length() - 1; 122 int endDiffAct = actual.length() - 1; 123 while ((endDiffExp >= beginDiff) && 124 (endDiffAct >= beginDiff) && 125 (expected.charAt(endDiffExp) == actual.charAt(endDiffAct))) { 126 endDiffAct--; 127 endDiffExp--; 128 } 129 130 133 String delta = actual.substring(beginDiff, endDiffAct+1); 134 if (delta.equals("")) { 135 delta = expected.substring(beginDiff, endDiffExp+1); 136 } 137 138 142 boolean optimized = false; 143 while (!optimized) { 144 if (endDiffAct - beginDiff < MINIMAL_LENGTH - 1) { 145 if (beginDiff > 0 && !isStopper(actual.charAt(beginDiff))) { 146 beginDiff--; 147 } else if ((endDiffAct < actual.length()-1) && 148 !isStopper(actual.charAt(endDiffAct))) { 149 endDiffAct++; 150 endDiffExp++; 151 } else { 152 optimized = true; 153 } 154 } else { 155 optimized = true; 156 } 157 } 158 159 String expectedDisplay = expected.substring(beginDiff, endDiffExp + 1); 160 String actualDisplay = actual.substring(beginDiff, endDiffAct + 1); 161 162 if (delta.equals(actualDisplay)) { 164 delta = null; 165 } 166 167 if (beginDiff > 0) { 168 expectedDisplay = "..." + expectedDisplay; 169 actualDisplay = "..." + actualDisplay; 170 } 171 if (endDiffExp < expected.length() - 1) { 172 expectedDisplay = expectedDisplay + "..."; 173 } 174 if (endDiffAct < actual.length() - 1) { 175 actualDisplay = actualDisplay + "..."; 176 } 177 178 return format(message, expectedDisplay, actualDisplay, delta); 179 } 180 181 private static boolean isStopper(char input) { 182 return (input == ' ') || 183 (input == ',') || (input == ';') || 184 (input == '(') || (input == ')') || 185 (input == '[') || (input == ']') || 186 (input == '{') || (input == '}'); 187 } 188 189 private static String format(String message, 190 String expected, 191 String actual, 192 String delta) { 193 String formatted = ""; 194 if (message != null) { 195 formatted = message + " "; 196 } 197 if (delta == null) { 198 return formatted + "expected:<" + expected + "> but was:<" + actual + ">"; 199 } else { 200 return formatted + "expected:<" + expected + "> but was:<" + actual + "> ['" + delta + "']"; 201 } 202 } 203 204 } 205 | Popular Tags |