1 package org
.argeo
.slc
.jcr
;
4 import java
.util
.GregorianCalendar
;
5 import java
.util
.HashMap
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.PropertyIterator
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.query
.Query
;
14 import javax
.jcr
.query
.QueryManager
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.slc
.SlcException
;
18 import org
.argeo
.slc
.core
.attachment
.Attachment
;
19 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
20 import org
.argeo
.slc
.test
.TestResult
;
21 import org
.argeo
.slc
.test
.TestResultPart
;
22 import org
.argeo
.slc
.test
.TestRun
;
23 import org
.argeo
.slc
.test
.TestStatus
;
25 /** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_RESULT}. */
26 public class JcrTestResult
implements TestResult
, SlcNames
, AttachmentsEnabled
{
27 /** Should only be set for an already existing result. */
29 private Session session
;
30 private String resultType
= SlcTypes
.SLC_RESULT
;
32 /** cached for performance purposes */
33 private String nodeIdentifier
= null;
35 private Boolean logoutWhenDestroyed
= true;
37 private Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
43 uuid
= UUID
.randomUUID().toString();
44 String path
= SlcJcrUtils
.createResultPath(uuid
);
45 Node resultNode
= JcrUtils
.mkdirs(session
, path
, resultType
);
46 resultNode
.setProperty(SLC_UUID
, uuid
);
47 for (String attr
: attributes
.keySet()) {
48 String property
= attr
;
49 // compatibility with legacy applications
50 if ("testCase".equals(attr
))
51 property
= SLC_TEST_CASE
;
52 else if ("testCaseType".equals(attr
))
53 property
= SLC_TEST_CASE_TYPE
;
54 resultNode
.setProperty(property
, attributes
.get(attr
));
58 } catch (Exception e
) {
59 JcrUtils
.discardQuietly(session
);
60 throw new SlcException("Cannot initialize JCR result", e
);
64 public void destroy() {
65 if (logoutWhenDestroyed
)
66 JcrUtils
.logoutQuietly(session
);
69 public Node
getNode() {
72 if (nodeIdentifier
!= null) {
73 return session
.getNodeByIdentifier(nodeIdentifier
);
75 QueryManager qm
= session
.getWorkspace().getQueryManager();
76 Query q
= qm
.createQuery(
77 "select * from [slc:result] where [slc:uuid]='" + uuid
78 + "'", Query
.JCR_SQL2
);
79 resultNode
= JcrUtils
.querySingleNode(q
);
80 if (resultNode
!= null)
81 nodeIdentifier
= resultNode
.getIdentifier();
84 } catch (Exception e
) {
85 throw new SlcException("Cannot get result node", e
);
89 public void notifyTestRun(TestRun testRun
) {
92 public void addResultPart(TestResultPart testResultPart
) {
93 Node node
= getNode();
95 // TODO: find a better way to name it by default
96 String partName
= Long
.toString(System
.currentTimeMillis());
97 Node resultPartNode
= node
.addNode(partName
, SlcTypes
.SLC_CHECK
);
98 resultPartNode
.setProperty(SLC_SUCCESS
,
99 testResultPart
.getStatus() == TestStatus
.PASSED
);
100 if (testResultPart
.getMessage() != null)
101 resultPartNode
.setProperty(SLC_MESSAGE
,
102 testResultPart
.getMessage());
103 if (testResultPart
.getExceptionMessage() != null)
104 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
105 testResultPart
.getExceptionMessage());
106 JcrUtils
.updateLastModified(node
);
107 node
.getSession().save();
108 } catch (Exception e
) {
109 JcrUtils
.discardUnderlyingSessionQuietly(node
);
110 throw new SlcException("Cannot get UUID from " + node
, e
);
115 public String
getUuid() {
116 Node node
= getNode();
118 return node
.getProperty(SLC_UUID
).getString();
119 } catch (Exception e
) {
120 throw new SlcException("Cannot get UUID from " + node
, e
);
124 public void close() {
125 Node node
= getNode();
127 if (node
.hasNode(SLC_COMPLETED
))
129 node
.setProperty(SLC_COMPLETED
, new GregorianCalendar());
130 JcrUtils
.updateLastModified(node
);
131 node
.getSession().save();
132 } catch (Exception e
) {
133 JcrUtils
.discardUnderlyingSessionQuietly(node
);
134 throw new SlcException("Cannot get close date from " + node
, e
);
138 public Date
getCloseDate() {
139 Node node
= getNode();
141 if (!node
.hasNode(SLC_COMPLETED
))
143 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
144 } catch (Exception e
) {
145 throw new SlcException("Cannot get close date from " + node
, e
);
149 public Map
<String
, String
> getAttributes() {
150 Node node
= getNode();
152 Map
<String
, String
> map
= new HashMap
<String
, String
>();
153 PropertyIterator pit
= node
.getProperties();
154 while (pit
.hasNext()) {
155 Property p
= pit
.nextProperty();
157 map
.put(p
.getName(), p
.getValue().getString());
160 } catch (Exception e
) {
161 throw new SlcException("Cannot get close date from " + node
, e
);
165 public void addAttachment(Attachment attachment
) {
169 public void setUuid(String uuid
) {
173 public void setSession(Session session
) {
174 this.session
= session
;
177 public void setResultType(String resultType
) {
178 this.resultType
= resultType
;
181 public void setAttributes(Map
<String
, String
> attributes
) {
183 throw new SlcException(
184 "Attributes cannot be set on an already initialized test result."
185 + " Update the related JCR node directly instead.");
186 this.attributes
= attributes
;
189 public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed
) {
190 this.logoutWhenDestroyed
= logoutWhenDestroyed
;