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
.Vector
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.core
.attachment
.Attachment
;
16 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
17 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
18 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
19 import org
.argeo
.slc
.structure
.StructureAware
;
20 import org
.argeo
.slc
.structure
.StructureElement
;
21 import org
.argeo
.slc
.structure
.StructureRegistry
;
22 import org
.argeo
.slc
.test
.TestResult
;
23 import org
.argeo
.slc
.test
.TestResultListener
;
24 import org
.argeo
.slc
.test
.TestResultPart
;
25 import org
.argeo
.slc
.test
.TestRun
;
26 import org
.argeo
.slc
.test
.TestRunAware
;
29 * Complex implementation of a test result compatible with a tree based
32 public class TreeTestResult
implements TestResult
, StructureAware
<TreeSPath
>,
33 Comparable
<TreeTestResult
>, AttachmentsEnabled
, Serializable
{
34 private static final long serialVersionUID
= 1L;
36 private Log log
= LogFactory
.getLog(TreeTestResult
.class);
38 private List
<TestResultListener
<TreeTestResult
>> listeners
= new Vector
<TestResultListener
<TreeTestResult
>>();
40 private TreeSPath currentPath
;
41 private TestRun currentTestRun
;
43 private Date closeDate
;
45 private Boolean isClosed
= false;
47 private Boolean warnIfAlreadyClosed
= true;
51 private SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
52 private SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
53 private List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
55 private Map
<String
, String
> attributes
= new TreeMap
<String
, String
>();
57 /** Sets the list of listeners. */
58 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
59 this.listeners
= listeners
;
62 public void addResultPart(TestResultPart part
) {
64 throw new SlcException("Cannot result parts to a closed result");
66 if (currentPath
== null)
67 throw new SlcException("No current path set.");
69 PartSubList subList
= resultParts
.get(currentPath
);
70 if (subList
== null) {
71 subList
= new PartSubList();
72 resultParts
.put(currentPath
, subList
);
74 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
75 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
77 subList
.getParts().add(part
);
80 synchronized (listeners
) {
81 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
82 listener
.resultPartAdded(this, part
);
87 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
89 if (registry
!= null) {
90 for (TreeSPath p
: path
.getHierarchyAsList()) {
91 if (!elements
.containsKey(p
)) {
92 StructureElement elem
= registry
.getElement(p
);
94 elements
.put(p
, elem
);
97 if (log
.isTraceEnabled())
98 log
.trace("An element is already registered for path "
99 + p
+ " and was not updated");
108 /** Gets the current path. */
109 public TreeSPath
getCurrentPath() {
113 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
114 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
118 /** Used by ORM systems. */
119 void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
120 this.resultParts
= resultParts
;
123 public void close() {
124 if (resultParts
.size() == 0) {
125 if (log
.isTraceEnabled())
126 log
.trace("Test Result #" + getUuid()
127 + " contains no results, no need to close it.");
132 if (warnIfAlreadyClosed
)
133 log
.warn("Test Result #" + getUuid()
134 + " already closed. Doing nothing.");
138 closeDate
= new Date();
140 synchronized (listeners
) {
141 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
142 listener
.close(this);
147 if (log
.isTraceEnabled())
148 log
.trace("Test Result " + getUuid() + " closed.");
151 public Date
getCloseDate() {
155 /** Sets the close date (for ORM) */
156 public void setCloseDate(Date closeDate
) {
157 this.closeDate
= closeDate
;
160 public void notifyTestRun(TestRun testRun
) {
161 currentTestRun
= testRun
;
164 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
168 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
169 this.elements
= pathNames
;
172 public String
getUuid() {
176 public void setUuid(String uuid
) {
180 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
183 throw new SlcException(
184 "Cannot retrieve element for a null path in result #"
187 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
188 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
189 for (TreeSPath currPath
: elements
.keySet()) {
190 if (hierarchy
.contains(currPath
)) {
191 relatedElements
.put(currPath
, elements
.get(currPath
));
194 return relatedElements
;
197 public TestRun
getCurrentTestRun() {
198 return currentTestRun
;
201 public int compareTo(TreeTestResult ttr2
) {
202 TreeTestResult ttr1
= this;
203 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
204 if (ttr1
.getCloseDate().equals(ttr2
.getCloseDate()))
205 return compareUuid(ttr1
, ttr2
);
207 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
208 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
210 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
213 return compareUuid(ttr1
, ttr2
);
217 protected int compareUuid(TestResult ttr1
, TestResult ttr2
) {
218 if (ttr1
.getUuid() == null || ttr2
.getUuid() == null)
219 throw new SlcException(
220 "Cannot compare tree test result with null uuid");
222 if (ttr1
.getUuid().equals(ttr2
.getUuid()))
224 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
228 public boolean equals(Object obj
) {
229 if (obj
instanceof TestResult
)
230 return compareUuid(this, ((TestResult
) obj
)) == 0;
235 public int hashCode() {
237 return uuid
.hashCode();
239 return super.hashCode();
242 public Map
<String
, String
> getAttributes() {
246 public void setAttributes(Map
<String
, String
> attributes
) {
247 this.attributes
= attributes
;
250 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
251 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;
254 public List
<SimpleAttachment
> getAttachments() {
258 public void setAttachments(List
<SimpleAttachment
> attachments
) {
259 this.attachments
= attachments
;
262 public void addAttachment(Attachment attachment
) {
263 attachments
.add((SimpleAttachment
) attachment
);
264 synchronized (listeners
) {
265 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
266 if (listener
instanceof TreeTestResultListener
)
267 ((TreeTestResultListener
) listener
).addAttachment(this,