Fix text justification and linked tests
[nikiroo-utils.git] / src / be / nikiroo / utils / test / TestCase.java
index e4860fa11f51e0f5e4cda5d366fffa8db0eef285..5cff3630a725aa1e5fb67301ff1518875589a96c 100644 (file)
@@ -1,5 +1,7 @@
 package be.nikiroo.utils.test;
 
+import java.util.List;
+
 /**
  * A {@link TestCase} that can be run with {@link TestLauncher}.
  * 
@@ -15,6 +17,10 @@ abstract public class TestCase {
        class AssertException extends Exception {
                private static final long serialVersionUID = 1L;
 
+               public AssertException(String reason, Exception source) {
+                       super(reason, source);
+               }
+
                public AssertException(String reason) {
                        super(reason);
                }
@@ -38,6 +44,7 @@ abstract public class TestCase {
         * @throws Exception
         *             in case of error
         */
+       @SuppressWarnings("unused")
        public void setUp() throws Exception {
        }
 
@@ -47,6 +54,7 @@ abstract public class TestCase {
         * @throws Exception
         *             in case of error
         */
+       @SuppressWarnings("unused")
        public void tearDown() throws Exception {
        }
 
@@ -109,8 +117,8 @@ abstract public class TestCase {
        /**
         * Check that 2 {@link Object}s are equals.
         * 
-        * @param the
-        *            error message to display if they differ
+        * @param errorMessage
+        *            the error message to display if they differ
         * @param expected
         *            the expected value
         * @param actual
@@ -121,19 +129,20 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, Object expected, Object actual)
                        throws AssertException {
-
-               if (errorMessage == null) {
-                       errorMessage = generateAssertMessage(expected, actual);
-               }
-
                if ((expected == null && actual != null)
                                || (expected != null && !expected.equals(actual))) {
-                       throw new AssertException(errorMessage);
+                       if (errorMessage == null) {
+                               throw new AssertException(generateAssertMessage(expected,
+                                               actual));
+                       }
+
+                       throw new AssertException(errorMessage, new AssertException(
+                                       generateAssertMessage(expected, actual)));
                }
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 longs are equals.
         * 
         * @param expected
         *            the expected value
@@ -144,14 +153,14 @@ abstract public class TestCase {
         *             in case they differ
         */
        public void assertEquals(long expected, long actual) throws AssertException {
-               assertEquals(new Long(expected), new Long(actual));
+               assertEquals(Long.valueOf(expected), Long.valueOf(actual));
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 longs are equals.
         * 
-        * @param the
-        *            error message to display if they differ
+        * @param errorMessage
+        *            the error message to display if they differ
         * @param expected
         *            the expected value
         * @param actual
@@ -162,11 +171,11 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, long expected, long actual)
                        throws AssertException {
-               assertEquals(errorMessage, new Long(expected), new Long(actual));
+               assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual));
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 booleans are equals.
         * 
         * @param expected
         *            the expected value
@@ -178,14 +187,14 @@ abstract public class TestCase {
         */
        public void assertEquals(boolean expected, boolean actual)
                        throws AssertException {
-               assertEquals(new Boolean(expected), new Boolean(actual));
+               assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 booleans are equals.
         * 
-        * @param the
-        *            error message to display if they differ
+        * @param errorMessage
+        *            the error message to display if they differ
         * @param expected
         *            the expected value
         * @param actual
@@ -196,11 +205,12 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, boolean expected,
                        boolean actual) throws AssertException {
-               assertEquals(errorMessage, new Boolean(expected), new Boolean(actual));
+               assertEquals(errorMessage, Boolean.valueOf(expected),
+                               Boolean.valueOf(actual));
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 doubles are equals.
         * 
         * @param expected
         *            the expected value
@@ -212,14 +222,14 @@ abstract public class TestCase {
         */
        public void assertEquals(double expected, double actual)
                        throws AssertException {
-               assertEquals(new Double(expected), new Double(actual));
+               assertEquals(Double.valueOf(expected), Double.valueOf(actual));
        }
 
        /**
-        * Check that 2 {@link Object}s are equals.
+        * Check that 2 doubles are equals.
         * 
-        * @param the
-        *            error message to display if they differ
+        * @param errorMessage
+        *            the error message to display if they differ
         * @param expected
         *            the expected value
         * @param actual
@@ -230,7 +240,61 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, double expected, double actual)
                        throws AssertException {
-               assertEquals(errorMessage, new Double(expected), new Double(actual));
+               assertEquals(errorMessage, Double.valueOf(expected),
+                               Double.valueOf(actual));
+       }
+
+       /**
+        * Check that 2 {@link List}s are equals.
+        * 
+        * @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(List<?> expected, List<?> actual)
+                       throws AssertException {
+
+               assertEquals("The 2 lists don't contain the same number of items",
+                               expected.size(), actual.size());
+
+               int size = expected.size();
+               for (int i = 0; i < size; i++) {
+                       assertEquals("Line " + i + " (0-based) is not correct",
+                                       expected.get(i), actual.get(i));
+               }
+       }
+
+       /**
+        * Check that given {@link Object} is not NULL.
+        * 
+        * @param errorMessage
+        *            the error message to display if it is NULL
+        * @param actual
+        *            the actual value
+        * 
+        * @throws AssertException
+        *             in case they differ
+        */
+       public void assertNotNull(String errorMessage, Object actual)
+                       throws AssertException {
+               if (actual == null) {
+                       String defaultReason = String.format("" //
+                                       + "Assertion failed!%n" //
+                                       + "Object should not have been NULL");
+
+                       if (errorMessage == null) {
+                               throw new AssertException(defaultReason);
+                       }
+
+                       throw new AssertException(errorMessage, new AssertException(
+                                       defaultReason));
+               }
        }
 
        /**
@@ -246,8 +310,8 @@ abstract public class TestCase {
         */
        public static String generateAssertMessage(Object expected, Object actual) {
                return String.format("" //
-                               + "Assertion failed!\n" //
-                               + "Expected value: [%s]\n" //
+                               + "Assertion failed!%n" //
+                               + "Expected value: [%s]%n" //
                                + "Actual value: [%s]", expected, actual);
        }
 }