X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FTestCase.java;h=490edbb4ec9d47db7f2c24cf21630ac303dc7044;hb=cd0c27d2e457ea19fcd9def879e1534a528292c2;hp=e4860fa11f51e0f5e4cda5d366fffa8db0eef285;hpb=86057589d5211fbad4b7cdbcd4dd0f1e3777d4c1;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index e4860fa..490edbb 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); } @@ -109,8 +113,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,14 +125,15 @@ 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))); } } @@ -150,8 +155,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 @@ -184,8 +189,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 @@ -218,8 +223,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 @@ -233,6 +238,33 @@ abstract public class TestCase { 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.