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