1 package org
.argeo
.slc
.core
.test
.tree
;
6 import java
.util
.SortedMap
;
7 import java
.util
.TreeMap
;
8 import java
.util
.Vector
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.core
.SlcException
;
13 import org
.argeo
.slc
.core
.structure
.StructureAware
;
14 import org
.argeo
.slc
.core
.structure
.StructureElement
;
15 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
16 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
17 import org
.argeo
.slc
.core
.test
.TestResult
;
18 import org
.argeo
.slc
.core
.test
.TestResultListener
;
19 import org
.argeo
.slc
.core
.test
.TestResultPart
;
20 import org
.argeo
.slc
.core
.test
.TestRun
;
21 import org
.argeo
.slc
.core
.test
.TestRunAware
;
24 * Complex implementation of a test result compatible with a tree based
27 public class TreeTestResult
implements TestResult
, StructureAware
<TreeSPath
>,
28 Comparable
<TreeTestResult
> {
29 private Log log
= LogFactory
.getLog(TreeTestResult
.class);
31 private List
<TestResultListener
<TreeTestResult
>> listeners
= new Vector
<TestResultListener
<TreeTestResult
>>();
33 private TreeSPath currentPath
;
34 private TestRun currentTestRun
;
36 private Date closeDate
;
38 private Boolean isClosed
= false;
40 private Boolean warnIfAlreadyClosed
= true;
44 private SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
45 private SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
47 private Map
<String
, String
> attributes
= new TreeMap
<String
, String
>();
49 /** Sets the list of listeners. */
50 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
51 this.listeners
= listeners
;
54 public void addResultPart(TestResultPart part
) {
56 throw new SlcException("Cannot result parts to a closed result");
58 if (currentPath
== null)
59 throw new SlcException("No current path set.");
61 PartSubList subList
= resultParts
.get(currentPath
);
62 if (subList
== null) {
63 subList
= new PartSubList();
64 resultParts
.put(currentPath
, subList
);
66 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
67 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
69 subList
.getParts().add(part
);
72 synchronized (listeners
) {
73 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
74 listener
.resultPartAdded(this, part
);
79 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
81 if (registry
!= null) {
82 for (TreeSPath p
: path
.getHierarchyAsList()) {
83 if (!elements
.containsKey(p
)) {
84 StructureElement elem
= registry
.getElement(p
);
86 elements
.put(p
, elem
);
89 if (log
.isTraceEnabled())
90 log
.trace("An element is already registered for path "
91 + p
+ " and was not updated");
100 /** Gets the current path. */
101 public TreeSPath
getCurrentPath() {
105 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
106 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
110 /** Used by ORM systems. */
111 void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
112 this.resultParts
= resultParts
;
115 public void close() {
116 if (resultParts
.size() == 0) {
117 if (log
.isTraceEnabled())
118 log
.trace("Test Result #" + getUuid()
119 + " contains no results, no need to close it.");
124 if (warnIfAlreadyClosed
)
125 log
.warn("Test Result #" + getUuid()
126 + " already closed. Doing nothing.");
130 closeDate
= new Date();
132 synchronized (listeners
) {
133 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
134 listener
.close(this);
140 if (log
.isTraceEnabled())
141 log
.trace("Test Result " + getUuid() + " closed.");
144 public Date
getCloseDate() {
148 /** Sets the close date (for ORM) */
149 public void setCloseDate(Date closeDate
) {
150 this.closeDate
= closeDate
;
153 public void notifyTestRun(TestRun testRun
) {
154 currentTestRun
= testRun
;
157 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
161 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
162 this.elements
= pathNames
;
165 public String
getUuid() {
169 public void setUuid(String uuid
) {
173 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
176 throw new SlcException(
177 "Cannot retrieve element for a null path in result #"
180 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
181 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
182 for (TreeSPath currPath
: elements
.keySet()) {
183 if (hierarchy
.contains(currPath
)) {
184 relatedElements
.put(currPath
, elements
.get(currPath
));
187 return relatedElements
;
190 public TestRun
getCurrentTestRun() {
191 return currentTestRun
;
194 public int compareTo(TreeTestResult ttr2
) {
195 TreeTestResult ttr1
= this;
196 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
197 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
198 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
200 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
203 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
207 public Map
<String
, String
> getAttributes() {
211 public void setAttributes(Map
<String
, String
> attributes
) {
212 this.attributes
= attributes
;
215 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
216 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;