1 package org
.argeo
.slc
.core
.test
.tree
;
3 import java
.io
.Serializable
;
4 import java
.util
.ArrayList
;
8 import java
.util
.SortedMap
;
9 import java
.util
.TreeMap
;
10 import java
.util
.UUID
;
11 import java
.util
.Vector
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.core
.attachment
.Attachment
;
17 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
18 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
19 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
20 import org
.argeo
.slc
.structure
.StructureAware
;
21 import org
.argeo
.slc
.structure
.StructureElement
;
22 import org
.argeo
.slc
.structure
.StructureRegistry
;
23 import org
.argeo
.slc
.test
.TestResult
;
24 import org
.argeo
.slc
.test
.TestResultListener
;
25 import org
.argeo
.slc
.test
.TestResultPart
;
26 import org
.argeo
.slc
.test
.TestRun
;
27 import org
.argeo
.slc
.test
.TestRunAware
;
30 * Complex implementation of a test result compatible with a tree based
33 public class TreeTestResult
implements TestResult
, StructureAware
<TreeSPath
>,
34 Comparable
<TreeTestResult
>, AttachmentsEnabled
, Serializable
{
35 private static final long serialVersionUID
= 1L;
37 private Log log
= LogFactory
.getLog(TreeTestResult
.class);
39 private List
<TestResultListener
<TreeTestResult
>> listeners
= new Vector
<TestResultListener
<TreeTestResult
>>();
41 private TreeSPath currentPath
;
42 private TestRun currentTestRun
;
44 private Date closeDate
;
46 private Boolean isClosed
= false;
48 private Boolean warnIfAlreadyClosed
= true;
50 private String uuid
= UUID
.randomUUID().toString();
52 private SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
53 private SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
54 private List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
56 private Map
<String
, String
> attributes
= new TreeMap
<String
, String
>();
58 /** Sets the list of listeners. */
59 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
60 this.listeners
= listeners
;
63 public void addResultPart(TestResultPart part
) {
65 throw new SlcException("Cannot result parts to a closed result");
67 if (currentPath
== null)
68 throw new SlcException("No current path set.");
70 PartSubList subList
= resultParts
.get(currentPath
);
71 if (subList
== null) {
72 subList
= new PartSubList();
73 resultParts
.put(currentPath
, subList
);
75 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
76 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
78 subList
.getParts().add(part
);
81 synchronized (listeners
) {
82 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
83 listener
.resultPartAdded(this, part
);
88 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
90 if (registry
!= null) {
91 for (TreeSPath p
: path
.getHierarchyAsList()) {
92 if (!elements
.containsKey(p
)) {
93 StructureElement elem
= registry
.getElement(p
);
95 elements
.put(p
, elem
);
98 if (log
.isTraceEnabled())
99 log
.trace("An element is already registered for path "
100 + p
+ " and was not updated");
109 /** Gets the current path. */
110 public TreeSPath
getCurrentPath() {
114 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
115 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
119 /** Used by ORM systems. */
120 void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
121 this.resultParts
= resultParts
;
124 public void close() {
125 if (resultParts
.size() == 0) {
126 if (log
.isTraceEnabled())
127 log
.trace("Test Result #" + getUuid()
128 + " contains no results, no need to close it.");
133 if (warnIfAlreadyClosed
)
134 log
.warn("Test Result #" + getUuid()
135 + " already closed. Doing nothing.");
139 closeDate
= new Date();
141 synchronized (listeners
) {
142 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
143 listener
.close(this);
148 if (log
.isTraceEnabled())
149 log
.trace("Test Result " + getUuid() + " closed.");
152 public Date
getCloseDate() {
156 /** Sets the close date (for ORM) */
157 public void setCloseDate(Date closeDate
) {
158 this.closeDate
= closeDate
;
161 public void notifyTestRun(TestRun testRun
) {
162 currentTestRun
= testRun
;
165 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
169 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
170 this.elements
= pathNames
;
173 public String
getUuid() {
177 public void setUuid(String uuid
) {
181 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
184 throw new SlcException(
185 "Cannot retrieve element for a null path in result #"
188 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
189 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
190 for (TreeSPath currPath
: elements
.keySet()) {
191 if (hierarchy
.contains(currPath
)) {
192 relatedElements
.put(currPath
, elements
.get(currPath
));
195 return relatedElements
;
198 public TestRun
getCurrentTestRun() {
199 return currentTestRun
;
202 public int compareTo(TreeTestResult ttr2
) {
203 TreeTestResult ttr1
= this;
204 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
205 if (ttr1
.getCloseDate().equals(ttr2
.getCloseDate()))
206 return compareUuid(ttr1
, ttr2
);
208 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
209 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
211 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
214 return compareUuid(ttr1
, ttr2
);
218 protected int compareUuid(TestResult ttr1
, TestResult ttr2
) {
219 if (ttr1
.getUuid() == null || ttr2
.getUuid() == null)
220 throw new SlcException(
221 "Cannot compare tree test result with null uuid");
223 if (ttr1
.getUuid().equals(ttr2
.getUuid()))
225 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
229 public boolean equals(Object obj
) {
230 if (obj
instanceof TestResult
)
231 return compareUuid(this, ((TestResult
) obj
)) == 0;
236 public int hashCode() {
238 return uuid
.hashCode();
240 return super.hashCode();
243 public Map
<String
, String
> getAttributes() {
247 public void setAttributes(Map
<String
, String
> attributes
) {
248 this.attributes
= attributes
;
251 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
252 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;
255 public List
<SimpleAttachment
> getAttachments() {
259 public void setAttachments(List
<SimpleAttachment
> attachments
) {
260 this.attachments
= attachments
;
263 public void addAttachment(Attachment attachment
) {
264 attachments
.add((SimpleAttachment
) attachment
);
265 synchronized (listeners
) {
266 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
267 if (listener
instanceof TreeTestResultListener
)
268 ((TreeTestResultListener
) listener
).addAttachment(this,