]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java
Introduce parent context
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / context / ContextUtils.java
index 4f9d6ea9c068976a1a1bd9624dfbbb379b8e963a..f157ac78c17eacf2bfca95aebbc94c035d9ca7e9 100644 (file)
@@ -1,5 +1,8 @@
 package org.argeo.slc.core.test.context;\r
 \r
+import java.util.Map;\r
+import java.util.TreeMap;\r
+\r
 import org.apache.commons.logging.Log;\r
 import org.apache.commons.logging.LogFactory;\r
 \r
@@ -72,6 +75,70 @@ public class ContextUtils {
                }\r
        }\r
 \r
+       /**\r
+        * Makes sure that all children and sub-children of parent share the same\r
+        * maps for values and expected values.\r
+        */\r
+       public static void synchronize(ParentContextAware parent) {\r
+               Map<String, Object> expectedValuesCommon = new TreeMap<String, Object>(\r
+                               parent.getExpectedValues());\r
+               synchronize(parent, expectedValuesCommon);\r
+               if (log.isDebugEnabled())\r
+                       log.debug("Synchonized context " + parent);\r
+\r
+       }\r
+\r
+       private static void synchronize(ParentContextAware parent,\r
+                       Map<String, Object> expectedValuesCommon) {\r
+               for (ContextAware child : parent.getChildContexts()) {\r
+                       // Values\r
+                       putNotContained(parent.getValues(), child.getValues());\r
+                       child.setValues(parent.getValues());\r
+\r
+                       // Expected Values\r
+                       // Expected values reference is not overridden: each child has its\r
+                       // own expected values map.\r
+                       overrideContained(expectedValuesCommon, child.getExpectedValues());\r
+\r
+                       // Creates a new Map in order not to disturb other context using the\r
+                       // same keys\r
+                       Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(\r
+                                       expectedValuesCommon);\r
+                       putNotContained(expectedValuesCommonChild, child\r
+                                       .getExpectedValues());\r
+\r
+                       if (child instanceof ParentContextAware) {\r
+                               // Recursive sync\r
+                               synchronize((ParentContextAware) child,\r
+                                               expectedValuesCommonChild);\r
+                       }\r
+               }\r
+\r
+       }\r
+\r
+       /**\r
+        * Put into common map the values from child map which are not already\r
+        * defined in common map.\r
+        */\r
+       private static void putNotContained(Map<String, Object> commonMap,\r
+                       Map<String, Object> childMap) {\r
+               for (String key : childMap.keySet()) {\r
+                       if (!commonMap.containsKey(key)) {\r
+                               commonMap.put(key, childMap.get(key));\r
+                       }\r
+               }\r
+       }\r
+\r
+       /** Overrides child map values with the values already set in common map */\r
+       private static void overrideContained(Map<String, Object> commonMap,\r
+                       Map<String, Object> childMap) {\r
+               for (String key : childMap.keySet()) {\r
+                       if (commonMap.containsKey(key)) {\r
+                               childMap.put(key, commonMap.get(key));\r
+                       }\r
+               }\r
+       }\r
+\r
        /** Makes sure this cannot be instantiated. */\r
        private ContextUtils() {\r
 \r