]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java
Deal with destruction callbacks
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / JcrTestResult.java
index 818d4f333bbc7d2f5f11fea705519cf8daa83fa1..d7f0b434d5d74b0c7814f93144573b504d9b86d1 100644 (file)
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.jcr.Credentials;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
@@ -29,6 +30,8 @@ import javax.jcr.Session;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.attachment.Attachment;
@@ -38,29 +41,36 @@ 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_TEST_RESULT}. */
+/**
+ * {@link TestResult} wrapping a JCR node of type
+ * {@link SlcTypes#SLC_TEST_RESULT}.
+ */
 public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
+       private final static Log log = LogFactory.getLog(JcrTestResult.class);
+
        /** Should only be set for an already existing result. */
        private String uuid;
        private Repository repository;
        private Session session;
+       /**
+        * For testing purposes, best practice is to not set them explicitely but
+        * via other mechanisms such as JAAS or SPring Security.
+        */
+       private Credentials credentials = null;
        private String resultType = SlcTypes.SLC_TEST_RESULT;
 
        /** cached for performance purposes */
        private String nodeIdentifier = null;
 
-       private Boolean logoutWhenDestroyed = true;
-
        private Map<String, String> attributes = new HashMap<String, String>();
 
        public void init() {
                try {
-                       session = repository.login();
+                       session = repository.login(credentials);
                        if (uuid == null) {
                                // create new result
                                uuid = UUID.randomUUID().toString();
-                               String path = SlcJcrUtils.createResultPath(session.getUserID(),
-                                               uuid);
+                               String path = SlcJcrUtils.createResultPath(session, uuid);
                                Node resultNode = JcrUtils.mkdirs(session, path, resultType);
                                resultNode.setProperty(SLC_UUID, uuid);
                                for (String attr : attributes.keySet()) {
@@ -73,6 +83,8 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
                                        resultNode.setProperty(property, attributes.get(attr));
                                }
                                session.save();
+                               if (log.isDebugEnabled())
+                                       log.debug("Created test result " + uuid);
                        }
                } catch (Exception e) {
                        JcrUtils.discardQuietly(session);
@@ -81,8 +93,9 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
        }
 
        public void destroy() {
-               if (logoutWhenDestroyed)
-                       JcrUtils.logoutQuietly(session);
+               JcrUtils.logoutQuietly(session);
+               if (log.isTraceEnabled())
+                       log.trace("Logged out session for result " + uuid);
        }
 
        public Node getNode() {
@@ -92,10 +105,9 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
                                return session.getNodeByIdentifier(nodeIdentifier);
                        } else {
                                QueryManager qm = session.getWorkspace().getQueryManager();
-                               Query q = qm.createQuery(
-                                               "select * from [" + SlcTypes.SLC_TEST_RESULT
-                                               + "] where [slc:uuid]='" + uuid
-                                                               + "'", Query.JCR_SQL2);
+                               Query q = qm.createQuery("select * from ["
+                                               + SlcTypes.SLC_TEST_RESULT + "] where [slc:uuid]='"
+                                               + uuid + "'", Query.JCR_SQL2);
                                resultNode = JcrUtils.querySingleNode(q);
                                if (resultNode != null)
                                        nodeIdentifier = resultNode.getIdentifier();
@@ -107,12 +119,17 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
        }
 
        public void notifyTestRun(TestRun testRun) {
+               // TODO store meta data about the test running
+               // if (log.isDebugEnabled())
+               // log.debug("Running test "
+               // + testRun.getTestDefinition().getClass().getName() + "...");
        }
 
        public void addResultPart(TestResultPart testResultPart) {
                Node node = getNode();
                try {
-                       Node resultPartNode = node.addNode(SlcNames.SLC_STATUS, SlcTypes.SLC_CHECK);
+                       Node resultPartNode = node.addNode(SlcNames.SLC_STATUS,
+                                       SlcTypes.SLC_CHECK);
                        resultPartNode.setProperty(SLC_SUCCESS,
                                        testResultPart.getStatus() == TestStatus.PASSED);
                        if (testResultPart.getMessage() != null)
@@ -139,6 +156,7 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
                }
        }
 
+       /** JCR session is NOT logged out */
        public void close() {
                Node node = getNode();
                try {
@@ -151,8 +169,6 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
                        JcrUtils.discardUnderlyingSessionQuietly(node);
                        throw new SlcException("Cannot get close date from " + node, e);
                }
-               if (logoutWhenDestroyed)
-                       JcrUtils.logoutQuietly(session);
        }
 
        public Date getCloseDate() {
@@ -206,8 +222,7 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
                this.attributes = attributes;
        }
 
-//     public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
-//             this.logoutWhenDestroyed = logoutWhenDestroyed;
-//     }
-
+       public void setCredentials(Credentials credentials) {
+               this.credentials = credentials;
+       }
 }