X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FTestCase.java;h=85a65d7a8cc83e716d499ea201efd481295d1867;hb=edeff2abd7f57e62c478a07c7dc2d138c96f408e;hp=490edbb4ec9d47db7f2c24cf21630ac303dc7044;hpb=cd0c27d2e457ea19fcd9def879e1534a528292c2;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index 490edbb..85a65d7 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -1,5 +1,7 @@ package be.nikiroo.utils.test; +import java.util.List; + /** * A {@link TestCase} that can be run with {@link TestLauncher}. * @@ -36,6 +38,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). * @@ -63,6 +74,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. * @@ -86,12 +111,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); } /** @@ -138,7 +179,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 longs are equals. * * @param expected * the expected value @@ -149,11 +190,11 @@ 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 errorMessage * the error message to display if they differ @@ -167,11 +208,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 @@ -183,11 +224,11 @@ 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 errorMessage * the error message to display if they differ @@ -201,11 +242,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 @@ -217,11 +259,11 @@ 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 errorMessage * the error message to display if they differ @@ -235,7 +277,57 @@ 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("Assertion failed", expected, 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 + * @param errorMessage + * the error message to display if they differ + * + * @throws AssertException + * in case they differ + */ + public void assertEquals(String errorMessage, List expected, + List actual) throws AssertException { + + if (expected.size() != actual.size()) { + assertEquals(errorMessage + ": not same number of items", + list(expected), list(actual)); + } + + int size = expected.size(); + for (int i = 0; i < size; i++) { + assertEquals(errorMessage + ": item " + i + + " (0-based) is not correct", expected.get(i), + actual.get(i)); + } } /** @@ -253,8 +345,8 @@ abstract public class TestCase { throws AssertException { if (actual == null) { String defaultReason = String.format("" // - + "Assertion failed!\n" // - + "Object should have been NULL: [%s]", actual); + + "Assertion failed!%n" // + + "Object should not have been NULL"); if (errorMessage == null) { throw new AssertException(defaultReason); @@ -278,8 +370,29 @@ 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); } + + private static String list(List items) { + StringBuilder builder = new StringBuilder(); + for (Object item : items) { + if (builder.length() == 0) { + builder.append(items.size() + " item(s): "); + } else { + builder.append(", "); + } + + builder.append("" + item); + + if (builder.length() > 60) { + builder.setLength(57); + builder.append("..."); + break; + } + } + + return builder.toString(); + } }