2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.test
.tree
;
19 import java
.io
.Serializable
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Date
;
22 import java
.util
.List
;
23 import java
.util
.SortedMap
;
24 import java
.util
.TreeMap
;
25 import java
.util
.UUID
;
26 import java
.util
.Vector
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.core
.attachment
.Attachment
;
32 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
33 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
34 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
35 import org
.argeo
.slc
.structure
.StructureAware
;
36 import org
.argeo
.slc
.structure
.StructureElement
;
37 import org
.argeo
.slc
.structure
.StructureRegistry
;
38 import org
.argeo
.slc
.test
.TestResult
;
39 import org
.argeo
.slc
.test
.TestResultListener
;
40 import org
.argeo
.slc
.test
.TestResultPart
;
41 import org
.argeo
.slc
.test
.TestRun
;
42 import org
.argeo
.slc
.test
.TestRunAware
;
45 * Complex implementation of a test result compatible with a tree based
48 public class TreeTestResult
implements TestResult
, StructureAware
<TreeSPath
>,
49 Comparable
<TreeTestResult
>, AttachmentsEnabled
, Serializable
{
51 private static final long serialVersionUID
= 1L;
52 private final static Log log
= LogFactory
.getLog(TreeTestResult
.class);
55 private String uuid
= UUID
.randomUUID().toString();
56 private Date closeDate
;
58 private SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
59 private SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
60 private List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
62 // Headers. Used to accelerate request on a specific test result.
63 private SortedMap
<String
, String
> attributes
= new TreeMap
<String
, String
>();
66 private TreeSPath currentPath
;
67 private transient TestRun currentTestRun
;
68 private Boolean warnIfAlreadyClosed
= true;
69 private Boolean strictChecks
= false;
70 // TODO is it really necessary closeDate == null ?
71 private Boolean isClosed
= false;
73 private transient List
<TestResultListener
<TreeTestResult
>> listeners
= new Vector
<TestResultListener
<TreeTestResult
>>();
75 /** Sets the list of listeners. */
76 public void setListeners(List
<TestResultListener
<TreeTestResult
>> listeners
) {
77 this.listeners
= listeners
;
80 public void addResultPart(TestResultPart part
) {
83 "Trying to add result parts to an already closed result,"
84 + " consider changing the scope of this test result:"
85 + " you are referencing the same stored data with each new call.",
88 if (currentPath
== null)
89 throw new SlcException("No current path set.");
91 PartSubList subList
= resultParts
.get(currentPath
);
92 if (subList
== null) {
93 subList
= new PartSubList();
94 resultParts
.put(currentPath
, subList
);
96 if (part
instanceof TestRunAware
&& currentTestRun
!= null) {
97 ((TestRunAware
) part
).notifyTestRun(currentTestRun
);
99 subList
.getParts().add(part
);
102 synchronized (listeners
) {
103 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
104 listener
.resultPartAdded(this, part
);
109 protected void notifyIssue(String msg
, Exception e
) {
111 throw new SlcException(msg
, e
);
116 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
118 if (registry
!= null) {
119 for (TreeSPath p
: path
.getHierarchyAsList()) {
120 if (!elements
.containsKey(p
)) {
121 StructureElement elem
= registry
.getElement(p
);
123 elements
.put(p
, elem
);
126 if (log
.isTraceEnabled())
127 log
.trace("An element is already registered for path "
128 + p
+ " and was not updated");
137 /** Gets the current path. */
138 public TreeSPath
getCurrentPath() {
142 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
143 public SortedMap
<TreeSPath
, PartSubList
> getResultParts() {
148 * Used by ORM systems. Changed to public in order to enable jcr persistence
150 public void setResultParts(SortedMap
<TreeSPath
, PartSubList
> resultParts
) {
151 this.resultParts
= resultParts
;
154 public void close() {
155 if (resultParts
.size() == 0) {
156 if (log
.isTraceEnabled())
157 log
.trace("Test Result #" + getUuid()
158 + " contains no results, no need to close it.");
163 if (warnIfAlreadyClosed
)
164 log
.warn("Test Result #" + getUuid()
165 + " already closed. Doing nothing.");
169 closeDate
= new Date();
171 synchronized (listeners
) {
172 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
173 listener
.close(this);
178 if (log
.isTraceEnabled())
179 log
.trace("Test Result " + getUuid() + " closed.");
182 public Date
getCloseDate() {
186 /** Sets the close date (for ORM) */
187 public void setCloseDate(Date closeDate
) {
188 this.closeDate
= closeDate
;
191 public void notifyTestRun(TestRun testRun
) {
192 currentTestRun
= testRun
;
195 public SortedMap
<TreeSPath
, StructureElement
> getElements() {
199 public void setElements(SortedMap
<TreeSPath
, StructureElement
> pathNames
) {
200 this.elements
= pathNames
;
203 public String
getUuid() {
207 public void setUuid(String uuid
) {
211 public SortedMap
<TreeSPath
, StructureElement
> getRelatedElements(
214 throw new SlcException(
215 "Cannot retrieve element for a null path in result #"
218 SortedMap
<TreeSPath
, StructureElement
> relatedElements
= new TreeMap
<TreeSPath
, StructureElement
>();
219 List
<TreeSPath
> hierarchy
= path
.getHierarchyAsList();
220 for (TreeSPath currPath
: elements
.keySet()) {
221 if (hierarchy
.contains(currPath
)) {
222 relatedElements
.put(currPath
, elements
.get(currPath
));
225 return relatedElements
;
228 public TestRun
getCurrentTestRun() {
229 return currentTestRun
;
232 public int compareTo(TreeTestResult ttr2
) {
233 TreeTestResult ttr1
= this;
234 if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() != null) {
235 if (ttr1
.getCloseDate().equals(ttr2
.getCloseDate()))
236 return compareUuid(ttr1
, ttr2
);
238 return -ttr1
.getCloseDate().compareTo(ttr2
.getCloseDate());
239 } else if (ttr1
.getCloseDate() != null && ttr2
.getCloseDate() == null) {
241 } else if (ttr1
.getCloseDate() == null && ttr2
.getCloseDate() != null) {
244 return compareUuid(ttr1
, ttr2
);
248 protected int compareUuid(TestResult ttr1
, TestResult ttr2
) {
249 if (ttr1
.getUuid() == null || ttr2
.getUuid() == null)
250 throw new SlcException(
251 "Cannot compare tree test result with null uuid");
253 if (ttr1
.getUuid().equals(ttr2
.getUuid()))
255 return ttr1
.getUuid().compareTo(ttr2
.getUuid());
259 public boolean equals(Object obj
) {
260 if (obj
instanceof TestResult
)
261 return compareUuid(this, ((TestResult
) obj
)) == 0;
266 public int hashCode() {
268 return uuid
.hashCode();
270 return super.hashCode();
273 public SortedMap
<String
, String
> getAttributes() {
277 public void setAttributes(SortedMap
<String
, String
> attributes
) {
278 this.attributes
= attributes
;
281 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed
) {
282 this.warnIfAlreadyClosed
= warnIfAlreadyClosed
;
285 public List
<SimpleAttachment
> getAttachments() {
289 public void setAttachments(List
<SimpleAttachment
> attachments
) {
290 this.attachments
= attachments
;
293 public void addAttachment(Attachment attachment
) {
294 attachments
.add((SimpleAttachment
) attachment
);
295 synchronized (listeners
) {
296 for (TestResultListener
<TreeTestResult
> listener
: listeners
) {
297 if (listener
instanceof TreeTestResultListener
)
298 ((TreeTestResultListener
) listener
).addAttachment(this,
304 public void setStrictChecks(Boolean strictChecks
) {
305 this.strictChecks
= strictChecks
;