1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.util;
19  
20  import java.io.BufferedReader;
21  import java.io.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.FileInputStream;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.io.File;
28  
29  
30  public class Compare {
31  
32    static final int B1_NULL = -1;
33    static final int B2_NULL = -2;
34    
35    static 
36    public
37    boolean compare(String file1, String file2) throws FileNotFoundException, 
38                                                              IOException {
39      BufferedReader in1 = new BufferedReader(new FileReader(file1));
40      BufferedReader in2 = new BufferedReader(new FileReader(file2));
41      
42      String s1;
43      int lineCounter = 0;
44      while((s1 = in1.readLine()) != null) {
45        lineCounter++;
46        String s2 = in2.readLine();
47        if(!s1.equals(s2)) {
48  	System.out.println("Files ["+file1+"] and ["+file2+"] differ on line " 
49  			   +lineCounter);
50  	System.out.println("One reads:  ["+s1+"].");
51  	System.out.println("Other reads:["+s2+"].");
52  	return false;
53        }
54      }
55      
56      // the second file is longer
57      if(in2.read() != -1) {
58        System.out.println("File ["+file2+"] longer than file ["+file1+"].");
59        return false;
60      }
61      
62      return true;
63    }
64  
65  private static final InputStream open(
66            final Class testClass,
67            final String fileName) throws IOException {
68        String resourceName = fileName;
69        if (fileName.startsWith("witness/")) {
70            resourceName = fileName.substring(fileName.lastIndexOf('/') + 1);
71        }
72        InputStream is = testClass.getResourceAsStream(resourceName);
73        if (is == null) {
74            File file = new File(fileName);
75            if (file.exists()) {
76                is = new FileInputStream(file);
77            } else {
78                throw new FileNotFoundException("Resource "
79                        + resourceName + " not found");
80            }
81        }
82        return is;
83    }
84  
85    public static boolean compare(Class testClass,
86                                  final String file1,
87                                  final String file2)
88      throws IOException {
89      BufferedReader in1 = new BufferedReader(new FileReader(file1));
90      BufferedReader in2 = new BufferedReader(new InputStreamReader(
91              open(testClass, file2)));
92      try {
93        return compare(testClass, file1, file2, in1, in2);
94      } finally {
95        in1.close();
96        in2.close();
97      }
98    }
99      
100  public static boolean compare(
101          Class testClass, String file1, String file2, BufferedReader in1, BufferedReader in2) throws IOException {
102 
103     String s1;
104     int lineCounter = 0;
105 
106     while ((s1 = in1.readLine()) != null) {
107       lineCounter++;
108 
109       String s2 = in2.readLine();
110 
111       if (!s1.equals(s2)) {
112         System.out.println(
113           "Files [" + file1 + "] and [" + file2 + "] differ on line "
114           + lineCounter);
115         System.out.println("One reads:  [" + s1 + "].");
116         System.out.println("Other reads:[" + s2 + "].");
117         outputFile(testClass, file1);
118         outputFile(testClass, file2);
119 
120         return false;
121       }
122     }
123 
124     // the second file is longer
125     if (in2.read() != -1) {
126       System.out.println(
127         "File [" + file2 + "] longer than file [" + file1 + "].");
128       outputFile(testClass, file1);
129       outputFile(testClass, file2);
130 
131       return false;
132     }
133 
134     return true;
135   }
136 
137   /** 
138    * 
139    * Prints file on the console.
140    *
141    */
142   private static void outputFile(Class testClass, String file)
143     throws IOException {
144     InputStream is = open(testClass, file);
145     BufferedReader in1 = new BufferedReader(new InputStreamReader(is));
146 
147     String s1;
148     int lineCounter = 0;
149     System.out.println("--------------------------------");
150     System.out.println("Contents of " + file + ":");
151 
152     while ((s1 = in1.readLine()) != null) {
153       lineCounter++;
154       System.out.print(lineCounter);
155 
156       if (lineCounter < 10) {
157         System.out.print("   : ");
158       } else if (lineCounter < 100) {
159         System.out.print("  : ");
160       } else if (lineCounter < 1000) {
161         System.out.print(" : ");
162       } else {
163         System.out.print(": ");
164       }
165 
166       System.out.println(s1);
167     }
168     in1.close();
169   }
170 
171   
172 }