]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
Improve formatting
[gpl/argeo-slc.git] / 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.util.Date;
4 import java.util.List;
5 import java.util.SortedMap;
6 import java.util.TreeMap;
7 import java.util.Vector;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import org.argeo.slc.core.SlcException;
13 import org.argeo.slc.core.structure.StructureAware;
14 import org.argeo.slc.core.structure.StructureElement;
15 import org.argeo.slc.core.structure.StructureRegistry;
16 import org.argeo.slc.core.structure.tree.TreeSPath;
17 import org.argeo.slc.core.test.TestResult;
18 import org.argeo.slc.core.test.TestResultListener;
19 import org.argeo.slc.core.test.TestResultPart;
20 import org.argeo.slc.core.test.TestRun;
21 import org.argeo.slc.core.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 String uuid;
41
42 private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();
43 private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();
44
45 /** Sets the list of listeners. */
46 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
47 this.listeners = listeners;
48 }
49
50 public void addResultPart(TestResultPart part) {
51 if (currentPath == null) {
52 throw new SlcException("No current path set.");
53 }
54 PartSubList subList = resultParts.get(currentPath);
55 if (subList == null) {
56 subList = new PartSubList();
57 resultParts.put(currentPath, subList);
58 }
59 if (part instanceof TestRunAware && currentTestRun != null) {
60 ((TestRunAware) part).notifyTestRun(currentTestRun);
61 }
62 subList.getParts().add(part);
63
64 // notify listeners
65 synchronized (listeners) {
66 for (TestResultListener<TreeTestResult> listener : listeners) {
67 listener.resultPartAdded(this, part);
68 }
69 }
70 }
71
72 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
73 TreeSPath path) {
74 if (registry != null) {
75 for (TreeSPath p : path.getHierarchyAsList()) {
76 if (!elements.containsKey(p)) {
77 StructureElement elem = registry.getElement(p);
78 if (elem != null) {
79 elements.put(p, elem);
80 }
81 } else {
82 if (log.isTraceEnabled())
83 log.trace("An element is already registered for path "
84 + p + " and was not updated");
85 }
86
87 }
88 }
89
90 currentPath = (TreeSPath) path;
91 }
92
93 /** Gets the current path. */
94 public TreeSPath getCurrentPath() {
95 return currentPath;
96 }
97
98 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
99 public SortedMap<TreeSPath, PartSubList> getResultParts() {
100 return resultParts;
101 }
102
103 /** Used by ORM systems. */
104 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
105 this.resultParts = resultParts;
106 }
107
108 public void close() {
109 if (isClosed) {
110 throw new SlcException("Test Result #" + getUuid()
111 + " alredy closed.");
112 }
113 closeDate = new Date();
114
115 synchronized (listeners) {
116 for (TestResultListener<TreeTestResult> listener : listeners) {
117 listener.close(this);
118 }
119 listeners.clear();
120 }
121 isClosed = true;
122
123 log.info("Test Result #" + getUuid() + " closed.");
124 }
125
126 public Date getCloseDate() {
127 return closeDate;
128 }
129
130 /** Sets the close date (for ORM) */
131 public void setCloseDate(Date closeDate) {
132 this.closeDate = closeDate;
133 }
134
135 public void notifyTestRun(TestRun testRun) {
136 currentTestRun = testRun;
137 }
138
139 public SortedMap<TreeSPath, StructureElement> getElements() {
140 return elements;
141 }
142
143 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
144 this.elements = pathNames;
145 }
146
147 public String getUuid() {
148 return uuid;
149 }
150
151 public void setUuid(String uuid) {
152 this.uuid = uuid;
153 }
154
155 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
156 TreeSPath path) {
157 SortedMap<TreeSPath, StructureElement> relatedElements = new TreeMap<TreeSPath, StructureElement>();
158 List<TreeSPath> hierarchy = path.getHierarchyAsList();
159 for (TreeSPath currPath : elements.keySet()) {
160 if (hierarchy.contains(currPath)) {
161 relatedElements.put(currPath, elements.get(currPath));
162 }
163 }
164 return relatedElements;
165 }
166
167 public TestRun getCurrentTestRun() {
168 return currentTestRun;
169 }
170
171 public int compareTo(TreeTestResult ttr2) {
172 TreeTestResult ttr1 = this;
173 if (ttr1.getCloseDate() != null && ttr2.getCloseDate() != null) {
174 return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate());
175 } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) {
176 return 1;
177 } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) {
178 return -1;
179 } else {
180 return ttr1.getUuid().compareTo(ttr2.getUuid());
181 }
182 }
183
184 }