]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce JCR test result
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 4 Oct 2011 10:17:30 +0000 (10:17 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 4 Oct 2011 10:17:30 +0000 (10:17 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@4792 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java [new file with mode: 0644]
runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrResultListener.java

diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java
new file mode 100644 (file)
index 0000000..74a3167
--- /dev/null
@@ -0,0 +1,161 @@
+package org.argeo.slc.jcr;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
+
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.test.TestResult;
+import org.argeo.slc.test.TestResultPart;
+import org.argeo.slc.test.TestRun;
+import org.argeo.slc.test.TestStatus;
+
+/** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_RESULT}. */
+public class JcrTestResult implements TestResult, SlcNames {
+       /** Should only be set for an already existing result. */
+       private String uuid;
+       private Session session;
+       private String resultType = SlcTypes.SLC_RESULT;
+
+       /** cached for performance purposes */
+       private String nodeIdentifier = null;
+
+       public void init() {
+               try {
+                       if (uuid == null) {
+                               // create new result
+                               uuid = UUID.randomUUID().toString();
+                               String path = SlcJcrUtils.createResultPath(uuid);
+                               Node resultNode = JcrUtils.mkdirs(session, path, resultType);
+                               resultNode.setProperty(SLC_UUID, uuid);
+                               session.save();
+                       }
+               } catch (Exception e) {
+                       JcrUtils.discardQuietly(session);
+                       throw new SlcException("Cannot initialize JCR result", e);
+               }
+       }
+
+       public void destroy() {
+
+       }
+
+       public Node getNode() {
+               try {
+                       Node resultNode;
+                       if (nodeIdentifier != null) {
+                               return session.getNodeByIdentifier(nodeIdentifier);
+                       } else {
+                               QueryManager qm = session.getWorkspace().getQueryManager();
+                               Query q = qm.createQuery(
+                                               "select * from [slc:result] where [slc:uuid]='" + uuid
+                                                               + "'", Query.JCR_SQL2);
+                               resultNode = JcrUtils.querySingleNode(q);
+                               if (resultNode != null)
+                                       nodeIdentifier = resultNode.getIdentifier();
+                       }
+                       return resultNode;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot get result node", e);
+               }
+       }
+
+       public void notifyTestRun(TestRun testRun) {
+       }
+
+       public void addResultPart(TestResultPart testResultPart) {
+               Node node = getNode();
+               try {
+                       // TODO: find a better way to name it by default
+                       String partName = Long.toString(System.currentTimeMillis());
+                       Node resultPartNode = node.addNode(partName, SlcTypes.SLC_CHECK);
+                       resultPartNode.setProperty(SLC_SUCCESS,
+                                       testResultPart.getStatus() == TestStatus.PASSED);
+                       if (testResultPart.getMessage() != null)
+                               resultPartNode.setProperty(SLC_MESSAGE,
+                                               testResultPart.getMessage());
+                       if (testResultPart.getExceptionMessage() != null)
+                               resultPartNode.setProperty(SLC_ERROR_MESSAGE,
+                                               testResultPart.getExceptionMessage());
+                       JcrUtils.updateLastModified(node);
+                       node.getSession().save();
+               } catch (Exception e) {
+                       JcrUtils.discardUnderlyingSessionQuietly(node);
+                       throw new SlcException("Cannot get UUID from " + node, e);
+               }
+
+       }
+
+       public String getUuid() {
+               Node node = getNode();
+               try {
+                       return node.getProperty(SLC_UUID).getString();
+               } catch (Exception e) {
+                       throw new SlcException("Cannot get UUID from " + node, e);
+               }
+       }
+
+       public void close() {
+               Node node = getNode();
+               try {
+                       if (node.hasNode(SLC_COMPLETED))
+                               return;
+                       node.setProperty(SLC_COMPLETED, new GregorianCalendar());
+                       JcrUtils.updateLastModified(node);
+                       node.getSession().save();
+               } catch (Exception e) {
+                       JcrUtils.discardUnderlyingSessionQuietly(node);
+                       throw new SlcException("Cannot get close date from " + node, e);
+               }
+       }
+
+       public Date getCloseDate() {
+               Node node = getNode();
+               try {
+                       if (!node.hasNode(SLC_COMPLETED))
+                               return null;
+                       return node.getProperty(SLC_COMPLETED).getDate().getTime();
+               } catch (Exception e) {
+                       throw new SlcException("Cannot get close date from " + node, e);
+               }
+       }
+
+       public Map<String, String> getAttributes() {
+               Node node = getNode();
+               try {
+                       Map<String, String> map = new HashMap<String, String>();
+                       PropertyIterator pit = node.getProperties();
+                       while (pit.hasNext()) {
+                               Property p = pit.nextProperty();
+                               if (!p.isMultiple())
+                                       map.put(p.getName(), p.getValue().getString());
+                       }
+                       return map;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot get close date from " + node, e);
+               }
+       }
+
+       public void setUuid(String uuid) {
+               this.uuid = uuid;
+       }
+
+       public void setSession(Session session) {
+               this.session = session;
+       }
+
+       public void setResultType(String resultType) {
+               this.resultType = resultType;
+       }
+
+}
index 0eb900e19816cf93e34cbf1ce4b2c3c5ebdb3870..79e6b12a005448493b634509a57f8b91ac15ce5a 100644 (file)
@@ -31,7 +31,11 @@ import org.argeo.slc.jcr.SlcTypes;
 import org.argeo.slc.test.TestResultPart;
 import org.argeo.slc.test.TestStatus;
 
-/** Persists results in JCR */
+/**
+ * Persists results in JCR by listening to {@link TreeTestResult}. This is to
+ * facilitate transition from legacy approaches and should not be used in new
+ * implementations.
+ */
 public class JcrResultListener implements TreeTestResultListener, SlcNames {
        private final static Log log = LogFactory.getLog(JcrResultListener.class);