View Javadoc

    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.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<org.apache.log4j.rewrite.RewriteAppenderTest> 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<org.apache.log4j.rewrite.RewriteAppenderTest> testClass,
    56                                  final String file1,
    57                                  final String file2)
    58      throws IOException {
    59        try (BufferedReader in1 = new BufferedReader(new FileReader(file1));
    60             BufferedReader in2 = new BufferedReader(new InputStreamReader(
    61                open(testClass, file2)))) {
    62            return compare(testClass, file1, file2, in1, in2);
    63        }
    64    }
    65
    66   public static boolean compare(
    67           Class<org.apache.log4j.rewrite.RewriteAppenderTest> testClass, String file1, String file2, BufferedReader in1, BufferedReader in2) throws IOException {
    68
    69      String s1;
    70      int lineCounter = 0;
    71
    72      while ((s1 = in1.readLine()) != null) {
    73        lineCounter++;
    74
    75        String s2 = in2.readLine();
    76
    77        if (!s1.equals(s2)) {
    78          System.out.println(
    79            "Files [" + file1 + "] and [" + file2 + "] differ on line "
    80            + lineCounter);
    81          System.out.println("One reads:  [" + s1 + "].");
    82          System.out.println("Other reads:[" + s2 + "].");
    83          outputFile(testClass, file1);
    84          outputFile(testClass, file2);
    85
    86          return false;
    87        }
    88      }
    89
    90      // the second file is longer
    91      if (in2.read() != -1) {
    92        System.out.println(
    93          "File [" + file2 + "] longer than file [" + file1 + "].");
    94        outputFile(testClass, file1);
    95        outputFile(testClass, file2);
    96
    97        return false;
    98      }
    99
    100     return true;
    101   }
    102
    103   /**
    104    *
    105    * Prints file on the console.
    106    *
    107    */
    108   private static void outputFile(Class<org.apache.log4j.rewrite.RewriteAppenderTest> testClass, String file)
    109     throws IOException {
    110     InputStream is = open(testClass, file);
    111     BufferedReader in1 = new BufferedReader(new InputStreamReader(is));
    112
    113     String s1;
    114     int lineCounter = 0;
    115     System.out.println("--------------------------------");
    116     System.out.println("Contents of " + file + ":");
    117
    118     while ((s1 = in1.readLine()) != null) {
    119       lineCounter++;
    120       System.out.print(lineCounter);
    121
    122       if (lineCounter < 10) {
    123         System.out.print("   : ");
    124       } else if (lineCounter < 100) {
    125         System.out.print("  : ");
    126       } else if (lineCounter < 1000) {
    127         System.out.print(" : ");
    128       } else {
    129         System.out.print(": ");
    130       }
    131
    132       System.out.println(s1);
    133     }
    134     in1.close();
    135   }
    136
    137
    138     public static boolean gzCompare(final Class<org.apache.log4j.rewrite.RewriteAppenderTest> testClass,
    139                                     final String actual,
    140                                     final String expected)
    141       throws IOException {
    142       String resourceName = expected;
    143       int lastSlash = expected.lastIndexOf("/");
    144       if (lastSlash >= 0) {
    145           resourceName = expected.substring(lastSlash + 1);
    146       }
    147       InputStream resourceStream = testClass.getResourceAsStream(resourceName);
    148       if (resourceStream == null) {
    149           throw new FileNotFoundException("Could not locate resource " + resourceName);
    150       }
    151
    152         try (BufferedReader in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(actual))));
    153              BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(resourceStream)))) {
    154             return gzCompare(testClass, actual, expected, in1, in2);
    155         }
    156     }
    157
    158     public static boolean gzCompare(Class<org.apache.log4j.rewrite.RewriteAppenderTest> testClass, String file1, String file2, BufferedReader in1, BufferedReader in2) throws IOException {
    159
    160       String s1;
    161       int lineCounter = 0;
    162
    163       while ((s1 = in1.readLine()) != null) {
    164         lineCounter++;
    165
    166         String s2 = in2.readLine();
    167
    168         if (!s1.equals(s2)) {
    169           System.out.println(
    170             "Files [" + file1 + "] and [" + file2 + "] differ on line "
    171             + lineCounter);
    172           System.out.println("One reads:  [" + s1 + "].");
    173           System.out.println("Other reads:[" + s2 + "].");
    174           outputFile(testClass, file1);
    175           outputFile(testClass, file2);
    176
    177           return false;
    178         }
    179       }
    180
    181       // the second file is longer
    182       if (in2.read() != -1) {
    183         System.out.println(
    184           "File [" + file2 + "] longer than file [" + file1 + "].");
    185         outputFile(testClass, file1);
    186         outputFile(testClass, file2);
    187
    188         return false;
    189       }
    190
    191       return true;
    192     }
    193
    194 }