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=f157ac78c17eacf2bfca95aebbc94c035d9ca7e9;hb=7717aa5e3983a7f91e071671a2ef0b8a904e5913;hp=4f9d6ea9c068976a1a1bd9624dfbbb379b8e963a;hpb=3c79a525e511524f904078a4a5092a16b19d2e66;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..f157ac78c 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; @@ -72,6 +75,70 @@ public class ContextUtils { } } + /** + * 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. + */ + private 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 */ + private 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() {