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
.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
;
45 * {@link TestResult} wrapping a JCR node of type
46 * {@link SlcTypes#SLC_TEST_RESULT}.
48 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
49 private final static Log log
= LogFactory
.getLog(JcrTestResult
.class);
51 /** Should only be set for an already existing result. */
53 private Repository repository
;
54 private Session session
;
56 * For testing purposes, best practice is to not set them explicitely but
57 * via other mechanisms such as JAAS or SPring Security.
59 private Credentials credentials
= null;
60 private String resultType
= SlcTypes
.SLC_TEST_RESULT
;
62 /** cached for performance purposes */
63 private String nodeIdentifier
= null;
65 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
69 session
= repository
.login(credentials
);
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
));
86 if (log
.isDebugEnabled())
87 log
.debug("Created test result " + uuid
);
89 } catch (Exception e
) {
90 JcrUtils
.discardQuietly(session
);
91 throw new SlcException("Cannot initialize JCR result", e
);
95 public void destroy() {
96 JcrUtils
.logoutQuietly(session
);
97 if (log
.isTraceEnabled())
98 log
.trace("Logged out session for result " + uuid
);
101 public Node
getNode() {
104 if (nodeIdentifier
!= null) {
105 return session
.getNodeByIdentifier(nodeIdentifier
);
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();
116 } catch (Exception e
) {
117 throw new SlcException("Cannot get result node", e
);
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() + "...");
128 public void addResultPart(TestResultPart testResultPart
) {
129 Node node
= getNode();
131 Node resultPartNode
= node
.addNode(SlcNames
.SLC_STATUS
,
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
);
150 public String
getUuid() {
151 Node node
= getNode();
153 return node
.getProperty(SLC_UUID
).getString();
154 } catch (Exception e
) {
155 throw new SlcException("Cannot get UUID from " + node
, e
);
159 /** JCR session is NOT logged out */
160 public void close() {
161 Node node
= getNode();
163 if (node
.hasNode(SLC_COMPLETED
))
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
);
174 public Date
getCloseDate() {
175 Node node
= getNode();
177 if (!node
.hasNode(SLC_COMPLETED
))
179 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
180 } catch (Exception e
) {
181 throw new SlcException("Cannot get close date from " + node
, e
);
185 public Map
<String
, String
> getAttributes() {
186 Node node
= getNode();
188 Map
<String
, String
> map
= new HashMap
<String
, String
>();
189 PropertyIterator pit
= node
.getProperties();
190 while (pit
.hasNext()) {
191 Property p
= pit
.nextProperty();
193 map
.put(p
.getName(), p
.getValue().getString());
196 } catch (Exception e
) {
197 throw new SlcException("Cannot get close date from " + node
, e
);
201 public void addAttachment(Attachment attachment
) {
205 public void setUuid(String uuid
) {
209 public void setRepository(Repository repository
) {
210 this.repository
= repository
;
213 public void setResultType(String resultType
) {
214 this.resultType
= resultType
;
217 public void setAttributes(Map
<String
, String
> attributes
) {
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
;
225 public void setCredentials(Credentials credentials
) {
226 this.credentials
= credentials
;