X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Ftest%2Fcontext%2FContextUtils.java;h=3826a8710aeb63b7418c5b04c3283f692c961ddd;hb=1a4b784250a43f6c7421ab5d8567c3bfbf51e990;hp=4f9d6ea9c068976a1a1bd9624dfbbb379b8e963a;hpb=bcb5e50efd7e513288a40554c8223322bf50b07d;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java index 4f9d6ea9c..3826a8710 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java @@ -1,5 +1,8 @@ package org.argeo.slc.core.test.context; +import java.util.Map; +import java.util.TreeMap; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,32 +22,27 @@ public class ContextUtils { TestResult testResult, TreeSRelated treeSRelated) { for (String key : contextAware.getExpectedValues().keySet()) { - // Register in structure - if (treeSRelated != null) { - if (treeSRelated.getBasePath() != null) { - TreeSPath path = treeSRelated.getBasePath() - .createChild(key); - StructureRegistry registry = treeSRelated - .getRegistry(); - final StructureElement element = treeSRelated - .getStructureElement(key); - registry.register(path, element); - if (testResult instanceof StructureAware) - ((StructureAware) testResult) - .notifyCurrentPath(registry, path); - - if (log.isDebugEnabled()) - log.debug("Checking key " + key + " for path " + path); - } - } - // Compare expected values with reached ones Object expectedValue = contextAware.getExpectedValues().get(key); + if (expectedValue.toString().equals( + contextAware.getContextSkipFlag())) { + if (log.isDebugEnabled()) + log.debug("Skipped check for key '" + key + "'"); + continue; + } + + // Register in structure + registerInStructure(testResult, treeSRelated, key); + if (contextAware.getValues().containsKey(key)) { Object reachedValue = contextAware.getValues().get(key); - if (expectedValue.equals(reachedValue)) { + if (expectedValue.equals(contextAware.getContextAnyFlag())) { + testResult.addResultPart(new SimpleResultPart( + TestStatus.PASSED, "Expected any value for key '" + + key + "'")); + } else if (expectedValue.equals(reachedValue)) { testResult.addResultPart(new SimpleResultPart( TestStatus.PASSED, "Values matched for key '" + key + "'")); @@ -59,19 +57,107 @@ public class ContextUtils { TestStatus.FAILED, "No value reached for key '" + key + "'")); } + resetStructure(testResult, treeSRelated); + } + } - if (treeSRelated != null) { - if (treeSRelated.getBasePath() != null) { - if (testResult instanceof StructureAware) { - ((StructureAware) testResult) - .notifyCurrentPath(treeSRelated.getRegistry(), - treeSRelated.getBasePath()); - } + private static void registerInStructure(TestResult testResult, + TreeSRelated treeSRelated, String key) { + if (treeSRelated != null) { + if (treeSRelated.getBasePath() != null) { + TreeSPath path = treeSRelated.getBasePath().createChild(key); + StructureRegistry registry = treeSRelated + .getRegistry(); + final StructureElement element = treeSRelated + .getStructureElement(key); + registry.register(path, element); + if (testResult instanceof StructureAware) + ((StructureAware) testResult).notifyCurrentPath( + registry, path); + + if (log.isDebugEnabled()) + log.debug("Checking key " + key + " for path " + path); + } + } + } + + private static void resetStructure(TestResult testResult, + TreeSRelated treeSRelated) { + if (treeSRelated != null) { + if (treeSRelated.getBasePath() != null) { + if (testResult instanceof StructureAware) { + ((StructureAware) testResult).notifyCurrentPath( + treeSRelated.getRegistry(), treeSRelated + .getBasePath()); } } } } + /** + * Makes sure that all children and sub-children of parent share the same + * maps for values and expected values. + */ + public static void synchronize(ParentContextAware parent) { + Map expectedValuesCommon = new TreeMap( + parent.getExpectedValues()); + synchronize(parent, expectedValuesCommon); + if (log.isDebugEnabled()) + log.debug("Synchonized context " + parent); + + } + + private static void synchronize(ParentContextAware parent, + Map expectedValuesCommon) { + for (ContextAware child : parent.getChildContexts()) { + // Values + putNotContained(parent.getValues(), child.getValues()); + child.setValues(parent.getValues()); + + // Expected Values + // Expected values reference is not overridden: each child has its + // own expected values map. + overrideContained(expectedValuesCommon, child.getExpectedValues()); + + // Creates a new Map in order not to disturb other context using the + // same keys + Map expectedValuesCommonChild = new TreeMap( + expectedValuesCommon); + putNotContained(expectedValuesCommonChild, child + .getExpectedValues()); + + if (child instanceof ParentContextAware) { + // Recursive sync + synchronize((ParentContextAware) child, + expectedValuesCommonChild); + } + } + + } + + /** + * Put into common map the values from child map which are not already + * defined in common map. + */ + public static void putNotContained(Map commonMap, + Map childMap) { + for (String key : childMap.keySet()) { + if (!commonMap.containsKey(key)) { + commonMap.put(key, childMap.get(key)); + } + } + } + + /** Overrides child map values with the values already set in common map */ + public static void overrideContained(Map commonMap, + Map childMap) { + for (String key : childMap.keySet()) { + if (commonMap.containsKey(key)) { + childMap.put(key, commonMap.get(key)); + } + } + } + /** Makes sure this cannot be instantiated. */ private ContextUtils() {