From edeff2abd7f57e62c478a07c7dc2d138c96f408e Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 22 Jun 2018 04:33:21 +0000 Subject: [PATCH] SerialTest (array of anonymous classes) + TestCas - TestCase now allow an Execption argument to the "fail(..)" command --- src/be/nikiroo/utils/test/SerialTest.java | 31 +++++++++++++++++++++++ src/be/nikiroo/utils/test/TestCase.java | 18 ++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java index 26571df..0221e65 100644 --- a/src/be/nikiroo/utils/test/SerialTest.java +++ b/src/be/nikiroo/utils/test/SerialTest.java @@ -31,6 +31,7 @@ class SerialTest extends TestLauncher { }); addTest(new TestCase() { + @SuppressWarnings("unused") private TestCase me = setName("Anonymous inner class"); @Override @@ -48,6 +49,36 @@ class SerialTest extends TestLauncher { } }); + addTest(new TestCase() { + @SuppressWarnings("unused") + private TestCase me = setName("Array of anonymous inner classes"); + + @Override + public void test() throws Exception { + Data[] data = new Data[] { new Data() { + } }; + + String encoded = new Exporter().append(data).toString(false); + Object redata = new Importer().read(encoded).getValue(); + String reencoded = new Exporter().append(redata) + .toString(false); + + // Comparing the 2 strings won't be useful, because the @REFs + // will be ZIP-encoded; so we parse and re-encode the object + encoded = new Exporter().append(data[0]).toString(false); + try { + reencoded = new Exporter().append(((Data[]) redata)[0]) + .toString(false); + } catch (Exception e) { + fail("Cannot cast the returned data into its original object", + e); + } + + assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), + reencoded.replaceAll("@[0-9]*", "@REF")); + } + }); + addTest(new TestCase("URL Import/Export") { @Override public void test() throws Exception { diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index 0479370..85a65d7 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -111,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); } /** -- 2.27.0