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 final static 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 private Boolean strictChecks
= false;
60 /** Sets the list of listeners. */
61 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
62 this.listeners
= listeners
;
65 public void addResultPart(TestResultPart part
) {
68 "Trying to add result parts to an already closed result,"
69 + " consider changing the scope of this test result:"
70 + " you are referencing the same stored data with each new call.",
73 if (currentPath
== null)
74 throw new SlcException("No current path set.");
76 PartSubList subList
= resultParts
.get(currentPath
);
77 if (subList
== null) {
78 subList
= new PartSubList();
79 resultParts
.put(currentPath
, subList
);
81 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
82 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
84 subList
.getParts().add(part
);
87 synchronized (listeners
) {
88 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
89 listener
.resultPartAdded(this, part
);
94 protected void notifyIssue(String msg
, Exception e
) {
96 throw new SlcException(msg
, e
);
101 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
103 if (registry
!= null) {
104 for (TreeSPath p
: path
.getHierarchyAsList()) {
105 if (!elements
.containsKey(p
)) {
106 StructureElement elem
= registry
.getElement(p
);
108 elements
.put(p
, elem
);
111 if (log
.isTraceEnabled())
112 log
.trace("An element is already registered for path "
113 + p
+ " and was not updated");
122 /** Gets the current path. */
123 public TreeSPath
getCurrentPath() {
127 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
128 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
132 /** Used by ORM systems. */
133 void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
134 this.resultParts
= resultParts
;
137 public void close() {
138 if (resultParts
.size() == 0) {
139 if (log
.isTraceEnabled())
140 log
.trace("Test Result #" + getUuid()
141 + " contains no results, no need to close it.");
146 if (warnIfAlreadyClosed
)
147 log
.warn("Test Result #" + getUuid()
148 + " already closed. Doing nothing.");
152 closeDate
= new Date();
154 synchronized (listeners
) {
155 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
156 listener
.close(this);
161 if (log
.isTraceEnabled())
162 log
.trace("Test Result " + getUuid() + " closed.");
165 public Date
getCloseDate() {
169 /** Sets the close date (for ORM) */
170 public void setCloseDate(Date closeDate
) {
171 this.closeDate
= closeDate
;
174 public void notifyTestRun(TestRun testRun
) {
175 currentTestRun
= testRun
;
178 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
182 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
183 this.elements
= pathNames
;
186 public String
getUuid() {
190 public void setUuid(String uuid
) {
194 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
197 throw new SlcException(
198 "Cannot retrieve element for a null path in result #"
201 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
202 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
203 for (TreeSPath currPath
: elements
.keySet()) {
204 if (hierarchy
.contains(currPath
)) {
205 relatedElements
.put(currPath
, elements
.get(currPath
));
208 return relatedElements
;
211 public TestRun
getCurrentTestRun() {
212 return currentTestRun
;
215 public int compareTo(TreeTestResult ttr2
) {
216 TreeTestResult ttr1
= this;
217 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
218 if (ttr1
.getCloseDate().equals(ttr2
.getCloseDate()))
219 return compareUuid(ttr1
, ttr2
);
221 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
222 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
224 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
227 return compareUuid(ttr1
, ttr2
);
231 protected int compareUuid(TestResult ttr1
, TestResult ttr2
) {
232 if (ttr1
.getUuid() == null || ttr2
.getUuid() == null)
233 throw new SlcException(
234 "Cannot compare tree test result with null uuid");
236 if (ttr1
.getUuid().equals(ttr2
.getUuid()))
238 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
242 public boolean equals(Object obj
) {
243 if (obj
instanceof TestResult
)
244 return compareUuid(this, ((TestResult
) obj
)) == 0;
249 public int hashCode() {
251 return uuid
.hashCode();
253 return super.hashCode();
256 public Map
<String
, String
> getAttributes() {
260 public void setAttributes(Map
<String
, String
> attributes
) {
261 this.attributes
= attributes
;
264 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
265 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;
268 public List
<SimpleAttachment
> getAttachments() {
272 public void setAttachments(List
<SimpleAttachment
> attachments
) {
273 this.attachments
= attachments
;
276 public void addAttachment(Attachment attachment
) {
277 attachments
.add((SimpleAttachment
) attachment
);
278 synchronized (listeners
) {
279 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
280 if (listener
instanceof TreeTestResultListener
)
281 ((TreeTestResultListener
) listener
).addAttachment(this,
287 public void setStrictChecks(Boolean strictChecks
) {
288 this.strictChecks
= strictChecks
;