]> 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 test result attributes instead of root tags
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / context / ContextUtils.java
index 4f9d6ea9c068976a1a1bd9624dfbbb379b8e963a..3826a8710aeb63b7418c5b04c3283f692c961ddd 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
@@ -19,32 +22,27 @@ public class ContextUtils {
                        TestResult testResult, TreeSRelated treeSRelated) {\r
                for (String key : contextAware.getExpectedValues().keySet()) {\r
 \r
-                       // Register in structure\r
-                       if (treeSRelated != null) {\r
-                               if (treeSRelated.getBasePath() != null) {\r
-                                       TreeSPath path = treeSRelated.getBasePath()\r
-                                                       .createChild(key);\r
-                                       StructureRegistry<TreeSPath> registry = treeSRelated\r
-                                                       .getRegistry();\r
-                                       final StructureElement element = treeSRelated\r
-                                                       .getStructureElement(key);\r
-                                       registry.register(path, element);\r
-                                       if (testResult instanceof StructureAware)\r
-                                               ((StructureAware<TreeSPath>) testResult)\r
-                                                               .notifyCurrentPath(registry, path);\r
-\r
-                                       if (log.isDebugEnabled())\r
-                                               log.debug("Checking key " + key + " for path " + path);\r
-                               }\r
-                       }\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
+                       // Register in structure\r
+                       registerInStructure(testResult, treeSRelated, key);\r
+\r
                        if (contextAware.getValues().containsKey(key)) {\r
                                Object reachedValue = contextAware.getValues().get(key);\r
 \r
-                               if (expectedValue.equals(reachedValue)) {\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
@@ -59,19 +57,107 @@ public class ContextUtils {
                                                TestStatus.FAILED, "No value reached for key '" + key\r
                                                                + "'"));\r
                        }\r
+                       resetStructure(testResult, treeSRelated);\r
+               }\r
+       }\r
 \r
-                       if (treeSRelated != null) {\r
-                               if (treeSRelated.getBasePath() != null) {\r
-                                       if (testResult instanceof StructureAware) {\r
-                                               ((StructureAware<TreeSPath>) testResult)\r
-                                                               .notifyCurrentPath(treeSRelated.getRegistry(),\r
-                                                                               treeSRelated.getBasePath());\r
-                                       }\r
+       private static void registerInStructure(TestResult testResult,\r
+                       TreeSRelated treeSRelated, String key) {\r
+               if (treeSRelated != null) {\r
+                       if (treeSRelated.getBasePath() != null) {\r
+                               TreeSPath path = treeSRelated.getBasePath().createChild(key);\r
+                               StructureRegistry<TreeSPath> registry = treeSRelated\r
+                                               .getRegistry();\r
+                               final StructureElement element = treeSRelated\r
+                                               .getStructureElement(key);\r
+                               registry.register(path, element);\r
+                               if (testResult instanceof StructureAware)\r
+                                       ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(\r
+                                                       registry, path);\r
+\r
+                               if (log.isDebugEnabled())\r
+                                       log.debug("Checking key " + key + " for path " + path);\r
+                       }\r
+               }\r
+       }\r
+\r
+       private static void resetStructure(TestResult testResult,\r
+                       TreeSRelated treeSRelated) {\r
+               if (treeSRelated != null) {\r
+                       if (treeSRelated.getBasePath() != null) {\r
+                               if (testResult instanceof StructureAware) {\r
+                                       ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(\r
+                                                       treeSRelated.getRegistry(), treeSRelated\r
+                                                                       .getBasePath());\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("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
+       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