Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* $Id: IOTester.java,v 1.7 2014/05/04 18:34:33 oboehm Exp $
*
* Copyright (c) 2011 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 30.03.2011 by oliver ([email protected])
*/
package patterntesting.runtime.junit;
import java.io.*;
import java.nio.charset.Charset;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.slf4j.*;
import patterntesting.annotation.check.runtime.MayReturnNull;
import patterntesting.runtime.io.LineReader;
/**
* With the IOTester you can assert if two files have equals content or
* if two directories have the same structure with equal files.
*
* Note: This test is not yet tested with binary files.
*
* @author oliver
* @since 1.1 (30.03.2011)
*/
public final class IOTester {
private static final Logger log = LoggerFactory.getLogger(IOTester.class);
/** Utility class - no need to instantiate it. */
private IOTester() {}
/**
* Asserts that the content of two InputStream are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2)
throws AssertionError {
assertContentEquals(new InputStreamReader(in1), new InputStreamReader(in2));
}
/**
* Asserts that the content of two InputStream are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @param encoding the encoding
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2,
final Charset encoding) throws AssertionError {
assertContentEquals(in1, in2, Charset.defaultCharset());
}
/**
* Asserts that the content of two InputStream are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @param from the line number from which the comparison is started
* (starting with 1)
* @param to the last line number where the comparison ends.
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2,
final int from, final int to) throws AssertionError {
assertContentEquals(in1, in2, Charset.defaultCharset(), from, to);
}
/**
* Asserts that the content of two InputStream are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @param encoding the encoding
* @param from the line number from which the comparison is started
* (starting with 1)
* @param to the last line number where the comparison ends.
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2,
final Charset encoding, final int from, final int to) throws AssertionError {
assertContentEquals(new InputStreamReader(in1, encoding), new InputStreamReader(in2,
encoding), from, to);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @param ignores the lines matching this pattern will be ignored
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2,
final Pattern... ignores) throws AssertionError {
assertContentEquals(in1, in2, Charset.defaultCharset(), ignores);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param in1 the first InputStream
* @param in2 the second InputStream
* @param encoding the encoding
* @param ignores the lines matching this pattern will be ignored
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final InputStream in1, final InputStream in2,
final Charset encoding, final Pattern... ignores) throws AssertionError {
assertContentEquals(new InputStreamReader(in1, encoding), new InputStreamReader(in2,
encoding), ignores);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @throws AssertionError if the check fails
*/
// TODO: shold work also for binary files
public static void assertContentEquals(final Reader r1, final Reader r2) throws AssertionError {
assertContentEquals(new BufferedReader(r1), new BufferedReader(r2));
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @param from the line number from which the comparison is started
* (starting with 1)
* @param to the last line number where the comparison ends.
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final Reader r1, final Reader r2, final int from,
final int to) throws AssertionError {
assertContentEquals(new LineReader(r1), new LineReader(r2), from, to);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @param ignores the lines matching this pattern will be ignored
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final Reader r1, final Reader r2,
final Pattern... ignores) throws AssertionError {
assertContentEquals(new LineReader(r1), new LineReader(r2), ignores);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final BufferedReader r1, final BufferedReader r2)
throws AssertionError {
assertContentEquals(r1, r2, 1, Integer.MAX_VALUE);
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @param from the line number from which the comparison is started
* (starting with 1)
* @param to the last line number where the comparison ends.
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final LineReader r1, final LineReader r2,
final int from, final int to) throws AssertionError {
try {
r1.skipLines(from - 1);
r2.skipLines(from - 1);
for (int lineNo = from; lineNo <= to; lineNo++) {
String line1 = r1.readLine();
String line2 = r2.readLine();
if ((line1 == null) && (line2 == null)) {
return;
}
Assert.assertEquals("line " + lineNo + " -", line1, line2);
}
} catch (IOException ioe) {
throw new AssertionError(ioe);
}
}
/**
* Asserts that the content of two Readers are equal.
* If they are not equals the first different line or byte will be shown.
*
* @param r1 the first Reader
* @param r2 the second Reader
* @param ignores the lines matching this pattern will be ignored
* @throws AssertionError if the check fails
*/
public static void assertContentEquals(final LineReader r1, final LineReader r2,
final Pattern... ignores) throws AssertionError {
try {
for (;;) {
String line1 = nextLine(r1, ignores);
String line2 = nextLine(r2, ignores);
if ((line1 == null) && (line2 == null)) {
return;
}
if (r1.getLineNumber() == r2.getLineNumber()) {
Assert.assertEquals("line " + r1.getLineNumber() + " -", line1, line2);
} else {
Assert.assertEquals("line " + r1.getLineNumber() + "/" + r2.getLineNumber() + " -", line1, line2);
}
}
} catch (IOException ioe) {
throw new AssertionError(ioe);
}
}
@MayReturnNull
private static String nextLine(final BufferedReader reader, final Pattern... ignores) throws IOException {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (matches(line, ignores)) {
log.trace("\"{}\" maches {} and is ignored.", line, ignores);
} else {
return line;
}
}
return null;
}
private static boolean matches(final String line, final Pattern[] ignores) {
for (int i = 0; i < ignores.length; i++) {
if (ignores[i].matcher(line).matches()) {
return true;
}
}
return false;
}
}