| 1 |
// Copyright 2002-2005, FreeHEP. |
| 2 |
package org.freehep.graphicsbase.util; |
| 3 |
|
| 4 |
import java.io.BufferedInputStream; |
| 5 |
import java.io.BufferedReader; |
| 6 |
import java.io.File; |
| 7 |
import java.io.FileInputStream; |
| 8 |
import java.io.FileNotFoundException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.InputStreamReader; |
| 11 |
import java.io.IOException; |
| 12 |
import java.util.zip.GZIPInputStream; |
| 13 |
|
| 14 |
import junit.framework.AssertionFailedError; |
| 15 |
|
| 16 |
/** |
| 17 |
* Additional assert methods to the junit.framework.Assert class. |
| 18 |
* |
| 19 |
* @author Mark Donszelmann |
| 20 |
* @version $Id: Assert.java 8584 2006-08-10 23:06:37Z duns $ |
| 21 |
*/ |
| 22 |
public class Assert extends junit.framework.Assert { |
| 23 |
|
| 24 |
/** |
| 25 |
* static class only |
| 26 |
*/ |
| 27 |
protected Assert() { |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Compares two files. The files may be gzipped, and will then be uncompressed on |
| 32 |
* the fly. |
| 33 |
* |
| 34 |
* @param expected reference file |
| 35 |
* @param actual file to be tested |
| 36 |
* @param isBinary when true will do byte-by-byte comparison rather than line by line. |
| 37 |
* @throws FileNotFoundException if one of the files cannot be found. |
| 38 |
* @throws IOException if one of the files cannot read. |
| 39 |
* @throws AssertionFailedError if the files are not equal. |
| 40 |
*/ |
| 41 |
public static void assertEquals(File expected, File actual, boolean isBinary) |
| 42 |
throws FileNotFoundException, IOException { |
| 43 |
InputStream ref = new BufferedInputStream(new FileInputStream(expected)); |
| 44 |
if (expected.getPath().toLowerCase().endsWith(".gz")) { |
| 45 |
ref = new GZIPInputStream(ref); |
| 46 |
} |
| 47 |
|
| 48 |
InputStream tst = new BufferedInputStream(new FileInputStream(actual)); |
| 49 |
if (actual.getPath().toLowerCase().endsWith(".gz")) { |
| 50 |
tst = new GZIPInputStream(tst); |
| 51 |
} |
| 52 |
|
| 53 |
assertEquals(ref, tst, isBinary, "File "+actual.getPath()); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Compares two streams. |
| 58 |
* |
| 59 |
* @param expected reference stream |
| 60 |
* @param actual stream to be tested |
| 61 |
* @param filename an prefix for the error message (normally filename associated with the actual stream) |
| 62 |
* @param isBinary when true will do byte-by-byte comparison rather than line by line (Reader). |
| 63 |
* @throws IOException if one of the streams cannot read. |
| 64 |
* @throws AssertionFailedError if the streams are not equal. |
| 65 |
*/ |
| 66 |
public static void assertEquals(InputStream expected, InputStream actual, boolean isBinary, String filename) throws IOException { |
| 67 |
int diff; |
| 68 |
if (isBinary) { |
| 69 |
diff = diff (expected, actual); |
| 70 |
if (diff >= 0) throw new AssertionFailedError(filename+": comparison failed at offset "+diff); |
| 71 |
} else { |
| 72 |
diff = diff(new BufferedReader(new InputStreamReader(expected)), |
| 73 |
new BufferedReader(new InputStreamReader(actual))); |
| 74 |
if (diff >= 0) throw new AssertionFailedError(filename+": comparison failed at line "+diff); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
private static int diff(InputStream ref, InputStream tst) throws IOException { |
| 79 |
int bRef, bTst; |
| 80 |
int i = 0; |
| 81 |
do { |
| 82 |
bRef = ref.read(); |
| 83 |
bTst = tst.read(); |
| 84 |
i++; |
| 85 |
} while ((bRef >= 0) && (bTst >= 0) && (bRef == bTst)); |
| 86 |
ref.close(); |
| 87 |
tst.close(); |
| 88 |
return (bRef == bTst) ? -1 : i-1; |
| 89 |
} |
| 90 |
|
| 91 |
private static int diff(BufferedReader ref, BufferedReader tst) throws IOException { |
| 92 |
String bRef, bTst; |
| 93 |
int i = 1; |
| 94 |
do { |
| 95 |
bRef = ref.readLine(); |
| 96 |
bTst = tst.readLine(); |
| 97 |
i++; |
| 98 |
} while ((bRef != null) && (bTst != null) && (bRef.equals(bTst))); |
| 99 |
ref.close(); |
| 100 |
tst.close(); |
| 101 |
|
| 102 |
return ((bRef == null) && (bTst == null)) ? -1 : i-1; |
| 103 |
} |
| 104 |
} |