]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / TreeTestResult.java
index 789d468ae9940a8c41be908e7eddceb1875527c1..f102ae6ab4babaf0be961118a27c85e185afa367 100644 (file)
@@ -1,10 +1,24 @@
+/*\r
+ * Copyright (C) 2007-2012 Mathieu Baudier\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
 package org.argeo.slc.core.test.tree;\r
 \r
 import java.io.Serializable;\r
 import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
-import java.util.Map;\r
 import java.util.SortedMap;\r
 import java.util.TreeMap;\r
 import java.util.UUID;\r
@@ -32,28 +46,32 @@ import org.argeo.slc.test.TestRunAware;
  */\r
 public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,\r
                Comparable<TreeTestResult>, AttachmentsEnabled, Serializable {\r
-       private static final long serialVersionUID = 1L;\r
-\r
-       private Log log = LogFactory.getLog(TreeTestResult.class);\r
-\r
-       private List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();\r
-\r
-       private TreeSPath currentPath;\r
-       private TestRun currentTestRun;\r
-\r
-       private Date closeDate;\r
 \r
-       private Boolean isClosed = false;\r
-\r
-       private Boolean warnIfAlreadyClosed = true;\r
+       private static final long serialVersionUID = 1L;\r
+       private final static Log log = LogFactory.getLog(TreeTestResult.class);\r
 \r
+       // Persistence data\r
        private String uuid = UUID.randomUUID().toString();\r
+       private Date closeDate;\r
 \r
        private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();\r
        private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();\r
        private List<SimpleAttachment> attachments = new ArrayList<SimpleAttachment>();\r
 \r
-       private Map<String, String> attributes = new TreeMap<String, String>();\r
+       // Headers. Used to accelerate request on a specific test result.\r
+       private SortedMap<String, String> attributes = new TreeMap<String, String>();\r
+\r
+       // Runtime Data\r
+       private TreeSPath currentPath;\r
+       private transient TestRun currentTestRun;\r
+       private Boolean warnIfAlreadyClosed = true;\r
+       private Boolean strictChecks = false;\r
+       // TODO is it really necessary closeDate == null ?\r
+       private Boolean isClosed = false;\r
+\r
+       private Boolean cache = true;\r
+\r
+       private transient List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();\r
 \r
        /** Sets the list of listeners. */\r
        public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {\r
@@ -62,20 +80,27 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
 \r
        public void addResultPart(TestResultPart part) {\r
                if (isClosed)\r
-                       throw new SlcException("Cannot result parts to a closed result");\r
+                       notifyIssue(\r
+                                       "Trying to add result parts to an already closed result,"\r
+                                                       + " consider changing the scope of this test result:"\r
+                                                       + " you are referencing the same stored data with each new call.",\r
+                                       null);\r
 \r
                if (currentPath == null)\r
                        throw new SlcException("No current path set.");\r
 \r
-               PartSubList subList = resultParts.get(currentPath);\r
-               if (subList == null) {\r
-                       subList = new PartSubList();\r
-                       resultParts.put(currentPath, subList);\r
+               if (cache) {\r
+                       PartSubList subList = resultParts.get(currentPath);\r
+                       if (subList == null) {\r
+                               subList = new PartSubList();\r
+                               resultParts.put(currentPath, subList);\r
+                       }\r
+                       subList.getParts().add(part);\r
                }\r
+\r
                if (part instanceof TestRunAware && currentTestRun != null) {\r
                        ((TestRunAware) part).notifyTestRun(currentTestRun);\r
                }\r
-               subList.getParts().add(part);\r
 \r
                // notify listeners\r
                synchronized (listeners) {\r
@@ -85,8 +110,18 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                }\r
        }\r
 \r
+       protected void notifyIssue(String msg, Exception e) {\r
+               if (strictChecks)\r
+                       throw new SlcException(msg, e);\r
+               else\r
+                       log.error(msg, e);\r
+       }\r
+\r
        public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,\r
                        TreeSPath path) {\r
+               if (!cache)\r
+                       return;\r
+\r
                if (registry != null) {\r
                        for (TreeSPath p : path.getHierarchyAsList()) {\r
                                if (!elements.containsKey(p)) {\r
@@ -116,8 +151,10 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                return resultParts;\r
        }\r
 \r
-       /** Used by ORM systems. */\r
-       void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {\r
+       /**\r
+        * Used by ORM systems. Changed to public in order to enable jcr persistence\r
+        */\r
+       public void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {\r
                this.resultParts = resultParts;\r
        }\r
 \r
@@ -240,11 +277,11 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                        return super.hashCode();\r
        }\r
 \r
-       public Map<String, String> getAttributes() {\r
+       public SortedMap<String, String> getAttributes() {\r
                return attributes;\r
        }\r
 \r
-       public void setAttributes(Map<String, String> attributes) {\r
+       public void setAttributes(SortedMap<String, String> attributes) {\r
                this.attributes = attributes;\r
        }\r
 \r
@@ -271,4 +308,16 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                }\r
        }\r
 \r
+       public void setStrictChecks(Boolean strictChecks) {\r
+               this.strictChecks = strictChecks;\r
+       }\r
+\r
+       /**\r
+        * Whether information should be stored in thsi object or simply forwarded\r
+        * to teh listeners.\r
+        */\r
+       public void setCache(Boolean cache) {\r
+               this.cache = cache;\r
+       }\r
+\r
 }\r