X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FTestCase.java;h=2e46a7b5472af4eabc2a5ecb4aea4e4d6d6fe89c;hb=3232fdd1fffa919f98eca33f90f8a3b69c6f7ed2;hp=0479370f64df57bc5e27a809ee706b0908005f85;hpb=72648e757f648cd152bc00dfb83f895260f037a0;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index 0479370..2e46a7b 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -1,6 +1,12 @@ package be.nikiroo.utils.test; +import java.io.File; +import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import be.nikiroo.utils.IOUtils; /** * A {@link TestCase} that can be run with {@link TestLauncher}. @@ -111,12 +117,28 @@ abstract public class TestCase { * * @param reason * the failure reason + * * @throws AssertException * every time */ public void fail(String reason) throws AssertException { + fail(reason, null); + } + + /** + * Force a failure. + * + * @param reason + * the failure reason + * @param e + * the exception that caused the failure (can be NULL) + * + * @throws AssertException + * every time + */ + public void fail(String reason, Exception e) throws AssertException { throw new AssertException("Failed!" + // - reason != null ? "\n" + reason : ""); + reason != null ? "\n" + reason : "", e); } /** @@ -292,8 +314,6 @@ abstract public class TestCase { * the expected value * @param actual * the actual value - * @param errorMessage - * the error message to display if they differ * * @throws AssertException * in case they differ @@ -314,6 +334,138 @@ abstract public class TestCase { } } + /** + * Check that 2 {@link File}s are equals, by doing a line-by-line + * comparison. + * + * @param expected + * the expected value + * @param actual + * the actual value + * @param errorMessage + * the error message to display if they differ + * + * @throws AssertException + * in case they differ + */ + public void assertEquals(File expected, File actual) throws AssertException { + assertEquals(generateAssertMessage(expected, actual), expected, actual); + } + + /** + * Check that 2 {@link File}s are equals, by doing a line-by-line + * comparison. + * + * @param errorMessage + * the error message to display if they differ + * @param expected + * the expected value + * @param actual + * the actual value + * + * @throws AssertException + * in case they differ + */ + public void assertEquals(String errorMessage, File expected, File actual) + throws AssertException { + assertEquals(errorMessage, expected, actual, null); + } + + /** + * Check that 2 {@link File}s are equals, by doing a line-by-line + * comparison. + * + * @param errorMessage + * the error message to display if they differ + * @param expected + * the expected value + * @param actual + * the actual value + * @param skipCompare + * skip the lines starting with some values for the given files + * (relative path from base directory in recursive mode) + * + * @throws AssertException + * in case they differ + */ + public void assertEquals(String errorMessage, File expected, File actual, + Map> skipCompare) throws AssertException { + assertEquals(errorMessage, expected, actual, skipCompare, null); + } + + private void assertEquals(String errorMessage, File expected, File actual, + Map> skipCompare, String removeFromName) + throws AssertException { + + if (expected.isDirectory() || actual.isDirectory()) { + assertEquals(errorMessage + ": type mismatch: expected a " + + (expected.isDirectory() ? "directory" : "file") + + ", received a " + + (actual.isDirectory() ? "directory" : "file"), + expected.isDirectory(), actual.isDirectory()); + + List expectedFiles = Arrays.asList(expected.list()); + expectedFiles.sort(null); + List actualFiles = Arrays.asList(actual.list()); + actualFiles.sort(null); + + assertEquals(errorMessage, expectedFiles, actualFiles); + for (int i = 0; i < actualFiles.size(); i++) { + File expectedFile = new File(expected, expectedFiles.get(i)); + File actualFile = new File(actual, actualFiles.get(i)); + + assertEquals(errorMessage, expectedFile, actualFile, + skipCompare, expected.getAbsolutePath()); + } + } else { + try { + List expectedLines = Arrays.asList(IOUtils + .readSmallFile(expected).split("\n")); + List resultLines = Arrays.asList(IOUtils.readSmallFile( + actual).split("\n")); + + String name = expected.getAbsolutePath(); + if (removeFromName != null && name.startsWith(removeFromName)) { + name = expected.getName() + + name.substring(removeFromName.length()); + } + + assertEquals(errorMessage + ": " + name + + ": the number of lines is not the same", + expectedLines.size(), resultLines.size()); + + for (int j = 0; j < expectedLines.size(); j++) { + String expectedLine = expectedLines.get(j); + String resultLine = resultLines.get(j); + + boolean skip = false; + if (skipCompare != null) { + for (Entry> skipThose : skipCompare + .entrySet()) { + for (String skipStart : skipThose.getValue()) { + if (name.endsWith(skipThose.getKey()) + && expectedLine.startsWith(skipStart) + && resultLine.startsWith(skipStart)) { + skip = true; + } + } + } + } + + if (skip) { + continue; + } + + assertEquals(errorMessage + ": line " + (j + 1) + + " is not the same in file " + name, expectedLine, + resultLine); + } + } catch (Exception e) { + throw new AssertException(errorMessage, e); + } + } + } + /** * Check that given {@link Object} is not NULL. *