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
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
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