SerialTest (array of anonymous classes) + TestCas
authorNiki Roo <niki@nikiroo.be>
Fri, 22 Jun 2018 04:33:21 +0000 (04:33 +0000)
committerNiki Roo <niki@nikiroo.be>
Fri, 22 Jun 2018 04:33:21 +0000 (04:33 +0000)
- TestCase now allow an Execption argument to the "fail(..)" command

src/be/nikiroo/utils/test/SerialTest.java
src/be/nikiroo/utils/test/TestCase.java

index 26571dfcb9755653d0208558548a720c60d84b4f..0221e6523ee63219afda9529daddf19b9588a739 100644 (file)
@@ -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 {
index 0479370f64df57bc5e27a809ee706b0908005f85..85a65d7a8cc83e716d499ea201efd481295d1867 100644 (file)
@@ -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);
        }
 
        /**