]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.runtime/src/org/argeo/slc/runtime/test/ContextUtils.java
Continue to remove dependencies with Spring.
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / test / ContextUtils.java
diff --git a/org.argeo.slc.runtime/src/org/argeo/slc/runtime/test/ContextUtils.java b/org.argeo.slc.runtime/src/org/argeo/slc/runtime/test/ContextUtils.java
new file mode 100644 (file)
index 0000000..663e1ed
--- /dev/null
@@ -0,0 +1,139 @@
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.slc.runtime.test;\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
+import org.argeo.slc.test.TestResult;\r
+import org.argeo.slc.test.TestStatus;\r
+import org.argeo.slc.test.context.ContextAware;\r
+import org.argeo.slc.test.context.ParentContextAware;\r
+\r
+/** Utilities for comparing and synchronising contexts. */\r
+public class ContextUtils {\r
+       private final static Log log = LogFactory.getLog(ContextUtils.class);\r
+\r
+       public static void compareReachedExpected(ContextAware contextAware,\r
+                       TestResult testResult) {\r
+               for (String key : contextAware.getExpectedValues().keySet()) {\r
+\r
+                       // Compare expected values with reached ones\r
+                       Object expectedValue = contextAware.getExpectedValues().get(key);\r
+\r
+                       if (expectedValue.toString().equals(\r
+                                       contextAware.getContextSkipFlag())) {\r
+                               if (log.isDebugEnabled())\r
+                                       log.debug("Skipped check for key '" + key + "'");\r
+                               continue;\r
+                       }\r
+\r
+                       if (contextAware.getValues().containsKey(key)) {\r
+                               Object reachedValue = contextAware.getValues().get(key);\r
+\r
+                               if (expectedValue.equals(contextAware.getContextAnyFlag())) {\r
+                                       testResult.addResultPart(new SimpleResultPart(\r
+                                                       TestStatus.PASSED, "Expected any value for key '"\r
+                                                                       + key + "'"));\r
+                               } else if (expectedValue.equals(reachedValue)) {\r
+                                       testResult.addResultPart(new SimpleResultPart(\r
+                                                       TestStatus.PASSED, "Values matched for key '" + key\r
+                                                                       + "'"));\r
+                               } else {\r
+                                       testResult.addResultPart(new SimpleResultPart(\r
+                                                       TestStatus.FAILED, "Mismatch for key '" + key\r
+                                                                       + "': expected '" + expectedValue\r
+                                                                       + "' but reached '" + reachedValue + "'"));\r
+                               }\r
+                       } else {\r
+                               testResult.addResultPart(new SimpleResultPart(\r
+                                               TestStatus.FAILED, "No value reached for key '" + key\r
+                                                               + "'"));\r
+                       }\r
+               }\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("Synchronized 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,\r
+                                       child.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
+       public 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
+       public 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
+       }\r
+}\r