]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java
Improve reporting
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / core / test / tree / AsynchronousTreeTestResultListener.java
index d6f0c935d2aa2d03657f23e3ade019617119f042..c3bc793ef5f357d03a16b821c1dd067f5163f83b 100644 (file)
@@ -3,46 +3,76 @@ package org.argeo.slc.core.test.tree;
 import java.util.Vector;\r
 \r
 import org.argeo.slc.core.structure.tree.TreeSPath;\r
+import org.argeo.slc.core.test.NumericTRId;\r
 import org.argeo.slc.core.test.TestResult;\r
-import org.argeo.slc.core.test.TestResultId;\r
 import org.argeo.slc.core.test.TestResultListener;\r
 import org.argeo.slc.core.test.TestResultPart;\r
 \r
+/**\r
+ * Abstract asynchronous implementation of a listener listening to a\r
+ * <code>TreeTestResult</code>.\r
+ * \r
+ * @see TreeTestResult\r
+ */\r
 public abstract class AsynchronousTreeTestResultListener implements\r
                TestResultListener, Runnable {\r
        private Vector<PartStruct> partStructs = new Vector<PartStruct>();\r
        private Thread thread;\r
 \r
+       /** Starts the underlying thread. */\r
        public void init() {\r
                thread = new Thread(this);\r
                thread.start();\r
        }\r
 \r
-       public void destroy() {\r
-               thread = null;\r
+       /** Finish the remaining and destroy */\r
+       public void close(TestResult testResult) {\r
                synchronized (partStructs) {\r
+                       // TODO: put a timeout\r
+                       while (partStructs.size() != 0) {\r
+                               try {\r
+                                       partStructs.wait(500);\r
+                               } catch (InterruptedException e) {\r
+                                       // silent\r
+                               }\r
+                       }\r
+                       thread = null;\r
                        partStructs.notifyAll();\r
                }\r
+               postClose((TreeTestResult)testResult);\r
        }\r
 \r
        public final void resultPartAdded(TestResult testResult,\r
                        TestResultPart testResultPart) {\r
                TreeTestResult result = (TreeTestResult) testResult;\r
                synchronized (partStructs) {\r
-                       partStructs.add(new PartStruct(result.getCurrentPath(), result\r
-                                       .getTestResultId(), testResultPart, result));\r
+                       partStructs.add(new PartStruct(result.getCurrentPath(),\r
+                                       (NumericTRId) result.getTestResultId(), testResultPart,\r
+                                       result));\r
                        partStructs.notifyAll();\r
                }\r
        }\r
 \r
+       /** Called when a result part has been added. */\r
+       protected abstract void resultPartAdded(PartStruct partStruct);\r
+\r
+       /**\r
+        * Called at the end of close. Default implementation is empty. To be\r
+        * overridden.\r
+        */\r
+       protected void postClose(TreeTestResult testResult) {\r
+\r
+       }\r
+\r
        public void run() {\r
                while (thread != null) {\r
-                       for (PartStruct partStruct : partStructs) {\r
-                               resultPartAdded(partStruct);\r
-                       }\r
-                       \r
                        synchronized (partStructs) {\r
+                               for (PartStruct partStruct : partStructs) {\r
+                                       resultPartAdded(partStruct);\r
+                               }\r
+\r
                                partStructs.clear();\r
+                               partStructs.notifyAll();\r
                                while (partStructs.size() == 0) {\r
                                        try {\r
                                                partStructs.wait();\r
@@ -54,15 +84,19 @@ public abstract class AsynchronousTreeTestResultListener implements
                }\r
        }\r
 \r
-       protected abstract void resultPartAdded(PartStruct partStruct);\r
-\r
+       /** Structure used to pass tree specific information to subclasses. */\r
        protected static class PartStruct {\r
+               /** The tree path of this part. */\r
                public final TreeSPath path;\r
-               public final TestResultId resultId;\r
+               /** The test result id of the related test result */\r
+               public final NumericTRId resultId;\r
+               /** The part itself */\r
                public final TestResultPart part;\r
+               /** The tree test result itself. */\r
                public final TreeTestResult result;\r
 \r
-               public PartStruct(TreeSPath path, TestResultId resultId,\r
+               /** Constructor */\r
+               public PartStruct(TreeSPath path, NumericTRId resultId,\r
                                TestResultPart part, TreeTestResult result) {\r
                        super();\r
                        this.path = path;\r