]> 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
Add failOnError property
[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 final static 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 private Boolean strictChecks = false;
59
60 /** Sets the list of listeners. */
61 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
62 this.listeners = listeners;
63 }
64
65 public void addResultPart(TestResultPart part) {
66 if (isClosed)
67 notifyIssue(
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.",
71 null);
72
73 if (currentPath == null)
74 throw new SlcException("No current path set.");
75
76 PartSubList subList = resultParts.get(currentPath);
77 if (subList == null) {
78 subList = new PartSubList();
79 resultParts.put(currentPath, subList);
80 }
81 if (part instanceof TestRunAware && currentTestRun != null) {
82 ((TestRunAware) part).notifyTestRun(currentTestRun);
83 }
84 subList.getParts().add(part);
85
86 // notify listeners
87 synchronized (listeners) {
88 for (TestResultListener<TreeTestResult> listener : listeners) {
89 listener.resultPartAdded(this, part);
90 }
91 }
92 }
93
94 protected void notifyIssue(String msg, Exception e) {
95 if (strictChecks)
96 throw new SlcException(msg, e);
97 else
98 log.error(msg, e);
99 }
100
101 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
102 TreeSPath path) {
103 if (registry != null) {
104 for (TreeSPath p : path.getHierarchyAsList()) {
105 if (!elements.containsKey(p)) {
106 StructureElement elem = registry.getElement(p);
107 if (elem != null) {
108 elements.put(p, elem);
109 }
110 } else {
111 if (log.isTraceEnabled())
112 log.trace("An element is already registered for path "
113 + p + " and was not updated");
114 }
115
116 }
117 }
118
119 currentPath = path;
120 }
121
122 /** Gets the current path. */
123 public TreeSPath getCurrentPath() {
124 return currentPath;
125 }
126
127 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
128 public SortedMap<TreeSPath, PartSubList> getResultParts() {
129 return resultParts;
130 }
131
132 /** Used by ORM systems. */
133 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
134 this.resultParts = resultParts;
135 }
136
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.");
142 return;
143 }
144
145 if (isClosed) {
146 if (warnIfAlreadyClosed)
147 log.warn("Test Result #" + getUuid()
148 + " already closed. Doing nothing.");
149 return;
150 }
151
152 closeDate = new Date();
153
154 synchronized (listeners) {
155 for (TestResultListener<TreeTestResult> listener : listeners) {
156 listener.close(this);
157 }
158 }
159 isClosed = true;
160
161 if (log.isTraceEnabled())
162 log.trace("Test Result " + getUuid() + " closed.");
163 }
164
165 public Date getCloseDate() {
166 return closeDate;
167 }
168
169 /** Sets the close date (for ORM) */
170 public void setCloseDate(Date closeDate) {
171 this.closeDate = closeDate;
172 }
173
174 public void notifyTestRun(TestRun testRun) {
175 currentTestRun = testRun;
176 }
177
178 public SortedMap<TreeSPath, StructureElement> getElements() {
179 return elements;
180 }
181
182 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
183 this.elements = pathNames;
184 }
185
186 public String getUuid() {
187 return uuid;
188 }
189
190 public void setUuid(String uuid) {
191 this.uuid = uuid;
192 }
193
194 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
195 TreeSPath path) {
196 if (path == null)
197 throw new SlcException(
198 "Cannot retrieve element for a null path in result #"
199 + uuid);
200
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));
206 }
207 }
208 return relatedElements;
209 }
210
211 public TestRun getCurrentTestRun() {
212 return currentTestRun;
213 }
214
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);
220 else
221 return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate());
222 } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) {
223 return 1;
224 } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) {
225 return -1;
226 } else {
227 return compareUuid(ttr1, ttr2);
228 }
229 }
230
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");
235 else {
236 if (ttr1.getUuid().equals(ttr2.getUuid()))
237 return 0;
238 return ttr1.getUuid().compareTo(ttr2.getUuid());
239 }
240 }
241
242 public boolean equals(Object obj) {
243 if (obj instanceof TestResult)
244 return compareUuid(this, ((TestResult) obj)) == 0;
245 else
246 return false;
247 }
248
249 public int hashCode() {
250 if (uuid != null)
251 return uuid.hashCode();
252 else
253 return super.hashCode();
254 }
255
256 public Map<String, String> getAttributes() {
257 return attributes;
258 }
259
260 public void setAttributes(Map<String, String> attributes) {
261 this.attributes = attributes;
262 }
263
264 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed) {
265 this.warnIfAlreadyClosed = warnIfAlreadyClosed;
266 }
267
268 public List<SimpleAttachment> getAttachments() {
269 return attachments;
270 }
271
272 public void setAttachments(List<SimpleAttachment> attachments) {
273 this.attachments = attachments;
274 }
275
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,
282 attachment);
283 }
284 }
285 }
286
287 public void setStrictChecks(Boolean strictChecks) {
288 this.strictChecks = strictChecks;
289 }
290
291 }