/*
- * 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.
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;
* {@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;
resultNode.setProperty(property, attributes.get(attr));
}
session.save();
+ if (log.isDebugEnabled())
+ log.debug("Created test result " + uuid);
}
} catch (Exception e) {
JcrUtils.discardQuietly(session);
public void destroy() {
JcrUtils.logoutQuietly(session);
+ if (log.isTraceEnabled())
+ log.trace("Logged out session for result " + uuid);
}
public Node getNode() {
}
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,
+ // 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() == TestStatus.PASSED);
+ 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() {
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
-
- // public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
- // this.logoutWhenDestroyed = logoutWhenDestroyed;
- // }
-
}