X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Ftest%2Ftree%2FTreeTestResult.java;h=3e365afe0822c6f28b51f3f835d89189a6fd7328;hb=c4e9c29266eb8656d0573a3aff8b6b2a713861d7;hp=8da3e7d5a2dde3bd932a5175b592cbac6d3c77a8;hpb=d5d72139e7497923c2b8e2f4d25f366d01425498;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java index 8da3e7d5a..3e365afe0 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java @@ -2,17 +2,14 @@ package org.argeo.slc.core.test.tree; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import java.util.Vector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.argeo.slc.core.SlcException; -import org.argeo.slc.core.process.SlcExecution; -import org.argeo.slc.core.process.SlcExecutionAware; -import org.argeo.slc.core.process.SlcExecutionStep; import org.argeo.slc.core.structure.StructureAware; import org.argeo.slc.core.structure.StructureElement; import org.argeo.slc.core.structure.StructureRegistry; @@ -20,51 +17,60 @@ import org.argeo.slc.core.structure.tree.TreeSPath; import org.argeo.slc.core.test.TestResult; import org.argeo.slc.core.test.TestResultListener; import org.argeo.slc.core.test.TestResultPart; +import org.argeo.slc.core.test.TestRun; +import org.argeo.slc.core.test.TestRunAware; /** * Complex implementation of a test result compatible with a tree based * structure. */ public class TreeTestResult implements TestResult, StructureAware, - SlcExecutionAware { + Comparable { private Log log = LogFactory.getLog(TreeTestResult.class); - private List listeners = new Vector(); + private List> listeners = new Vector>(); private TreeSPath currentPath; - private String currentSlcExecutionUuid; - private String currentSlcExecutionStepUuid; + private TestRun currentTestRun; private Date closeDate; - private boolean isClosed = false; + private Boolean isClosed = false; + + private Boolean warnIfAlreadyClosed = true; private String uuid; private SortedMap resultParts = new TreeMap(); private SortedMap elements = new TreeMap(); + private Map attributes = new TreeMap(); + /** Sets the list of listeners. */ - public void setListeners(List listeners) { + public void setListeners(List> listeners) { this.listeners = listeners; } public void addResultPart(TestResultPart part) { - if (currentPath == null) { + if (isClosed) + throw new SlcException("Cannot result parts to a closed result"); + + if (currentPath == null) throw new SlcException("No current path set."); - } + PartSubList subList = resultParts.get(currentPath); if (subList == null) { subList = new PartSubList(); - subList.setSlcExecutionUuid(currentSlcExecutionUuid); - subList.setSlcExecutionStepUuid(currentSlcExecutionStepUuid); resultParts.put(currentPath, subList); } + if (part instanceof TestRunAware && currentTestRun != null) { + ((TestRunAware) part).notifyTestRun(currentTestRun); + } subList.getParts().add(part); // notify listeners synchronized (listeners) { - for (TestResultListener listener : listeners) { + for (TestResultListener listener : listeners) { listener.resultPartAdded(this, part); } } @@ -88,7 +94,7 @@ public class TreeTestResult implements TestResult, StructureAware, } } - currentPath = (TreeSPath) path; + currentPath = path; } /** Gets the current path. */ @@ -107,21 +113,32 @@ public class TreeTestResult implements TestResult, StructureAware, } public void close() { + if (resultParts.size() == 0) { + if (log.isTraceEnabled()) + log.trace("Test Result #" + getUuid() + + " contains no results, no need to close it."); + return; + } + if (isClosed) { - throw new SlcException("Test Result #" + getUuid() - + " alredy closed."); + if (warnIfAlreadyClosed) + log.warn("Test Result #" + getUuid() + + " already closed. Doing nothing."); + return; } + closeDate = new Date(); synchronized (listeners) { - for (TestResultListener listener : listeners) { + for (TestResultListener listener : listeners) { listener.close(this); } listeners.clear(); } isClosed = true; - log.info("Test Result #" + getUuid() + " closed."); + if (log.isTraceEnabled()) + log.trace("Test Result " + getUuid() + " closed."); } public Date getCloseDate() { @@ -133,12 +150,8 @@ public class TreeTestResult implements TestResult, StructureAware, this.closeDate = closeDate; } - public void notifySlcExecution(SlcExecution slcExecution) { - currentSlcExecutionUuid = slcExecution.getUuid(); - SlcExecutionStep step = slcExecution.currentStep(); - if (step != null) { - currentSlcExecutionStepUuid = step.getUuid(); - } + public void notifyTestRun(TestRun testRun) { + currentTestRun = testRun; } public SortedMap getElements() { @@ -157,4 +170,50 @@ public class TreeTestResult implements TestResult, StructureAware, this.uuid = uuid; } + public SortedMap getRelatedElements( + TreeSPath path) { + if (path == null) + throw new SlcException( + "Cannot retrieve element for a null path in result #" + + uuid); + + SortedMap relatedElements = new TreeMap(); + List hierarchy = path.getHierarchyAsList(); + for (TreeSPath currPath : elements.keySet()) { + if (hierarchy.contains(currPath)) { + relatedElements.put(currPath, elements.get(currPath)); + } + } + return relatedElements; + } + + public TestRun getCurrentTestRun() { + return currentTestRun; + } + + public int compareTo(TreeTestResult ttr2) { + TreeTestResult ttr1 = this; + if (ttr1.getCloseDate() != null && ttr2.getCloseDate() != null) { + return -ttr1.getCloseDate().compareTo(ttr2.getCloseDate()); + } else if (ttr1.getCloseDate() != null && ttr2.getCloseDate() == null) { + return 1; + } else if (ttr1.getCloseDate() == null && ttr2.getCloseDate() != null) { + return -1; + } else { + return ttr1.getUuid().compareTo(ttr2.getUuid()); + } + } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } + + public void setWarnIfAlreadyClosed(Boolean warnIfAlreadyClosed) { + this.warnIfAlreadyClosed = warnIfAlreadyClosed; + } + }