1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.util;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.zip.GZIPInputStream;
29
30
31 public class Compare {
32 static final int B1_NULL = -1;
33 static final int B2_NULL = -2;
34
35 private static final InputStream open(
36 final Class testClass,
37 final String fileName) throws IOException {
38 String resourceName = fileName;
39 if (fileName.startsWith("witness/")) {
40 resourceName = fileName.substring(fileName.lastIndexOf('/') + 1);
41 }
42 InputStream is = testClass.getResourceAsStream(resourceName);
43 if (is == null) {
44 File file = new File(fileName);
45 if (file.exists()) {
46 is = new FileInputStream(file);
47 } else {
48 throw new FileNotFoundException("Resource "
49 + resourceName + " not found");
50 }
51 }
52 return is;
53 }
54
55 public static boolean compare(Class testClass,
56 final String file1,
57 final String file2)
58 throws IOException {
59 BufferedReader in1 = new BufferedReader(new FileReader(file1));
60 BufferedReader in2 = new BufferedReader(new InputStreamReader(
61 open(testClass, file2)));
62 try {
63 return compare(testClass, file1, file2, in1, in2);
64 } finally {
65 in1.close();
66 in2.close();
67 }
68 }
69
70 public static boolean compare(
71 Class testClass, String file1, String file2, BufferedReader in1, BufferedReader in2) throws IOException {
72
73 String s1;
74 int lineCounter = 0;
75
76 while ((s1 = in1.readLine()) != null) {
77 lineCounter++;
78
79 String s2 = in2.readLine();
80
81 if (!s1.equals(s2)) {
82 System.out.println(
83 "Files [" + file1 + "] and [" + file2 + "] differ on line "
84 + lineCounter);
85 System.out.println("One reads: [" + s1 + "].");
86 System.out.println("Other reads:[" + s2 + "].");
87 outputFile(testClass, file1);
88 outputFile(testClass, file2);
89
90 return false;
91 }
92 }
93
94
95 if (in2.read() != -1) {
96 System.out.println(
97 "File [" + file2 + "] longer than file [" + file1 + "].");
98 outputFile(testClass, file1);
99 outputFile(testClass, file2);
100
101 return false;
102 }
103
104 return true;
105 }
106
107
108
109
110
111
112 private static void outputFile(Class testClass, String file)
113 throws IOException {
114 InputStream is = open(testClass, file);
115 BufferedReader in1 = new BufferedReader(new InputStreamReader(is));
116
117 String s1;
118 int lineCounter = 0;
119 System.out.println("--------------------------------");
120 System.out.println("Contents of " + file + ":");
121
122 while ((s1 = in1.readLine()) != null) {
123 lineCounter++;
124 System.out.print(lineCounter);
125
126 if (lineCounter < 10) {
127 System.out.print(" : ");
128 } else if (lineCounter < 100) {
129 System.out.print(" : ");
130 } else if (lineCounter < 1000) {
131 System.out.print(" : ");
132 } else {
133 System.out.print(": ");
134 }
135
136 System.out.println(s1);
137 }
138 in1.close();
139 }
140
141
142 public static boolean gzCompare(final Class testClass,
143 final String actual,
144 final String expected)
145 throws FileNotFoundException, IOException {
146 String resourceName = expected;
147 int lastSlash = expected.lastIndexOf("/");
148 if (lastSlash >= 0) {
149 resourceName = expected.substring(lastSlash + 1);
150 }
151 InputStream resourceStream = testClass.getResourceAsStream(resourceName);
152 if (resourceStream == null) {
153 throw new FileNotFoundException("Could not locate resource " + resourceName);
154 }
155
156 BufferedReader in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(actual))));
157 BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(resourceStream)));
158 try {
159 return gzCompare(testClass, actual, expected, in1, in2);
160 } finally {
161 in1.close();
162 in2.close();
163 }
164 }
165
166 public static boolean gzCompare(Class testClass, String file1, String file2, BufferedReader in1, BufferedReader in2) throws IOException {
167
168 String s1;
169 int lineCounter = 0;
170
171 while ((s1 = in1.readLine()) != null) {
172 lineCounter++;
173
174 String s2 = in2.readLine();
175
176 if (!s1.equals(s2)) {
177 System.out.println(
178 "Files [" + file1 + "] and [" + file2 + "] differ on line "
179 + lineCounter);
180 System.out.println("One reads: [" + s1 + "].");
181 System.out.println("Other reads:[" + s2 + "].");
182 outputFile(testClass, file1);
183 outputFile(testClass, file2);
184
185 return false;
186 }
187 }
188
189
190 if (in2.read() != -1) {
191 System.out.println(
192 "File [" + file2 + "] longer than file [" + file1 + "].");
193 outputFile(testClass, file1);
194 outputFile(testClass, file2);
195
196 return false;
197 }
198
199 return true;
200 }
201
202 }