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
.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
;
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
;
43 * {@link TestResult} wrapping a JCR node of type
44 * {@link SlcTypes#SLC_TEST_RESULT}.
46 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
47 /** Should only be set for an already existing result. */
49 private Repository repository
;
50 private Session session
;
52 * For testing purposes, best practice is to not set them explicitely but
53 * via other mechanisms such as JAAS or SPring Security.
55 private Credentials credentials
= null;
56 private String resultType
= SlcTypes
.SLC_TEST_RESULT
;
58 /** cached for performance purposes */
59 private String nodeIdentifier
= null;
61 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
65 session
= repository
.login(credentials
);
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
));
83 } catch (Exception e
) {
84 JcrUtils
.discardQuietly(session
);
85 throw new SlcException("Cannot initialize JCR result", e
);
89 public void destroy() {
90 JcrUtils
.logoutQuietly(session
);
93 public Node
getNode() {
96 if (nodeIdentifier
!= null) {
97 return session
.getNodeByIdentifier(nodeIdentifier
);
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();
108 } catch (Exception e
) {
109 throw new SlcException("Cannot get result node", e
);
113 public void notifyTestRun(TestRun testRun
) {
116 public void addResultPart(TestResultPart testResultPart
) {
117 Node node
= getNode();
119 Node resultPartNode
= node
.addNode(SlcNames
.SLC_STATUS
,
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
);
138 public String
getUuid() {
139 Node node
= getNode();
141 return node
.getProperty(SLC_UUID
).getString();
142 } catch (Exception e
) {
143 throw new SlcException("Cannot get UUID from " + node
, e
);
147 /** JCR session is NOT logged out */
148 public void close() {
149 Node node
= getNode();
151 if (node
.hasNode(SLC_COMPLETED
))
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
);
162 public Date
getCloseDate() {
163 Node node
= getNode();
165 if (!node
.hasNode(SLC_COMPLETED
))
167 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
168 } catch (Exception e
) {
169 throw new SlcException("Cannot get close date from " + node
, e
);
173 public Map
<String
, String
> getAttributes() {
174 Node node
= getNode();
176 Map
<String
, String
> map
= new HashMap
<String
, String
>();
177 PropertyIterator pit
= node
.getProperties();
178 while (pit
.hasNext()) {
179 Property p
= pit
.nextProperty();
181 map
.put(p
.getName(), p
.getValue().getString());
184 } catch (Exception e
) {
185 throw new SlcException("Cannot get close date from " + node
, e
);
189 public void addAttachment(Attachment attachment
) {
193 public void setUuid(String uuid
) {
197 public void setRepository(Repository repository
) {
198 this.repository
= repository
;
201 public void setResultType(String resultType
) {
202 this.resultType
= resultType
;
205 public void setAttributes(Map
<String
, String
> attributes
) {
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
;
213 public void setCredentials(Credentials credentials
) {
214 this.credentials
= credentials
;
217 // public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
218 // this.logoutWhenDestroyed = logoutWhenDestroyed;