Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[fanfix.git] / src / be / nikiroo / utils / test / TestLauncher.java
index ba4e81de22262f23a8e9161c2b2b81fec4413c15..895b565ae0f607522e494d92c8bc87541c672f51 100644 (file)
@@ -1,5 +1,7 @@
 package be.nikiroo.utils.test;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -18,7 +20,7 @@ public class TestLauncher {
        private class SetupException extends Exception {
                private static final long serialVersionUID = 1L;
 
-               public SetupException(Exception e) {
+               public SetupException(Throwable e) {
                        super(e);
                }
        }
@@ -31,13 +33,15 @@ public class TestLauncher {
        private class TearDownException extends Exception {
                private static final long serialVersionUID = 1L;
 
-               public TearDownException(Exception e) {
+               public TearDownException(Throwable e) {
                        super(e);
                }
        }
 
        private List<TestLauncher> series;
        private List<TestCase> tests;
+       private TestLauncher parent;
+
        private int columns;
        private String okString;
        private String koString;
@@ -47,6 +51,9 @@ public class TestLauncher {
        protected int executed;
        protected int total;
 
+       private int currentSeries = 0;
+       private boolean details = false;
+
        /**
         * Create a new {@link TestLauncher} with default parameters.
         * 
@@ -86,6 +93,33 @@ public class TestLauncher {
                cont = true;
        }
 
+       /**
+        * Display the details of the errors
+        * 
+        * @return TRUE to display them, false to simply mark the test as failed
+        */
+       public boolean isDetails() {
+               if (parent != null) {
+                       return parent.isDetails();
+               }
+
+               return details;
+       }
+
+       /**
+        * Display the details of the errors
+        * 
+        * @param details
+        *            TRUE to display them, false to simply mark the test as failed
+        */
+       public void setDetails(boolean details) {
+               if (parent != null) {
+                       parent.setDetails(details);
+               }
+
+               this.details = details;
+       }
+
        /**
         * Called before actually starting the tests themselves.
         * 
@@ -110,6 +144,7 @@ public class TestLauncher {
 
        protected void addSeries(TestLauncher series) {
                this.series.add(series);
+               series.parent = this;
        }
 
        /**
@@ -144,10 +179,12 @@ public class TestLauncher {
                                System.out.println("");
                        }
 
+                       currentSeries = 0;
                        for (TestLauncher serie : series) {
                                errors += serie.launch(depth + 1);
                                executed += serie.executed;
                                total += serie.total;
+                               currentSeries++;
                        }
                } catch (Exception e) {
                        print(depth, "__start");
@@ -179,20 +216,20 @@ public class TestLauncher {
                for (TestCase test : tests) {
                        print(depth, test.getName());
 
-                       Exception ex = null;
+                       Throwable ex = null;
                        try {
                                try {
                                        test.setUp();
-                               } catch (Exception e) {
+                               } catch (Throwable e) {
                                        throw new SetupException(e);
                                }
                                test.test();
                                try {
                                        test.tearDown();
-                               } catch (Exception e) {
+                               } catch (Throwable e) {
                                        throw new TearDownException(e);
                                }
-                       } catch (Exception e) {
+                       } catch (Throwable e) {
                                ex = e;
                        }
 
@@ -263,7 +300,7 @@ public class TestLauncher {
                        System.out.println("[ Test suite: " + name + " ]");
                        System.out.println("");
                } else {
-                       System.out.println(prefix(depth) + name + ":");
+                       System.out.println(prefix(depth, false) + name + ":");
                }
        }
 
@@ -272,17 +309,19 @@ public class TestLauncher {
         * 
         * @param depth
         *            the level at which is the launcher (0 = main launcher)
-        * @param test
-        *            the {@link TestCase}
+        * @param name
+        *            the {@link TestCase} name
         */
        protected void print(int depth, String name) {
-               name = prefix(depth) + (name == null ? "" : name).replace("\t", "    ");
+               name = prefix(depth, false)
+                               + (name == null ? "" : name).replace("\t", "    ");
 
-               while (name.length() < columns - 11) {
-                       name += ".";
+               StringBuilder dots = new StringBuilder();
+               while ((name.length() + dots.length()) < columns - 11) {
+                       dots.append('.');
                }
 
-               System.out.print(name);
+               System.out.print(name + dots.toString());
        }
 
        /**
@@ -293,11 +332,17 @@ public class TestLauncher {
         * @param error
         *            the {@link Exception} it ran into if any
         */
-       private void print(int depth, Exception error) {
+       private void print(int depth, Throwable error) {
                if (error != null) {
                        System.out.println(" " + koString);
-                       for (String line : (error.getMessage() + "").split("\n")) {
-                               System.out.println(prefix(depth) + "\t\t" + line);
+                       if (isDetails()) {
+                               StringWriter sw = new StringWriter();
+                               PrintWriter pw = new PrintWriter(sw);
+                               error.printStackTrace(pw);
+                               String lines = sw.toString();
+                               for (String line : lines.split("\n")) {
+                                       System.out.println(prefix(depth, false) + "\t\t" + line);
+                               }
                        }
                } else {
                        System.out.println(" " + okString);
@@ -332,11 +377,10 @@ public class TestLauncher {
                if (depth == 0) {
                        System.out.println(resume);
                } else {
-                       String arrow = "┗▶";
-                       if (series.isEmpty()) {
-                               arrow = "━▶";
-                       }
-                       System.out.println(prefix(depth) + arrow + resume);
+                       String arrow = "┗▶ ";
+                       System.out.println(prefix(depth, currentSeries == 0) + arrow
+                                       + resume);
+                       System.out.println(prefix(depth, currentSeries == 0));
                }
        }
 
@@ -347,22 +391,25 @@ public class TestLauncher {
         * 
         * @param depth
         *            the current depth
+        * @param first
+        *            this line is the first of its tabulation level
         * 
         * @return the prefix
         */
-       private String prefix(int depth) {
+       private String prefix(int depth, boolean first) {
                String space = tabs(depth - 1);
 
                String line = "";
                if (depth > 0) {
                        if (depth > 1) {
-                               if (depth != last) {
+                               if (depth != last && first) {
                                        line = "╻"; // first line
                                } else {
                                        line = "┃"; // continuation
                                }
                        }
-                       space = space + line + tabs(1);
+
+                       space += line + tabs(1);
                }
 
                last = depth;
@@ -378,7 +425,6 @@ public class TestLauncher {
         * @return the string
         */
        private String tabs(int depth) {
-
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < depth; i++) {
                        builder.append("    ");