]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
Move to execution package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / TreeTestResult.java
1 package org.argeo.slc.core.test.tree;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.SortedMap;
9 import java.util.TreeMap;
10 import java.util.UUID;
11 import java.util.Vector;
12
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;
28
29 /**
30 * Complex implementation of a test result compatible with a tree based
31 * structure.
32 */
33 public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
34 Comparable<TreeTestResult>, AttachmentsEnabled, Serializable {
35 private static final long serialVersionUID = 1L;
36
37 private Log log = LogFactory.getLog(TreeTestResult.class);
38
39 private List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();
40
41 private TreeSPath currentPath;
42 private TestRun currentTestRun;
43
44 private Date closeDate;
45
46 private Boolean isClosed = false;
47
48 private Boolean warnIfAlreadyClosed = true;
49
50 private String uuid = UUID.randomUUID().toString();
51
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>();
55
56 private Map<String, String> attributes = new TreeMap<String, String>();
57
58 /** Sets the list of listeners. */
59 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
60 this.listeners = listeners;
61 }
62
63 public void addResultPart(TestResultPart part) {
64 if (isClosed)
65 throw new SlcException("Cannot result parts to a closed result");
66
67 if (currentPath == null)
68 throw new SlcException("No current path set.");
69
70 PartSubList subList = resultParts.get(currentPath);
71 if (subList == null) {
72 subList = new PartSubList();
73 resultParts.put(currentPath, subList);
74 }
75 if (part instanceof TestRunAware && currentTestRun != null) {
76 ((TestRunAware) part).notifyTestRun(currentTestRun);
77 }
78 subList.getParts().add(part);
79
80 // notify listeners
81 synchronized (listeners) {
82 for (TestResultListener<TreeTestResult> listener : listeners) {
83 listener.resultPartAdded(this, part);
84 }
85 }
86 }
87
88 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
89 TreeSPath path) {
90 if (registry != null) {
91 for (TreeSPath p : path.getHierarchyAsList()) {
92 if (!elements.containsKey(p)) {
93 StructureElement elem = registry.getElement(p);
94 if (elem != null) {
95 elements.put(p, elem);
96 }
97 } else {
98 if (log.isTraceEnabled())
99 log.trace("An element is already registered for path "
100 + p + " and was not updated");
101 }
102
103 }
104 }
105
106 currentPath = path;
107 }
108
109 /** Gets the current path. */
110 public TreeSPath getCurrentPath() {
111 return currentPath;
112 }
113
114 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
115 public SortedMap<TreeSPath, PartSubList> getResultParts() {
116 return resultParts;
117 }
118
119 /** Used by ORM systems. */
120 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
121 this.resultParts = resultParts;
122 }
123
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.");
129 return;
130 }
131
132 if (isClosed) {
133 if (warnIfAlreadyClosed)
134 log.warn("Test Result #" + getUuid()
135 + " already closed. Doing nothing.");
136 return;
137 }
138
139 closeDate = new Date();
140
141 synchronized (listeners) {
142 for (TestResultListener<TreeTestResult> listener : listeners) {
143 listener.close(this);
144 }
145 }
146 isClosed = true;
147
148 if (log.isTraceEnabled())
149 log.trace("Test Result " + getUuid() + " closed.");
150 }
151
152 public Date getCloseDate() {
153 return closeDate;
154 }
155
156 /** Sets the close date (for ORM) */
157 public void setCloseDate(Date closeDate) {
158 this.closeDate = closeDate;
159 }
160
161 public void notifyTestRun(TestRun testRun) {
162 currentTestRun = testRun;
163 }
164
165 public SortedMap<TreeSPath, StructureElement> getElements() {
166 return elements;
167 }
168
169 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
170 this.elements = pathNames;
171 }
172
173 public String getUuid() {
174 return uuid;
175 }
176
177 public void setUuid(String uuid) {
178 this.uuid = uuid;
179 }
180
181 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
182 TreeSPath path) {
183 if (path == null)
184 throw new SlcException(
185 "Cannot retrieve element for a null path in result #"
186 + uuid);
187
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));
193 }
194 }
195 return relatedElements;
196 }
197
198 public TestRun getCurrentTestRun() {
199 return currentTestRun;
200 }
201
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);
207 else
208 return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate());
209 } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) {
210 return 1;
211 } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) {
212 return -1;
213 } else {
214 return compareUuid(ttr1, ttr2);
215 }
216 }
217
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");
222 else {
223 if (ttr1.getUuid().equals(ttr2.getUuid()))
224 return 0;
225 return ttr1.getUuid().compareTo(ttr2.getUuid());
226 }
227 }
228
229 public boolean equals(Object obj) {
230 if (obj instanceof TestResult)
231 return compareUuid(this, ((TestResult) obj)) == 0;
232 else
233 return false;
234 }
235
236 public int hashCode() {
237 if (uuid != null)
238 return uuid.hashCode();
239 else
240 return super.hashCode();
241 }
242
243 public Map<String, String> getAttributes() {
244 return attributes;
245 }
246
247 public void setAttributes(Map<String, String> attributes) {
248 this.attributes = attributes;
249 }
250
251 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed) {
252 this.warnIfAlreadyClosed = warnIfAlreadyClosed;
253 }
254
255 public List<SimpleAttachment> getAttachments() {
256 return attachments;
257 }
258
259 public void setAttachments(List<SimpleAttachment> attachments) {
260 this.attachments = attachments;
261 }
262
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,
269 attachment);
270 }
271 }
272 }
273
274 }