]> 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
Gracefully handle empty resource sets
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / TreeTestResult.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.core.test.tree;
18
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;
27
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;
43
44 /**
45 * Complex implementation of a test result compatible with a tree based
46 * structure.
47 */
48 public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
49 Comparable<TreeTestResult>, AttachmentsEnabled, Serializable {
50
51 private static final long serialVersionUID = 1L;
52 private final static Log log = LogFactory.getLog(TreeTestResult.class);
53
54 // Persistence data
55 private String uuid = UUID.randomUUID().toString();
56 private Date closeDate;
57
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>();
61
62 // Headers. Used to accelerate request on a specific test result.
63 private SortedMap<String, String> attributes = new TreeMap<String, String>();
64
65 // Runtime Data
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;
72
73 private transient List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();
74
75 /** Sets the list of listeners. */
76 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
77 this.listeners = listeners;
78 }
79
80 public void addResultPart(TestResultPart part) {
81 if (isClosed)
82 notifyIssue(
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.",
86 null);
87
88 if (currentPath == null)
89 throw new SlcException("No current path set.");
90
91 PartSubList subList = resultParts.get(currentPath);
92 if (subList == null) {
93 subList = new PartSubList();
94 resultParts.put(currentPath, subList);
95 }
96 if (part instanceof TestRunAware && currentTestRun != null) {
97 ((TestRunAware) part).notifyTestRun(currentTestRun);
98 }
99 subList.getParts().add(part);
100
101 // notify listeners
102 synchronized (listeners) {
103 for (TestResultListener<TreeTestResult> listener : listeners) {
104 listener.resultPartAdded(this, part);
105 }
106 }
107 }
108
109 protected void notifyIssue(String msg, Exception e) {
110 if (strictChecks)
111 throw new SlcException(msg, e);
112 else
113 log.error(msg, e);
114 }
115
116 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
117 TreeSPath path) {
118 if (registry != null) {
119 for (TreeSPath p : path.getHierarchyAsList()) {
120 if (!elements.containsKey(p)) {
121 StructureElement elem = registry.getElement(p);
122 if (elem != null) {
123 elements.put(p, elem);
124 }
125 } else {
126 if (log.isTraceEnabled())
127 log.trace("An element is already registered for path "
128 + p + " and was not updated");
129 }
130
131 }
132 }
133
134 currentPath = path;
135 }
136
137 /** Gets the current path. */
138 public TreeSPath getCurrentPath() {
139 return currentPath;
140 }
141
142 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
143 public SortedMap<TreeSPath, PartSubList> getResultParts() {
144 return resultParts;
145 }
146
147 /**
148 * Used by ORM systems. Changed to public in order to enable jcr persistence
149 */
150 public void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
151 this.resultParts = resultParts;
152 }
153
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.");
159 return;
160 }
161
162 if (isClosed) {
163 if (warnIfAlreadyClosed)
164 log.warn("Test Result #" + getUuid()
165 + " already closed. Doing nothing.");
166 return;
167 }
168
169 closeDate = new Date();
170
171 synchronized (listeners) {
172 for (TestResultListener<TreeTestResult> listener : listeners) {
173 listener.close(this);
174 }
175 }
176 isClosed = true;
177
178 if (log.isTraceEnabled())
179 log.trace("Test Result " + getUuid() + " closed.");
180 }
181
182 public Date getCloseDate() {
183 return closeDate;
184 }
185
186 /** Sets the close date (for ORM) */
187 public void setCloseDate(Date closeDate) {
188 this.closeDate = closeDate;
189 }
190
191 public void notifyTestRun(TestRun testRun) {
192 currentTestRun = testRun;
193 }
194
195 public SortedMap<TreeSPath, StructureElement> getElements() {
196 return elements;
197 }
198
199 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
200 this.elements = pathNames;
201 }
202
203 public String getUuid() {
204 return uuid;
205 }
206
207 public void setUuid(String uuid) {
208 this.uuid = uuid;
209 }
210
211 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
212 TreeSPath path) {
213 if (path == null)
214 throw new SlcException(
215 "Cannot retrieve element for a null path in result #"
216 + uuid);
217
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));
223 }
224 }
225 return relatedElements;
226 }
227
228 public TestRun getCurrentTestRun() {
229 return currentTestRun;
230 }
231
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);
237 else
238 return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate());
239 } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) {
240 return 1;
241 } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) {
242 return -1;
243 } else {
244 return compareUuid(ttr1, ttr2);
245 }
246 }
247
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");
252 else {
253 if (ttr1.getUuid().equals(ttr2.getUuid()))
254 return 0;
255 return ttr1.getUuid().compareTo(ttr2.getUuid());
256 }
257 }
258
259 public boolean equals(Object obj) {
260 if (obj instanceof TestResult)
261 return compareUuid(this, ((TestResult) obj)) == 0;
262 else
263 return false;
264 }
265
266 public int hashCode() {
267 if (uuid != null)
268 return uuid.hashCode();
269 else
270 return super.hashCode();
271 }
272
273 public SortedMap<String, String> getAttributes() {
274 return attributes;
275 }
276
277 public void setAttributes(SortedMap<String, String> attributes) {
278 this.attributes = attributes;
279 }
280
281 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed) {
282 this.warnIfAlreadyClosed = warnIfAlreadyClosed;
283 }
284
285 public List<SimpleAttachment> getAttachments() {
286 return attachments;
287 }
288
289 public void setAttachments(List<SimpleAttachment> attachments) {
290 this.attachments = attachments;
291 }
292
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,
299 attachment);
300 }
301 }
302 }
303
304 public void setStrictChecks(Boolean strictChecks) {
305 this.strictChecks = strictChecks;
306 }
307
308 }