]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java
d7f0b434d5d74b0c7814f93144573b504d9b86d1
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / JcrTestResult.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.jcr;
17
18 import java.util.Date;
19 import java.util.GregorianCalendar;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import javax.jcr.Credentials;
25 import javax.jcr.Node;
26 import javax.jcr.Property;
27 import javax.jcr.PropertyIterator;
28 import javax.jcr.Repository;
29 import javax.jcr.Session;
30 import javax.jcr.query.Query;
31 import javax.jcr.query.QueryManager;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.argeo.jcr.JcrUtils;
36 import org.argeo.slc.SlcException;
37 import org.argeo.slc.core.attachment.Attachment;
38 import org.argeo.slc.core.attachment.AttachmentsEnabled;
39 import org.argeo.slc.test.TestResult;
40 import org.argeo.slc.test.TestResultPart;
41 import org.argeo.slc.test.TestRun;
42 import org.argeo.slc.test.TestStatus;
43
44 /**
45 * {@link TestResult} wrapping a JCR node of type
46 * {@link SlcTypes#SLC_TEST_RESULT}.
47 */
48 public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
49 private final static Log log = LogFactory.getLog(JcrTestResult.class);
50
51 /** Should only be set for an already existing result. */
52 private String uuid;
53 private Repository repository;
54 private Session session;
55 /**
56 * For testing purposes, best practice is to not set them explicitely but
57 * via other mechanisms such as JAAS or SPring Security.
58 */
59 private Credentials credentials = null;
60 private String resultType = SlcTypes.SLC_TEST_RESULT;
61
62 /** cached for performance purposes */
63 private String nodeIdentifier = null;
64
65 private Map<String, String> attributes = new HashMap<String, String>();
66
67 public void init() {
68 try {
69 session = repository.login(credentials);
70 if (uuid == null) {
71 // create new result
72 uuid = UUID.randomUUID().toString();
73 String path = SlcJcrUtils.createResultPath(session, uuid);
74 Node resultNode = JcrUtils.mkdirs(session, path, resultType);
75 resultNode.setProperty(SLC_UUID, uuid);
76 for (String attr : attributes.keySet()) {
77 String property = attr;
78 // compatibility with legacy applications
79 if ("testCase".equals(attr))
80 property = SLC_TEST_CASE;
81 else if ("testCaseType".equals(attr))
82 property = SLC_TEST_CASE_TYPE;
83 resultNode.setProperty(property, attributes.get(attr));
84 }
85 session.save();
86 if (log.isDebugEnabled())
87 log.debug("Created test result " + uuid);
88 }
89 } catch (Exception e) {
90 JcrUtils.discardQuietly(session);
91 throw new SlcException("Cannot initialize JCR result", e);
92 }
93 }
94
95 public void destroy() {
96 JcrUtils.logoutQuietly(session);
97 if (log.isTraceEnabled())
98 log.trace("Logged out session for result " + uuid);
99 }
100
101 public Node getNode() {
102 try {
103 Node resultNode;
104 if (nodeIdentifier != null) {
105 return session.getNodeByIdentifier(nodeIdentifier);
106 } else {
107 QueryManager qm = session.getWorkspace().getQueryManager();
108 Query q = qm.createQuery("select * from ["
109 + SlcTypes.SLC_TEST_RESULT + "] where [slc:uuid]='"
110 + uuid + "'", Query.JCR_SQL2);
111 resultNode = JcrUtils.querySingleNode(q);
112 if (resultNode != null)
113 nodeIdentifier = resultNode.getIdentifier();
114 }
115 return resultNode;
116 } catch (Exception e) {
117 throw new SlcException("Cannot get result node", e);
118 }
119 }
120
121 public void notifyTestRun(TestRun testRun) {
122 // TODO store meta data about the test running
123 // if (log.isDebugEnabled())
124 // log.debug("Running test "
125 // + testRun.getTestDefinition().getClass().getName() + "...");
126 }
127
128 public void addResultPart(TestResultPart testResultPart) {
129 Node node = getNode();
130 try {
131 Node resultPartNode = node.addNode(SlcNames.SLC_STATUS,
132 SlcTypes.SLC_CHECK);
133 resultPartNode.setProperty(SLC_SUCCESS,
134 testResultPart.getStatus() == TestStatus.PASSED);
135 if (testResultPart.getMessage() != null)
136 resultPartNode.setProperty(SLC_MESSAGE,
137 testResultPart.getMessage());
138 if (testResultPart.getExceptionMessage() != null)
139 resultPartNode.setProperty(SLC_ERROR_MESSAGE,
140 testResultPart.getExceptionMessage());
141 JcrUtils.updateLastModified(node);
142 node.getSession().save();
143 } catch (Exception e) {
144 JcrUtils.discardUnderlyingSessionQuietly(node);
145 throw new SlcException("Cannot get UUID from " + node, e);
146 }
147
148 }
149
150 public String getUuid() {
151 Node node = getNode();
152 try {
153 return node.getProperty(SLC_UUID).getString();
154 } catch (Exception e) {
155 throw new SlcException("Cannot get UUID from " + node, e);
156 }
157 }
158
159 /** JCR session is NOT logged out */
160 public void close() {
161 Node node = getNode();
162 try {
163 if (node.hasNode(SLC_COMPLETED))
164 return;
165 node.setProperty(SLC_COMPLETED, new GregorianCalendar());
166 JcrUtils.updateLastModified(node);
167 node.getSession().save();
168 } catch (Exception e) {
169 JcrUtils.discardUnderlyingSessionQuietly(node);
170 throw new SlcException("Cannot get close date from " + node, e);
171 }
172 }
173
174 public Date getCloseDate() {
175 Node node = getNode();
176 try {
177 if (!node.hasNode(SLC_COMPLETED))
178 return null;
179 return node.getProperty(SLC_COMPLETED).getDate().getTime();
180 } catch (Exception e) {
181 throw new SlcException("Cannot get close date from " + node, e);
182 }
183 }
184
185 public Map<String, String> getAttributes() {
186 Node node = getNode();
187 try {
188 Map<String, String> map = new HashMap<String, String>();
189 PropertyIterator pit = node.getProperties();
190 while (pit.hasNext()) {
191 Property p = pit.nextProperty();
192 if (!p.isMultiple())
193 map.put(p.getName(), p.getValue().getString());
194 }
195 return map;
196 } catch (Exception e) {
197 throw new SlcException("Cannot get close date from " + node, e);
198 }
199 }
200
201 public void addAttachment(Attachment attachment) {
202 // TODO implement it
203 }
204
205 public void setUuid(String uuid) {
206 this.uuid = uuid;
207 }
208
209 public void setRepository(Repository repository) {
210 this.repository = repository;
211 }
212
213 public void setResultType(String resultType) {
214 this.resultType = resultType;
215 }
216
217 public void setAttributes(Map<String, String> attributes) {
218 if (uuid != null)
219 throw new SlcException(
220 "Attributes cannot be set on an already initialized test result."
221 + " Update the related JCR node directly instead.");
222 this.attributes = attributes;
223 }
224
225 public void setCredentials(Credentials credentials) {
226 this.credentials = credentials;
227 }
228 }