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