X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FTestCase.java;h=9d0caaa9026ea16530360f57966ccaab584f6b45;hb=79ce1a4973eba079ba819ba841d906de42f38e40;hp=0349bc07c5cdcb3df3b6e90e92dd10d9febaba6f;hpb=80383c142f85a7850d0fbea418689608fdccac05;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index 0349bc0..9d0caaa 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -15,6 +15,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 +42,7 @@ abstract public class TestCase { * @throws Exception * in case of error */ + @SuppressWarnings("unused") public void setUp() throws Exception { } @@ -47,6 +52,7 @@ abstract public class TestCase { * @throws Exception * in case of error */ + @SuppressWarnings("unused") public void tearDown() throws Exception { } @@ -109,8 +115,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,17 +127,15 @@ abstract public class TestCase { */ public void assertEquals(String errorMessage, Object expected, Object actual) throws AssertException { - - if (errorMessage == null) { - errorMessage = String.format("" // - + "Assertion failed!\n" // - + "Expected value: [%s]\n" // - + "Actual value: [%s]", 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))); } } @@ -153,8 +157,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 @@ -187,8 +191,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 @@ -221,8 +225,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 @@ -235,4 +239,49 @@ abstract public class TestCase { throws AssertException { assertEquals(errorMessage, new Double(expected), new Double(actual)); } + + /** + * 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 have been NULL: [%s]", actual); + + if (errorMessage == null) { + throw new AssertException(defaultReason); + } + + throw new AssertException(errorMessage, new AssertException( + defaultReason)); + } + } + + /** + * Generate the default assert message for 2 different values that were + * supposed to be equals. + * + * @param expected + * the expected value + * @param actual + * the actual value + * + * @return the message + */ + public static String generateAssertMessage(Object expected, Object actual) { + return String.format("" // + + "Assertion failed!\n" // + + "Expected value: [%s]\n" // + + "Actual value: [%s]", expected, actual); + } }