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