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
.test
.TestResult
;
19 import org
.argeo
.slc
.test
.TestResultPart
;
20 import org
.argeo
.slc
.test
.TestRun
;
21 import org
.argeo
.slc
.test
.TestStatus
;
23 /** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_RESULT}. */
24 public class JcrTestResult
implements TestResult
, SlcNames
{
25 /** Should only be set for an already existing result. */
27 private Session session
;
28 private String resultType
= SlcTypes
.SLC_RESULT
;
30 /** cached for performance purposes */
31 private String nodeIdentifier
= null;
37 uuid
= UUID
.randomUUID().toString();
38 String path
= SlcJcrUtils
.createResultPath(uuid
);
39 Node resultNode
= JcrUtils
.mkdirs(session
, path
, resultType
);
40 resultNode
.setProperty(SLC_UUID
, uuid
);
43 } catch (Exception e
) {
44 JcrUtils
.discardQuietly(session
);
45 throw new SlcException("Cannot initialize JCR result", e
);
49 public void destroy() {
53 public Node
getNode() {
56 if (nodeIdentifier
!= null) {
57 return session
.getNodeByIdentifier(nodeIdentifier
);
59 QueryManager qm
= session
.getWorkspace().getQueryManager();
60 Query q
= qm
.createQuery(
61 "select * from [slc:result] where [slc:uuid]='" + uuid
62 + "'", Query
.JCR_SQL2
);
63 resultNode
= JcrUtils
.querySingleNode(q
);
64 if (resultNode
!= null)
65 nodeIdentifier
= resultNode
.getIdentifier();
68 } catch (Exception e
) {
69 throw new SlcException("Cannot get result node", e
);
73 public void notifyTestRun(TestRun testRun
) {
76 public void addResultPart(TestResultPart testResultPart
) {
77 Node node
= getNode();
79 // TODO: find a better way to name it by default
80 String partName
= Long
.toString(System
.currentTimeMillis());
81 Node resultPartNode
= node
.addNode(partName
, SlcTypes
.SLC_CHECK
);
82 resultPartNode
.setProperty(SLC_SUCCESS
,
83 testResultPart
.getStatus() == TestStatus
.PASSED
);
84 if (testResultPart
.getMessage() != null)
85 resultPartNode
.setProperty(SLC_MESSAGE
,
86 testResultPart
.getMessage());
87 if (testResultPart
.getExceptionMessage() != null)
88 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
89 testResultPart
.getExceptionMessage());
90 JcrUtils
.updateLastModified(node
);
91 node
.getSession().save();
92 } catch (Exception e
) {
93 JcrUtils
.discardUnderlyingSessionQuietly(node
);
94 throw new SlcException("Cannot get UUID from " + node
, e
);
99 public String
getUuid() {
100 Node node
= getNode();
102 return node
.getProperty(SLC_UUID
).getString();
103 } catch (Exception e
) {
104 throw new SlcException("Cannot get UUID from " + node
, e
);
108 public void close() {
109 Node node
= getNode();
111 if (node
.hasNode(SLC_COMPLETED
))
113 node
.setProperty(SLC_COMPLETED
, new GregorianCalendar());
114 JcrUtils
.updateLastModified(node
);
115 node
.getSession().save();
116 } catch (Exception e
) {
117 JcrUtils
.discardUnderlyingSessionQuietly(node
);
118 throw new SlcException("Cannot get close date from " + node
, e
);
122 public Date
getCloseDate() {
123 Node node
= getNode();
125 if (!node
.hasNode(SLC_COMPLETED
))
127 return node
.getProperty(SLC_COMPLETED
).getDate().getTime();
128 } catch (Exception e
) {
129 throw new SlcException("Cannot get close date from " + node
, e
);
133 public Map
<String
, String
> getAttributes() {
134 Node node
= getNode();
136 Map
<String
, String
> map
= new HashMap
<String
, String
>();
137 PropertyIterator pit
= node
.getProperties();
138 while (pit
.hasNext()) {
139 Property p
= pit
.nextProperty();
141 map
.put(p
.getName(), p
.getValue().getString());
144 } catch (Exception e
) {
145 throw new SlcException("Cannot get close date from " + node
, e
);
149 public void setUuid(String uuid
) {
153 public void setSession(Session session
) {
154 this.session
= session
;
157 public void setResultType(String resultType
) {
158 this.resultType
= resultType
;