1 23 24 package org.enhydra.xml.driver; 25 26 import java.io.BufferedReader ; 27 import java.io.BufferedWriter ; 28 import java.io.File ; 29 import java.io.FileNotFoundException ; 30 import java.io.FileReader ; 31 import java.io.FileWriter ; 32 import java.io.IOException ; 33 import java.io.PrintWriter ; 34 import java.util.ArrayList ; 35 36 import org.enhydra.xml.driver.diff.diff; 37 38 39 44 public class TestDiff { 45 48 public interface LineFilter { 49 50 public String filter(String line); 51 } 52 53 57 public static final int QUEUE_FAILURES = 0x01; 58 59 60 public static final int UPDATE_EXPECTED = 0x02; 61 62 63 public static final int NO_OUTPUT = 0x04; 64 65 66 private static final int BUF_SIZE = 2048; 67 68 69 private int fFlags; 70 71 72 private ArrayList fFilters = new ArrayList (); 73 74 75 private ArrayList fFailedFiles; 76 77 78 private PrintWriter fMsgWriter; 79 80 83 private class LineFilterReader extends BufferedReader { 84 85 private File fFile; 86 87 88 public LineFilterReader(File inFile) throws IOException { 89 super(new FileReader (inFile), BUF_SIZE); 90 fFile = inFile; 91 } 92 93 94 public File getFile() { 95 return fFile; 96 } 97 98 99 public String readLine() throws IOException { 100 String line = super.readLine(); 101 if (line == null) { 102 return null; 103 } 104 int numFilters= fFilters.size(); 105 for (int idx = 0; idx < numFilters; idx++) { 106 line = ((LineFilter)fFilters.get(idx)).filter(line); 107 } 108 return line; 109 } 110 } 111 112 115 public TestDiff(PrintWriter msgWriter, 116 int flags) { 117 fMsgWriter = msgWriter; 118 fFlags = flags; 119 } 120 121 122 public void addFilter(LineFilter filter) { 123 fFilters.add(filter); 124 } 125 126 127 private void copyLines(BufferedReader in, 128 BufferedWriter out) throws IOException { 129 String line; 130 while ((line = in.readLine()) != null) { 131 out.write(line, 0, line.length()); 132 out.newLine(); 133 } 134 } 135 136 137 private void updateExpected(File expectedFile, 138 File gotFile) { 139 fMsgWriter.println("Update " + expectedFile + " from " + gotFile); 140 try { 141 TestFileOps.ensureFileDir(expectedFile); 142 BufferedWriter out = new BufferedWriter (new FileWriter (expectedFile), 143 BUF_SIZE); 144 try { 145 BufferedReader in = new LineFilterReader(gotFile); 146 try { 147 copyLines(in, out); 148 } finally { 149 in.close(); 150 } 151 } finally { 152 out.close(); 153 } 154 } catch (IOException except) { 155 throw new TestError("update of expected file failed", except); 156 } 157 } 158 159 163 private void diffError(File expectedFile, 164 File gotFile, 165 Throwable except) { 166 if (fFailedFiles == null) { 167 fFailedFiles = new ArrayList (); 168 } 169 fFailedFiles.add(gotFile); 170 171 String msg = "diff of \"" + expectedFile + "\" and \"" 172 + gotFile + "\" failed"; 173 if (except instanceof FileNotFoundException ) { 174 fMsgWriter.println(msg + ": " + except); 176 } else if (except != null) { 177 fMsgWriter.println(msg); 178 except.printStackTrace(); 179 } 180 if ((fFlags & UPDATE_EXPECTED) != 0) { 181 updateExpected(expectedFile, gotFile); 182 } 183 if ((fFlags & QUEUE_FAILURES) == 0) { 184 throw new DiffException(msg, except); 185 } 186 } 187 188 189 private boolean doDiff(File expectedFile, 190 BufferedReader expected, 191 LineFilterReader got) throws IOException { 192 diff differ = new diff(); 193 differ.diffBuffer(expected, got); 194 int numHunks = differ.numberOfHunk(); 195 if (numHunks > 0) { 196 if ((fFlags & NO_OUTPUT) == 0) { 197 for (int idx = 0; idx < numHunks; idx++) { 199 fMsgWriter.print(differ.hunkAt(idx).convert()); 200 } 201 fMsgWriter.println(); 202 diffError(expectedFile, got.getFile(), null); 203 } 204 return false; 205 } 206 return true; 207 } 208 209 214 public boolean diff(File expectedFile, 215 File gotFile) { 216 try { 217 BufferedReader expected 218 = new BufferedReader (new FileReader (expectedFile), BUF_SIZE); 219 try { 220 LineFilterReader got = new LineFilterReader(gotFile); 221 222 try { 223 return doDiff(expectedFile, expected, got); 224 } finally { 225 got.close(); 226 } 227 } finally { 228 expected.close(); 229 } 230 } catch (IOException except) { 231 diffError(expectedFile, gotFile, except); 232 return false; 233 } 234 } 235 236 241 public void throwPendingDiffFailures() { 242 if (fFailedFiles != null) { 243 StringBuffer msg = new StringBuffer (512); 244 msg.append("diff with expected files failed, see stderr for details: "); 245 for (int idx = 0; idx < fFailedFiles.size(); idx++) { 246 if (idx > 0) { 247 msg.append(", "); 248 } 249 msg.append(fFailedFiles.get(idx)); 250 } 251 fFailedFiles = null; 252 throw new DiffException(msg.toString()); 253 } 254 } 255 256 259 public boolean fgrep(String searchStr, 260 File fileName) { 261 try { 262 BufferedReader in = new BufferedReader (new FileReader (fileName)); 263 try { 264 String line; 265 while ((line = in.readLine()) != null) { 266 if (line.indexOf(searchStr) >= 0) { 267 break; } 269 } 270 if (line == null) { 271 return false; 272 } else { 273 return true; 274 } 275 } finally { 276 in.close(); 277 } 278 } catch (FileNotFoundException except) { 279 throw new DiffException("fgrep failed: " + except); 280 } catch (IOException except) { 281 throw new DiffException("fgrep of \"" + fileName + "\" failed ", 282 except); 283 } 284 } 285 286 289 public void fgrepMustFind(String searchStr, 290 File fileName) { 291 if (!fgrep(searchStr, fileName)) { 292 throw new DiffException("did not find \"" + searchStr + "\" in \"" 293 + fileName + "\""); 294 } 295 } 296 297 300 public void fgrepMustNotFind(String searchStr, 301 File fileName) { 302 if (fgrep(searchStr, fileName)) { 303 throw new DiffException("should not have found \"" + searchStr + "\" in \"" 304 + fileName + "\""); 305 } 306 } 307 } 308 | Popular Tags |