01: package com.mockrunner.test.consistency;
02:
03: import java.io.BufferedReader;
04: import java.io.File;
05: import java.io.FileReader;
06: import java.util.HashMap;
07: import java.util.Iterator;
08: import java.util.Map;
09:
10: import com.mockrunner.gen.proc.GeneratorUtil;
11:
12: import junit.framework.TestCase;
13:
14: public class VersionConsistencyTest extends TestCase {
15: public void testFileConsistency() throws Exception {
16: compareDirTrees("srcjdk1.3", "src");
17: compareDirTrees("srcjdk1.4", "src");
18: compareDirTrees("srcj2ee1.3", "src");
19: compareDirTrees("srcj2ee1.4", "src");
20: }
21:
22: private void compareDirTrees(String sourceDir, String destinationDir)
23: throws Exception {
24: GeneratorUtil util = new GeneratorUtil();
25: File sourceFile = new File(sourceDir);
26: File destFile = new File(destinationDir);
27: Map sourceMap = new HashMap();
28: Map destMap = new HashMap();
29: util.addJavaSrcFiles(sourceDir, sourceFile, sourceMap);
30: util.addJavaSrcFiles(destinationDir, destFile, destMap);
31: boolean ok = true;
32: Iterator sourceIterator = sourceMap.keySet().iterator();
33: while (sourceIterator.hasNext()) {
34: String currentFileName = (String) sourceIterator.next();
35: File currentSourceFile = (File) sourceMap
36: .get(currentFileName);
37: File currentDestFile = (File) destMap.get(currentFileName);
38: if (null == currentDestFile) {
39: System.out.println("File "
40: + currentSourceFile.getPath()
41: + " not found under src");
42: ok = false;
43: } else {
44: if (!compareFiles(currentSourceFile, currentDestFile)) {
45: System.out.println("Mismatch in file "
46: + currentSourceFile.getPath());
47: System.out.println();
48: ok = false;
49: }
50: }
51: }
52: assertTrue("There are errors", ok);
53: }
54:
55: private boolean compareFiles(File file1, File file2)
56: throws Exception {
57: BufferedReader reader1 = new BufferedReader(new FileReader(
58: file1));
59: BufferedReader reader2 = new BufferedReader(new FileReader(
60: file2));
61: boolean match = true;
62: String line1 = null;
63: String line2 = null;
64: int lineNumber = 1;
65: while (null != (line1 = reader1.readLine())) {
66: line2 = reader2.readLine();
67: if (!line1.equals(line2)) {
68: line1 = filter(line1);
69: line2 = filter(line2);
70: if (!line1.equals(line2)) {
71: match = false;
72: System.out
73: .println("Mismatch in line " + lineNumber);
74: System.out.println("Line1: " + line1);
75: System.out.println("Line2: " + line2);
76: }
77: }
78: lineNumber++;
79: }
80: return match;
81: }
82:
83: private String filter(String line) {
84: if (null == line)
85: return "";
86: String filteredLine = line.replaceAll("//", "");
87: filteredLine = filteredLine.replaceAll("/\\*", "");
88: filteredLine = filteredLine.replaceAll("\\*/", "");
89: return filteredLine.trim();
90: }
91: }
|