]> 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
Introduce a factory bean to use execution resources
[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.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
9 import java.util.Vector;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.core.attachment.Attachment;
15 import org.argeo.slc.core.attachment.AttachmentsEnabled;
16 import org.argeo.slc.core.attachment.SimpleAttachment;
17 import org.argeo.slc.core.structure.tree.TreeSPath;
18 import org.argeo.slc.structure.StructureAware;
19 import org.argeo.slc.structure.StructureElement;
20 import org.argeo.slc.structure.StructureRegistry;
21 import org.argeo.slc.test.TestResult;
22 import org.argeo.slc.test.TestResultListener;
23 import org.argeo.slc.test.TestResultPart;
24 import org.argeo.slc.test.TestRun;
25 import org.argeo.slc.test.TestRunAware;
26
27 /**
28 * Complex implementation of a test result compatible with a tree based
29 * structure.
30 */
31 public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
32 Comparable<TreeTestResult>, AttachmentsEnabled {
33 private Log log = LogFactory.getLog(TreeTestResult.class);
34
35 private List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();
36
37 private TreeSPath currentPath;
38 private TestRun currentTestRun;
39
40 private Date closeDate;
41
42 private Boolean isClosed = false;
43
44 private Boolean warnIfAlreadyClosed = true;
45
46 private String uuid;
47
48 private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();
49 private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();
50 private List<SimpleAttachment> attachments = new ArrayList<SimpleAttachment>();
51
52 private Map<String, String> attributes = new TreeMap<String, String>();
53
54 /** Sets the list of listeners. */
55 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
56 this.listeners = listeners;
57 }
58
59 public void addResultPart(TestResultPart part) {
60 if (isClosed)
61 throw new SlcException("Cannot result parts to a closed result");
62
63 if (currentPath == null)
64 throw new SlcException("No current path set.");
65
66 PartSubList subList = resultParts.get(currentPath);
67 if (subList == null) {
68 subList = new PartSubList();
69 resultParts.put(currentPath, subList);
70 }
71 if (part instanceof TestRunAware && currentTestRun != null) {
72 ((TestRunAware) part).notifyTestRun(currentTestRun);
73 }
74 subList.getParts().add(part);
75
76 // notify listeners
77 synchronized (listeners) {
78 for (TestResultListener<TreeTestResult> listener : listeners) {
79 listener.resultPartAdded(this, part);
80 }
81 }
82 }
83
84 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
85 TreeSPath path) {
86 if (registry != null) {
87 for (TreeSPath p : path.getHierarchyAsList()) {
88 if (!elements.containsKey(p)) {
89 StructureElement elem = registry.getElement(p);
90 if (elem != null) {
91 elements.put(p, elem);
92 }
93 } else {
94 if (log.isTraceEnabled())
95 log.trace("An element is already registered for path "
96 + p + " and was not updated");
97 }
98
99 }
100 }
101
102 currentPath = path;
103 }
104
105 /** Gets the current path. */
106 public TreeSPath getCurrentPath() {
107 return currentPath;
108 }
109
110 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
111 public SortedMap<TreeSPath, PartSubList> getResultParts() {
112 return resultParts;
113 }
114
115 /** Used by ORM systems. */
116 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
117 this.resultParts = resultParts;
118 }
119
120 public void close() {
121 if (resultParts.size() == 0) {
122 if (log.isTraceEnabled())
123 log.trace("Test Result #" + getUuid()
124 + " contains no results, no need to close it.");
125 return;
126 }
127
128 if (isClosed) {
129 if (warnIfAlreadyClosed)
130 log.warn("Test Result #" + getUuid()
131 + " already closed. Doing nothing.");
132 return;
133 }
134
135 closeDate = new Date();
136
137 synchronized (listeners) {
138 for (TestResultListener<TreeTestResult> listener : listeners) {
139 listener.close(this);
140 }
141 }
142 isClosed = true;
143
144 if (log.isTraceEnabled())
145 log.trace("Test Result " + getUuid() + " closed.");
146 }
147
148 public Date getCloseDate() {
149 return closeDate;
150 }
151
152 /** Sets the close date (for ORM) */
153 public void setCloseDate(Date closeDate) {
154 this.closeDate = closeDate;
155 }
156
157 public void notifyTestRun(TestRun testRun) {
158 currentTestRun = testRun;
159 }
160
161 public SortedMap<TreeSPath, StructureElement> getElements() {
162 return elements;
163 }
164
165 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
166 this.elements = pathNames;
167 }
168
169 public String getUuid() {
170 return uuid;
171 }
172
173 public void setUuid(String uuid) {
174 this.uuid = uuid;
175 }
176
177 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
178 TreeSPath path) {
179 if (path == null)
180 throw new SlcException(
181 "Cannot retrieve element for a null path in result #"
182 + uuid);
183
184 SortedMap<TreeSPath, StructureElement> relatedElements = new TreeMap<TreeSPath, StructureElement>();
185 List<TreeSPath> hierarchy = path.getHierarchyAsList();
186 for (TreeSPath currPath : elements.keySet()) {
187 if (hierarchy.contains(currPath)) {
188 relatedElements.put(currPath, elements.get(currPath));
189 }
190 }
191 return relatedElements;
192 }
193
194 public TestRun getCurrentTestRun() {
195 return currentTestRun;
196 }
197
198 public int compareTo(TreeTestResult ttr2) {
199 TreeTestResult ttr1 = this;
200 if (ttr1.getCloseDate() != null && ttr2.getCloseDate() != null) {
201 return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate());
202 } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) {
203 return 1;
204 } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) {
205 return -1;
206 } else {
207 return ttr1.getUuid().compareTo(ttr2.getUuid());
208 }
209 }
210
211 public Map<String, String> getAttributes() {
212 return attributes;
213 }
214
215 public void setAttributes(Map<String, String> attributes) {
216 this.attributes = attributes;
217 }
218
219 public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed) {
220 this.warnIfAlreadyClosed = warnIfAlreadyClosed;
221 }
222
223 public List<SimpleAttachment> getAttachments() {
224 return attachments;
225 }
226
227 public void setAttachments(List<SimpleAttachment> attachments) {
228 this.attachments = attachments;
229 }
230
231 public void addAttachment(Attachment attachment) {
232 attachments.add((SimpleAttachment) attachment);
233 synchronized (listeners) {
234 for (TestResultListener<TreeTestResult> listener : listeners) {
235 if (listener instanceof TreeTestResultListener)
236 ((TreeTestResultListener) listener).addAttachment(this,
237 attachment);
238 }
239 }
240 }
241
242 }