2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jcr
;
18 import java
.util
.Date
;
19 import java
.util
.GregorianCalendar
;
20 import java
.util
.HashMap
;
22 import java
.util
.UUID
;
24 import javax
.jcr
.Node
;
25 import javax
.jcr
.Property
;
26 import javax
.jcr
.PropertyIterator
;
27 import javax
.jcr
.Session
;
28 import javax
.jcr
.query
.Query
;
29 import javax
.jcr
.query
.QueryManager
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.slc
.SlcException
;
33 import org
.argeo
.slc
.core
.attachment
.Attachment
;
34 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
35 import org
.argeo
.slc
.test
.TestResult
;
36 import org
.argeo
.slc
.test
.TestResultPart
;
37 import org
.argeo
.slc
.test
.TestRun
;
38 import org
.argeo
.slc
.test
.TestStatus
;
40 /** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_RESULT}. */
41 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
42 /** Should only be set for an already existing result. */
44 private Session session
;
45 private String resultType
= SlcTypes
.SLC_RESULT
;
47 /** cached for performance purposes */
48 private String nodeIdentifier
= null;
50 private Boolean logoutWhenDestroyed
= true;
52 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
58 uuid
= UUID
.randomUUID().toString();
59 String path
= SlcJcrUtils
.createResultPath(uuid
);
60 Node resultNode
= JcrUtils
.mkdirs(session
, path
, resultType
);
61 resultNode
.setProperty(SLC_UUID
, uuid
);
62 for (String attr
: attributes
.keySet()) {
63 String property
= attr
;
64 // compatibility with legacy applications
65 if ("testCase".equals(attr
))
66 property
= SLC_TEST_CASE
;
67 else if ("testCaseType".equals(attr
))
68 property
= SLC_TEST_CASE_TYPE
;
69 resultNode
.setProperty(property
, attributes
.get(attr
));
73 } catch (Exception e
) {
74 JcrUtils
.discardQuietly(session
);
75 throw new SlcException("Cannot initialize JCR result", e
);
79 public void destroy() {
80 if (logoutWhenDestroyed
)
81 JcrUtils
.logoutQuietly(session
);
84 public Node
getNode() {
87 if (nodeIdentifier
!= null) {
88 return session
.getNodeByIdentifier(nodeIdentifier
);
90 QueryManager qm
= session
.getWorkspace().getQueryManager();
91 Query q
= qm
.createQuery(
92 "select * from [slc:result] where [slc:uuid]='" + uuid
93 + "'", Query
.JCR_SQL2
);
94 resultNode
= JcrUtils
.querySingleNode(q
);
95 if (resultNode
!= null)
96 nodeIdentifier
= resultNode
.getIdentifier();
99 } catch (Exception e
) {
100 throw new SlcException("Cannot get result node", e
);
104 public void notifyTestRun(TestRun testRun
) {
107 public void addResultPart(TestResultPart testResultPart
) {
108 Node node
= getNode();
110 // TODO: find a better way to name it by default
111 String partName
= Long
.toString(System
.currentTimeMillis());
112 Node resultPartNode
= node
.addNode(partName
, SlcTypes
.SLC_CHECK
);
113 resultPartNode
.setProperty(SLC_SUCCESS
,
114 testResultPart
.getStatus() == TestStatus
.PASSED
);
115 if (testResultPart
.getMessage() != null)
116 resultPartNode
.setProperty(SLC_MESSAGE
,
117 testResultPart
.getMessage());
118 if (testResultPart
.getExceptionMessage() != null)
119 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
120 testResultPart
.getExceptionMessage());
121 JcrUtils
.updateLastModified(node
);
122 node
.getSession().save();
123 } catch (Exception e
) {
124 JcrUtils
.discardUnderlyingSessionQuietly(node
);
125 throw new SlcException("Cannot get UUID from " + node
, e
);
130 public String
getUuid() {
131 Node node
= getNode();
133 return node
.getProperty(SLC_UUID
).getString();
134 } catch (Exception e
) {
135 throw new SlcException("Cannot get UUID from " + node
, e
);
139 public void close() {
140 Node node
= getNode();
142 if (node
.hasNode(SLC_COMPLETED
))
144 node
.setProperty(SLC_COMPLETED
, new GregorianCalendar());
145 JcrUtils
.updateLastModified(node
);
146 node
.getSession().save();
147 } catch (Exception e
) {
148 JcrUtils
.discardUnderlyingSessionQuietly(node
);
149 throw new SlcException("Cannot get close date from " + node
, e
);
151 if (logoutWhenDestroyed
)
152 JcrUtils
.logoutQuietly(session
);
155 public Date
getCloseDate() {
156 Node node
= getNode();
158 if (!node
.hasNode(SLC_COMPLETED
))
160 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
161 } catch (Exception e
) {
162 throw new SlcException("Cannot get close date from " + node
, e
);
166 public Map
<String
, String
> getAttributes() {
167 Node node
= getNode();
169 Map
<String
, String
> map
= new HashMap
<String
, String
>();
170 PropertyIterator pit
= node
.getProperties();
171 while (pit
.hasNext()) {
172 Property p
= pit
.nextProperty();
174 map
.put(p
.getName(), p
.getValue().getString());
177 } catch (Exception e
) {
178 throw new SlcException("Cannot get close date from " + node
, e
);
182 public void addAttachment(Attachment attachment
) {
186 public void setUuid(String uuid
) {
190 public void setSession(Session session
) {
191 this.session
= session
;
194 public void setResultType(String resultType
) {
195 this.resultType
= resultType
;
198 public void setAttributes(Map
<String
, String
> attributes
) {
200 throw new SlcException(
201 "Attributes cannot be set on an already initialized test result."
202 + " Update the related JCR node directly instead.");
203 this.attributes
= attributes
;
206 public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed
) {
207 this.logoutWhenDestroyed
= logoutWhenDestroyed
;