X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FTestCase.java;h=85a65d7a8cc83e716d499ea201efd481295d1867;hb=edeff2abd7f57e62c478a07c7dc2d138c96f408e;hp=a62cb0b79646752fb9b37ffca7c4173dc795cac7;hpb=0988831f084e27de9927c1bb29e338e9263bfa42;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index a62cb0b..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,13 +38,21 @@ 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). * * @throws Exception * in case of error */ - @SuppressWarnings("unused") public void setUp() throws Exception { } @@ -52,7 +62,6 @@ abstract public class TestCase { * @throws Exception * in case of error */ - @SuppressWarnings("unused") public void tearDown() throws Exception { } @@ -65,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. * @@ -88,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); } /** @@ -140,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 @@ -155,7 +194,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 longs are equals. * * @param errorMessage * the error message to display if they differ @@ -173,7 +212,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 booleans are equals. * * @param expected * the expected value @@ -189,7 +228,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 booleans are equals. * * @param errorMessage * the error message to display if they differ @@ -208,7 +247,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 doubles are equals. * * @param expected * the expected value @@ -224,7 +263,7 @@ abstract public class TestCase { } /** - * Check that 2 {@link Object}s are equals. + * Check that 2 doubles are equals. * * @param errorMessage * the error message to display if they differ @@ -242,6 +281,55 @@ abstract public class TestCase { 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)); + } + } + /** * Check that given {@link Object} is not NULL. * @@ -286,4 +374,25 @@ abstract public class TestCase { + "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(); + } }