1 package org
.argeo
.slc
.core
.test
.tree
;
3 import java
.util
.ArrayList
;
7 import java
.util
.SortedMap
;
8 import java
.util
.TreeMap
;
9 import java
.util
.Vector
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.argeo
.slc
.core
.attachment
.Attachment
;
15 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
16 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
17 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
18 import org
.argeo
.slc
.structure
.StructureAware
;
19 import org
.argeo
.slc
.structure
.StructureElement
;
20 import org
.argeo
.slc
.structure
.StructureRegistry
;
21 import org
.argeo
.slc
.test
.TestResult
;
22 import org
.argeo
.slc
.test
.TestResultListener
;
23 import org
.argeo
.slc
.test
.TestResultPart
;
24 import org
.argeo
.slc
.test
.TestRun
;
25 import org
.argeo
.slc
.test
.TestRunAware
;
28 * Complex implementation of a test result compatible with a tree based
31 public class TreeTestResult
implements TestResult
, StructureAware
<TreeSPath
>,
32 Comparable
<TreeTestResult
>, AttachmentsEnabled
{
33 private Log log
= LogFactory
.getLog(TreeTestResult
.class);
35 private List
<TestResultListener
<TreeTestResult
>> listeners
= new Vector
<TestResultListener
<TreeTestResult
>>();
37 private TreeSPath currentPath
;
38 private TestRun currentTestRun
;
40 private Date closeDate
;
42 private Boolean isClosed
= false;
44 private Boolean warnIfAlreadyClosed
= true;
48 private SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
49 private SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
50 private List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
52 private Map
<String
, String
> attributes
= new TreeMap
<String
, String
>();
54 /** Sets the list of listeners. */
55 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
56 this.listeners
= listeners
;
59 public void addResultPart(TestResultPart part
) {
61 throw new SlcException("Cannot result parts to a closed result");
63 if (currentPath
== null)
64 throw new SlcException("No current path set.");
66 PartSubList subList
= resultParts
.get(currentPath
);
67 if (subList
== null) {
68 subList
= new PartSubList();
69 resultParts
.put(currentPath
, subList
);
71 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
72 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
74 subList
.getParts().add(part
);
77 synchronized (listeners
) {
78 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
79 listener
.resultPartAdded(this, part
);
84 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
86 if (registry
!= null) {
87 for (TreeSPath p
: path
.getHierarchyAsList()) {
88 if (!elements
.containsKey(p
)) {
89 StructureElement elem
= registry
.getElement(p
);
91 elements
.put(p
, elem
);
94 if (log
.isTraceEnabled())
95 log
.trace("An element is already registered for path "
96 + p
+ " and was not updated");
105 /** Gets the current path. */
106 public TreeSPath
getCurrentPath() {
110 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
111 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
115 /** Used by ORM systems. */
116 void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
117 this.resultParts
= resultParts
;
120 public void close() {
121 if (resultParts
.size() == 0) {
122 if (log
.isTraceEnabled())
123 log
.trace("Test Result #" + getUuid()
124 + " contains no results, no need to close it.");
129 if (warnIfAlreadyClosed
)
130 log
.warn("Test Result #" + getUuid()
131 + " already closed. Doing nothing.");
135 closeDate
= new Date();
137 synchronized (listeners
) {
138 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
139 listener
.close(this);
144 if (log
.isTraceEnabled())
145 log
.trace("Test Result " + getUuid() + " closed.");
148 public Date
getCloseDate() {
152 /** Sets the close date (for ORM) */
153 public void setCloseDate(Date closeDate
) {
154 this.closeDate
= closeDate
;
157 public void notifyTestRun(TestRun testRun
) {
158 currentTestRun
= testRun
;
161 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
165 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
166 this.elements
= pathNames
;
169 public String
getUuid() {
173 public void setUuid(String uuid
) {
177 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
180 throw new SlcException(
181 "Cannot retrieve element for a null path in result #"
184 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
185 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
186 for (TreeSPath currPath
: elements
.keySet()) {
187 if (hierarchy
.contains(currPath
)) {
188 relatedElements
.put(currPath
, elements
.get(currPath
));
191 return relatedElements
;
194 public TestRun
getCurrentTestRun() {
195 return currentTestRun
;
198 public int compareTo(TreeTestResult ttr2
) {
199 TreeTestResult ttr1
= this;
200 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
201 if (ttr1
.getCloseDate().equals(ttr2
.getCloseDate()))
202 return compareUuid(ttr1
, ttr2
);
204 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
205 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
207 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
210 return compareUuid(ttr1
, ttr2
);
214 protected int compareUuid(TestResult ttr1
, TestResult ttr2
) {
215 if (ttr1
.getUuid() == null || ttr2
.getUuid() == null)
216 throw new SlcException(
217 "Cannot compare tree test result with null uuid");
219 if (ttr1
.getUuid().equals(ttr2
.getUuid()))
221 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
225 public boolean equals(Object obj
) {
226 if (obj
instanceof TestResult
)
227 return compareUuid(this, ((TestResult
) obj
)) == 0;
232 public int hashCode() {
234 return uuid
.hashCode();
236 return super.hashCode();
239 public Map
<String
, String
> getAttributes() {
243 public void setAttributes(Map
<String
, String
> attributes
) {
244 this.attributes
= attributes
;
247 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
248 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;
251 public List
<SimpleAttachment
> getAttachments() {
255 public void setAttachments(List
<SimpleAttachment
> attachments
) {
256 this.attachments
= attachments
;
259 public void addAttachment(Attachment attachment
) {
260 attachments
.add((SimpleAttachment
) attachment
);
261 synchronized (listeners
) {
262 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
263 if (listener
instanceof TreeTestResultListener
)
264 ((TreeTestResultListener
) listener
).addAttachment(this,