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
;
42 * {@link TestResult} wrapping a JCR node of type
43 * {@link SlcTypes#SLC_TEST_RESULT}.
45 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
46 /** Should only be set for an already existing result. */
48 private Repository repository
;
49 private Session session
;
50 private String resultType
= SlcTypes
.SLC_TEST_RESULT
;
52 /** cached for performance purposes */
53 private String nodeIdentifier
= null;
55 private Boolean logoutWhenDestroyed
= true;
57 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
61 session
= repository
.login();
64 uuid
= UUID
.randomUUID().toString();
65 String path
= SlcJcrUtils
.createResultPath(session
, uuid
);
66 Node resultNode
= JcrUtils
.mkdirs(session
, path
, resultType
);
67 resultNode
.setProperty(SLC_UUID
, uuid
);
68 for (String attr
: attributes
.keySet()) {
69 String property
= attr
;
70 // compatibility with legacy applications
71 if ("testCase".equals(attr
))
72 property
= SLC_TEST_CASE
;
73 else if ("testCaseType".equals(attr
))
74 property
= SLC_TEST_CASE_TYPE
;
75 resultNode
.setProperty(property
, attributes
.get(attr
));
79 } catch (Exception e
) {
80 JcrUtils
.discardQuietly(session
);
81 throw new SlcException("Cannot initialize JCR result", e
);
85 public void destroy() {
86 if (logoutWhenDestroyed
)
87 JcrUtils
.logoutQuietly(session
);
90 public Node
getNode() {
93 if (nodeIdentifier
!= null) {
94 return session
.getNodeByIdentifier(nodeIdentifier
);
96 QueryManager qm
= session
.getWorkspace().getQueryManager();
97 Query q
= qm
.createQuery("select * from ["
98 + SlcTypes
.SLC_TEST_RESULT
+ "] where [slc:uuid]='"
99 + uuid
+ "'", Query
.JCR_SQL2
);
100 resultNode
= JcrUtils
.querySingleNode(q
);
101 if (resultNode
!= null)
102 nodeIdentifier
= resultNode
.getIdentifier();
105 } catch (Exception e
) {
106 throw new SlcException("Cannot get result node", e
);
110 public void notifyTestRun(TestRun testRun
) {
113 public void addResultPart(TestResultPart testResultPart
) {
114 Node node
= getNode();
116 Node resultPartNode
= node
.addNode(SlcNames
.SLC_STATUS
,
118 resultPartNode
.setProperty(SLC_SUCCESS
,
119 testResultPart
.getStatus() == TestStatus
.PASSED
);
120 if (testResultPart
.getMessage() != null)
121 resultPartNode
.setProperty(SLC_MESSAGE
,
122 testResultPart
.getMessage());
123 if (testResultPart
.getExceptionMessage() != null)
124 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
125 testResultPart
.getExceptionMessage());
126 JcrUtils
.updateLastModified(node
);
127 node
.getSession().save();
128 } catch (Exception e
) {
129 JcrUtils
.discardUnderlyingSessionQuietly(node
);
130 throw new SlcException("Cannot get UUID from " + node
, e
);
135 public String
getUuid() {
136 Node node
= getNode();
138 return node
.getProperty(SLC_UUID
).getString();
139 } catch (Exception e
) {
140 throw new SlcException("Cannot get UUID from " + node
, e
);
144 public void close() {
145 Node node
= getNode();
147 if (node
.hasNode(SLC_COMPLETED
))
149 node
.setProperty(SLC_COMPLETED
, new GregorianCalendar());
150 JcrUtils
.updateLastModified(node
);
151 node
.getSession().save();
152 } catch (Exception e
) {
153 JcrUtils
.discardUnderlyingSessionQuietly(node
);
154 throw new SlcException("Cannot get close date from " + node
, e
);
156 if (logoutWhenDestroyed
)
157 JcrUtils
.logoutQuietly(session
);
160 public Date
getCloseDate() {
161 Node node
= getNode();
163 if (!node
.hasNode(SLC_COMPLETED
))
165 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
166 } catch (Exception e
) {
167 throw new SlcException("Cannot get close date from " + node
, e
);
171 public Map
<String
, String
> getAttributes() {
172 Node node
= getNode();
174 Map
<String
, String
> map
= new HashMap
<String
, String
>();
175 PropertyIterator pit
= node
.getProperties();
176 while (pit
.hasNext()) {
177 Property p
= pit
.nextProperty();
179 map
.put(p
.getName(), p
.getValue().getString());
182 } catch (Exception e
) {
183 throw new SlcException("Cannot get close date from " + node
, e
);
187 public void addAttachment(Attachment attachment
) {
191 public void setUuid(String uuid
) {
195 public void setRepository(Repository repository
) {
196 this.repository
= repository
;
199 public void setResultType(String resultType
) {
200 this.resultType
= resultType
;
203 public void setAttributes(Map
<String
, String
> attributes
) {
205 throw new SlcException(
206 "Attributes cannot be set on an already initialized test result."
207 + " Update the related JCR node directly instead.");
208 this.attributes
= attributes
;
211 // public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
212 // this.logoutWhenDestroyed = logoutWhenDestroyed;