All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.junit.FileTester Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/*
 * $Id: FileTester.java,v 1.7 2013/11/13 20:09:28 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 06.04.2011 by oliver ([email protected])
 */

package patterntesting.runtime.junit;

import java.io.*;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;

import patterntesting.runtime.io.FileHelper;

/**
 * The Class FileTester.
 *
 * @author oliver
 * @since 1.1 (06.04.2011)
 */
public class FileTester {

    /** Utility class - no need to instantiate it. */
    private FileTester() {}

    /**
     * Asserts that the content of two files are equal.
     * If they are not equals the first different line or byte will be shown.
     *
     * @param file1 the first file
     * @param file2 the second file
     * @throws AssertionError if the check fails
     */
    public static void assertContentEquals(final File file1, final File file2)
            throws AssertionError {
        assertContentEquals(file1, file2, Charset.defaultCharset());
    }

    /**
     * Asserts that the content of two files are equal.
     * If they are not equals the first different line or byte will be shown.
     *
     * @param file1 the first file
     * @param file2 the second file
     * @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 File file1, final File file2, final int from, final int to)
            throws AssertionError {
        assertContentEquals(file1, file2, from, to, Charset.defaultCharset());
    }

    /**
     * Asserts that the content of two files are equal.
     * If they are not equals the first different line or byte will be shown.
     * 

* The encoding is only important for the output if the two files are not * equals. *

* * @param file1 the first file * @param file2 the second file * @param encoding the encoding * @throws AssertionError if the check fails */ public static void assertContentEquals(final File file1, final File file2, final String encoding) throws AssertionError { assertContentEquals(file1, file2, Charset.forName(encoding)); } /** * Asserts that the content of two files are equal. * If they are not equals the first different line or byte will be shown. *

* The encoding is only important for the output if the two files are not * equals. *

* * @param file1 the first file * @param file2 the second file * @param from the line number from which the comparison is started * (starting with 1) * @param to the last line number where the comparison ends. * @param encoding the encoding * @throws AssertionError if the check fails */ public static void assertContentEquals(final File file1, final File file2, final int from, final int to, final String encoding) throws AssertionError { assertContentEquals(file1, file2, from, to, Charset.forName(encoding)); } /** * Asserts that the content of two files are equal. * If they are not equals the first different line or byte will be shown. *

* The encoding is only important for the output if the two files are not * equals. *

* * @param file1 the first file * @param file2 the second file * @param encoding the encoding * @throws AssertionError if the check fails */ public static void assertContentEquals(final File file1, final File file2, final Charset encoding) throws AssertionError { try { if (FileUtils.contentEquals(file1, file2)) { return; } assertContentEquals(file1, file2, 1, Integer.MAX_VALUE, encoding); } catch (IOException ioe) { throwAssertionError("can't compare " + file1 + " with " + file2, ioe); } } /** * Asserts that the content of two files are equal. If they are not equals * the first different line or byte will be shown. *

* The encoding is only important for the output if the two files are not * equals. *

* * @param file1 the first file * @param file2 the second file * @param from the line number from which the comparison is started * (starting with 1) * @param to the last line number where the comparison ends. * @param encoding the encoding * @throws AssertionError if the check fails */ public static void assertContentEquals(final File file1, final File file2, final int from, final int to, final Charset encoding) throws AssertionError { try { Reader r1 = new InputStreamReader(new FileInputStream(file1), encoding); Reader r2 = new InputStreamReader(new FileInputStream(file2), encoding); try { IOTester.assertContentEquals(r1, r2, from, to); } finally { r1.close(); r2.close(); } } catch (IOException ioe) { throwAssertionError("can't compare " + file1 + " with " + file2, ioe); } } /** * Asserts that the content of two files are equal. Lines which matches the * given 'ignores' pattern will be ignored for comparison. E.g. if you want * to ignore two property files and want to ignore the comments and empty * lines you could use * *
     *     Pattern.compile("#.*"), Pattern.compile("[ \\t]*"
     * 
* * as ignores parameters. *

* If the two files are not equals the first different line or byte will be * shown. *

* * @param file1 the first file * @param file2 the second file * @param ignores the line pattern which should be ignored * @throws AssertionError if the check fails * @see #assertContentEquals(File, File, Charset, Pattern...) * @since 1.4 */ public static void assertContentEquals(final File file1, final File file2, final Pattern... ignores) throws AssertionError { assertContentEquals(file1, file2, Charset.defaultCharset(), ignores); } /** * Asserts that the content of two files are equal. Lines which matches the * given 'ignores' pattern will be ignored for comparison. E.g. if you want * to ignore two property files and want to ignore the comments and empty * lines you could use * *
     *     Pattern.compile("#.*"), Pattern.compile("[ \\t]*"
     * 
* * as ignores parameters. *

* If the two files are not equals the first different line or byte will be * shown. *

* * @param file1 the first file * @param file2 the second file * @param encoding the file encoding * @param ignores the line pattern which should be ignored * @throws AssertionError if the check fails * @since 1.4 */ public static void assertContentEquals(final File file1, final File file2, final Charset encoding, final Pattern... ignores) throws AssertionError { try { Reader r1 = new InputStreamReader(new FileInputStream(file1), encoding); Reader r2 = new InputStreamReader(new FileInputStream(file2), encoding); try { IOTester.assertContentEquals(r1, r2, ignores); } finally { r1.close(); r2.close(); } } catch (IOException ioe) { throwAssertionError("can't compare " + file1 + " with " + file2, ioe); } } /** * Two files are considered equals if the would point to the same file * location on the disk. * * @param file1 e.g. file "/a/b/c" * @param file2 e.g. file "/a/b/../b/c" */ public static void assertEquals(final File file1, final File file2) { Assert.assertEquals(FileHelper.normalize(file1), FileHelper.normalize(file2)); } private static void throwAssertionError(final String msg, final Throwable t) throws AssertionError { AssertionError error = new AssertionError(msg); error.initCause(t); throw error; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy