X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjcr%2FJcrTestResult.java;h=e3394e05cbbda6ecbcd8c11800487512d5b86e2b;hb=805e3301575e708491482a2a00dd37bc2b41b851;hp=f96e88ea7fc090fadfb5488698db9d6d57e5ccf9;hpb=4444b88528fd1ba4de62d47bf885bbe9403d2216;p=gpl%2Fargeo-slc.git 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 index f96e88ea7..e3394e05c 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2012 Mathieu Baudier + * Copyright (C) 2007-2012 Argeo GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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_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; - private String resultType = SlcTypes.SLC_RESULT; + /** + * 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 attributes = new HashMap(); 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,9 +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 [slc: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(); @@ -106,29 +119,93 @@ 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 { - // 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); + // error : revert all unsaved changes on the resultNode to be sure + // it is in a consistant state + if (testResultPart.getExceptionMessage() != null) + JcrUtils.discardQuietly(node.getSession()); + node.getSession().save(); + + // add the new result part, retrieving status information + Node resultPartNode = node.addNode(SlcNames.SLC_RESULT_PART, + SlcTypes.SLC_CHECK); + resultPartNode.setProperty(SLC_SUCCESS, testResultPart.getStatus() + .equals(TestStatus.PASSED)); if (testResultPart.getMessage() != null) resultPartNode.setProperty(SLC_MESSAGE, testResultPart.getMessage()); - if (testResultPart.getExceptionMessage() != null) + if (testResultPart.getStatus().equals(TestStatus.ERROR)) { resultPartNode.setProperty(SLC_ERROR_MESSAGE, - testResultPart.getExceptionMessage()); + (testResultPart.getExceptionMessage() == null) ? "" + : testResultPart.getExceptionMessage()); + } + + // helper update aggregate status node + Node mainStatus; + if (!node.hasNode(SLC_AGGREGATED_STATUS)) { + + mainStatus = node.addNode(SLC_AGGREGATED_STATUS, + SlcTypes.SLC_CHECK); + mainStatus.setProperty(SLC_SUCCESS, + resultPartNode.getProperty(SLC_SUCCESS).getBoolean()); + if (resultPartNode.hasProperty(SLC_MESSAGE)) + mainStatus.setProperty(SLC_MESSAGE, resultPartNode + .getProperty(SLC_MESSAGE).getString()); + if (resultPartNode.hasProperty(SLC_ERROR_MESSAGE)) + mainStatus.setProperty(SLC_ERROR_MESSAGE, resultPartNode + .getProperty(SLC_ERROR_MESSAGE).getString()); + } else { + mainStatus = node.getNode(SLC_AGGREGATED_STATUS); + if (mainStatus.hasProperty(SLC_ERROR_MESSAGE)) { + // main status already in error we do nothing + } else if (resultPartNode.hasProperty(SLC_ERROR_MESSAGE)) { + // main status was not in error and new result part is in + // error; we update main status + mainStatus.setProperty(SLC_SUCCESS, false); + mainStatus.setProperty(SLC_ERROR_MESSAGE, resultPartNode + .getProperty(SLC_ERROR_MESSAGE).getString()); + if (resultPartNode.hasProperty(SLC_MESSAGE)) + mainStatus.setProperty(SLC_MESSAGE, resultPartNode + .getProperty(SLC_MESSAGE).getString()); + else + // remove old message to remain consistent + mainStatus.setProperty(SLC_MESSAGE, ""); + } else if (!mainStatus.getProperty(SLC_SUCCESS).getBoolean()) { + // main status was already failed and new result part is not + // in error, we do nothing + } else if (!resultPartNode.getProperty(SLC_SUCCESS) + .getBoolean()) { + // new resultPart that is failed + mainStatus.setProperty(SLC_SUCCESS, false); + if (resultPartNode.hasProperty(SLC_MESSAGE)) + mainStatus.setProperty(SLC_MESSAGE, resultPartNode + .getProperty(SLC_MESSAGE).getString()); + else + // remove old message to remain consistent + mainStatus.setProperty(SLC_MESSAGE, ""); + } else if (resultPartNode.hasProperty(SLC_MESSAGE) + && (!mainStatus.hasProperty(SLC_MESSAGE) || ("" + .equals(mainStatus.getProperty(SLC_MESSAGE) + .getString().trim())))) { + mainStatus.setProperty(SLC_MESSAGE, resultPartNode + .getProperty(SLC_MESSAGE).getString()); + } + } JcrUtils.updateLastModified(node); node.getSession().save(); } catch (Exception e) { JcrUtils.discardUnderlyingSessionQuietly(node); - throw new SlcException("Cannot get UUID from " + node, e); + throw new SlcException("Cannot add ResultPart to node " + node, e); } - } public String getUuid() { @@ -140,6 +217,7 @@ public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled { } } + /** JCR session is NOT logged out */ public void close() { Node node = getNode(); try { @@ -152,8 +230,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() { @@ -207,8 +283,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; + } }