Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / test / TestCase.java
index 4e7d3800c5a588bf6a1b06f341bfc5d3c2c8c2ac..fe7b9af5e141d5f750be96b5c692b51484158f4f 100644 (file)
@@ -1,6 +1,13 @@
 package be.nikiroo.utils.test;
 
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
 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}.
@@ -38,6 +45,15 @@ abstract public class TestCase {
                this.name = name;
        }
 
+       /**
+        * This constructor can be used if you require a no-param constructor. In
+        * this case, you are allowed to set the name manually via
+        * {@link TestCase#setName}.
+        */
+       protected TestCase() {
+               this("no name");
+       }
+
        /**
         * Setup the test (called before the test is run).
         * 
@@ -65,6 +81,20 @@ abstract public class TestCase {
                return name;
        }
 
+       /**
+        * The test name.
+        * 
+        * @param name
+        *            the new name (internal use only)
+        * 
+        * @return this (so we can chain and so we can initialize it in a member
+        *         variable if this is an anonymous inner class)
+        */
+       protected TestCase setName(String name) {
+               this.name = name;
+               return this;
+       }
+
        /**
         * Actually do the test.
         * 
@@ -88,12 +118,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);
        }
 
        /**
@@ -269,8 +315,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
@@ -291,6 +335,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<String, List<String>> skipCompare) throws AssertException {
+               assertEquals(errorMessage, expected, actual, skipCompare, null);
+       }
+
+       private void assertEquals(String errorMessage, File expected, File actual,
+                       Map<String, List<String>> 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<String> expectedFiles = Arrays.asList(expected.list());
+                       Collections.sort(expectedFiles);
+                       List<String> actualFiles = Arrays.asList(actual.list());
+                       Collections.sort(actualFiles);
+
+                       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<String> expectedLines = Arrays.asList(IOUtils
+                                               .readSmallFile(expected).split("\n"));
+                               List<String> 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<String, List<String>> 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.
         * 
@@ -333,7 +509,7 @@ abstract public class TestCase {
                return String.format("" //
                                + "Assertion failed!%n" //
                                + "Expected value: [%s]%n" //
-                               + "Actual value: [%s]", expected, actual);
+                               + "Actual value:   [%s]", expected, actual);
        }
 
        private static String list(List<?> items) {