001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Julien Carsique
016 *
017 */
018
019package org.nuxeo.runtime.test;
020
021import org.junit.ComparisonFailure;
022
023import org.nuxeo.common.utils.FileUtils;
024
025/**
026 * Extension to {@link org.junit.Assert}
027 *
028 * @since 6.0
029 */
030public class Assert {
031
032    /**
033     * Protect constructor since it is a static only class
034     */
035    protected Assert() {
036    }
037
038    /**
039     * Asserts that two strings are equal even if their EOL are different. If they are not, an {@link AssertionError} is
040     * thrown with the given message. If <code>expected</code> and <code>actual</code> are <code>null</code>, they are
041     * considered equal.
042     *
043     * @param expected expected String with Windows or Unix like EOL
044     * @param actual actual String with Windows or Unix like EOL
045     * @see FileUtils#areFilesContentEquals(String, String)
046     */
047    static public void assertFilesContentEquals(String expected, String actual) {
048        assertFilesContentEquals(null, expected, actual);
049    }
050
051    /**
052     * Asserts that two strings are equal even if their EOL are different. If they are not, an {@link AssertionError} is
053     * thrown with the given message. If <code>expected</code> and <code>actual</code> are <code>null</code>, they are
054     * considered equal.
055     *
056     * @param message the identifying message for the {@link AssertionError} ( <code>null</code> okay)
057     * @param expected expected String with Windows or Unix like EOL
058     * @param actual actual String with Windows or Unix like EOL
059     * @see FileUtils#areFilesContentEquals(String, String)
060     */
061    static public void assertFilesContentEquals(String message, String expected, String actual) {
062        if (FileUtils.areFilesContentEquals(expected, actual)) {
063            return;
064        } else {
065            String cleanMessage = message == null ? "" : message;
066            throw new ComparisonFailure(cleanMessage, expected, actual);
067        }
068    }
069}