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
.Repository
;
28 import javax
.jcr
.Session
;
29 import javax
.jcr
.query
.Query
;
30 import javax
.jcr
.query
.QueryManager
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.argeo
.slc
.core
.attachment
.Attachment
;
35 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
36 import org
.argeo
.slc
.test
.TestResult
;
37 import org
.argeo
.slc
.test
.TestResultPart
;
38 import org
.argeo
.slc
.test
.TestRun
;
39 import org
.argeo
.slc
.test
.TestStatus
;
41 /** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_TEST_RESULT}. */
42 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
43 /** Should only be set for an already existing result. */
45 private Repository repository
;
46 private Session session
;
47 private String resultType
= SlcTypes
.SLC_TEST_RESULT
;
49 /** cached for performance purposes */
50 private String nodeIdentifier
= null;
52 private Boolean logoutWhenDestroyed
= true;
54 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
58 session
= repository
.login();
61 uuid
= UUID
.randomUUID().toString();
62 String path
= SlcJcrUtils
.createResultPath(session
.getUserID(),
64 Node resultNode
= JcrUtils
.mkdirs(session
, path
, resultType
);
65 resultNode
.setProperty(SLC_UUID
, uuid
);
66 for (String attr
: attributes
.keySet()) {
67 String property
= attr
;
68 // compatibility with legacy applications
69 if ("testCase".equals(attr
))
70 property
= SLC_TEST_CASE
;
71 else if ("testCaseType".equals(attr
))
72 property
= SLC_TEST_CASE_TYPE
;
73 resultNode
.setProperty(property
, attributes
.get(attr
));
77 } catch (Exception e
) {
78 JcrUtils
.discardQuietly(session
);
79 throw new SlcException("Cannot initialize JCR result", e
);
83 public void destroy() {
84 if (logoutWhenDestroyed
)
85 JcrUtils
.logoutQuietly(session
);
88 public Node
getNode() {
91 if (nodeIdentifier
!= null) {
92 return session
.getNodeByIdentifier(nodeIdentifier
);
94 QueryManager qm
= session
.getWorkspace().getQueryManager();
95 Query q
= qm
.createQuery(
96 "select * from [" + SlcTypes
.SLC_TEST_RESULT
97 + "] where [slc:uuid]='" + uuid
98 + "'", Query
.JCR_SQL2
);
99 resultNode
= JcrUtils
.querySingleNode(q
);
100 if (resultNode
!= null)
101 nodeIdentifier
= resultNode
.getIdentifier();
104 } catch (Exception e
) {
105 throw new SlcException("Cannot get result node", e
);
109 public void notifyTestRun(TestRun testRun
) {
112 public void addResultPart(TestResultPart testResultPart
) {
113 Node node
= getNode();
115 Node resultPartNode
= node
.addNode(SlcNames
.SLC_STATUS
, SlcTypes
.SLC_CHECK
);
116 resultPartNode
.setProperty(SLC_SUCCESS
,
117 testResultPart
.getStatus() == TestStatus
.PASSED
);
118 if (testResultPart
.getMessage() != null)
119 resultPartNode
.setProperty(SLC_MESSAGE
,
120 testResultPart
.getMessage());
121 if (testResultPart
.getExceptionMessage() != null)
122 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
123 testResultPart
.getExceptionMessage());
124 JcrUtils
.updateLastModified(node
);
125 node
.getSession().save();
126 } catch (Exception e
) {
127 JcrUtils
.discardUnderlyingSessionQuietly(node
);
128 throw new SlcException("Cannot get UUID from " + node
, e
);
133 public String
getUuid() {
134 Node node
= getNode();
136 return node
.getProperty(SLC_UUID
).getString();
137 } catch (Exception e
) {
138 throw new SlcException("Cannot get UUID from " + node
, e
);
142 public void close() {
143 Node node
= getNode();
145 if (node
.hasNode(SLC_COMPLETED
))
147 node
.setProperty(SLC_COMPLETED
, new GregorianCalendar());
148 JcrUtils
.updateLastModified(node
);
149 node
.getSession().save();
150 } catch (Exception e
) {
151 JcrUtils
.discardUnderlyingSessionQuietly(node
);
152 throw new SlcException("Cannot get close date from " + node
, e
);
154 if (logoutWhenDestroyed
)
155 JcrUtils
.logoutQuietly(session
);
158 public Date
getCloseDate() {
159 Node node
= getNode();
161 if (!node
.hasNode(SLC_COMPLETED
))
163 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
164 } catch (Exception e
) {
165 throw new SlcException("Cannot get close date from " + node
, e
);
169 public Map
<String
, String
> getAttributes() {
170 Node node
= getNode();
172 Map
<String
, String
> map
= new HashMap
<String
, String
>();
173 PropertyIterator pit
= node
.getProperties();
174 while (pit
.hasNext()) {
175 Property p
= pit
.nextProperty();
177 map
.put(p
.getName(), p
.getValue().getString());
180 } catch (Exception e
) {
181 throw new SlcException("Cannot get close date from " + node
, e
);
185 public void addAttachment(Attachment attachment
) {
189 public void setUuid(String uuid
) {
193 public void setRepository(Repository repository
) {
194 this.repository
= repository
;
197 public void setResultType(String resultType
) {
198 this.resultType
= resultType
;
201 public void setAttributes(Map
<String
, String
> attributes
) {
203 throw new SlcException(
204 "Attributes cannot be set on an already initialized test result."
205 + " Update the related JCR node directly instead.");
206 this.attributes
= attributes
;
209 // public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
210 // this.logoutWhenDestroyed = logoutWhenDestroyed;