]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java
Various changes
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / context / ContextUtils.java
1 package org.argeo.slc.core.test.context;
2
3 import java.util.Map;
4 import java.util.TreeMap;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import org.argeo.slc.core.structure.StructureAware;
10 import org.argeo.slc.core.structure.StructureElement;
11 import org.argeo.slc.core.structure.StructureRegistry;
12 import org.argeo.slc.core.structure.tree.TreeSPath;
13 import org.argeo.slc.core.structure.tree.TreeSRelated;
14 import org.argeo.slc.core.test.SimpleResultPart;
15 import org.argeo.slc.core.test.TestResult;
16 import org.argeo.slc.core.test.TestStatus;
17
18 public class ContextUtils {
19 private final static Log log = LogFactory.getLog(ContextUtils.class);
20
21 public static void compareReachedExpected(ContextAware contextAware,
22 TestResult testResult, TreeSRelated treeSRelated) {
23 for (String key : contextAware.getExpectedValues().keySet()) {
24
25 // Compare expected values with reached ones
26 Object expectedValue = contextAware.getExpectedValues().get(key);
27
28 if (expectedValue.toString().equals(
29 contextAware.getContextSkipFlag())) {
30 if (log.isDebugEnabled())
31 log.debug("Skipped check for key '" + key + "'");
32 continue;
33 }
34
35 // Register in structure
36 registerInStructure(testResult, treeSRelated, key);
37
38 if (contextAware.getValues().containsKey(key)) {
39 Object reachedValue = contextAware.getValues().get(key);
40
41 if (expectedValue.equals(contextAware.getContextAnyFlag())) {
42 testResult.addResultPart(new SimpleResultPart(
43 TestStatus.PASSED, "Expected any value for key '"
44 + key + "'"));
45 } else if (expectedValue.equals(reachedValue)) {
46 testResult.addResultPart(new SimpleResultPart(
47 TestStatus.PASSED, "Values matched for key '" + key
48 + "'"));
49 } else {
50 testResult.addResultPart(new SimpleResultPart(
51 TestStatus.FAILED, "Mismatch for key '" + key
52 + "': expected '" + expectedValue
53 + "' but reached '" + reachedValue + "'"));
54 }
55 } else {
56 testResult.addResultPart(new SimpleResultPart(
57 TestStatus.FAILED, "No value reached for key '" + key
58 + "'"));
59 }
60 resetStructure(testResult, treeSRelated);
61 }
62 }
63
64 private static void registerInStructure(TestResult testResult,
65 TreeSRelated treeSRelated, String key) {
66 if (treeSRelated != null) {
67 if (treeSRelated.getBasePath() != null) {
68 TreeSPath path = treeSRelated.getBasePath().createChild(key);
69 StructureRegistry<TreeSPath> registry = treeSRelated
70 .getRegistry();
71 final StructureElement element = treeSRelated
72 .getStructureElement(key);
73 registry.register(path, element);
74 if (testResult instanceof StructureAware)
75 ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(
76 registry, path);
77
78 if (log.isDebugEnabled())
79 log.debug("Checking key " + key + " for path " + path);
80 }
81 }
82 }
83
84 private static void resetStructure(TestResult testResult,
85 TreeSRelated treeSRelated) {
86 if (treeSRelated != null) {
87 if (treeSRelated.getBasePath() != null) {
88 if (testResult instanceof StructureAware) {
89 ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(
90 treeSRelated.getRegistry(), treeSRelated
91 .getBasePath());
92 }
93 }
94 }
95 }
96
97 /**
98 * Makes sure that all children and sub-children of parent share the same
99 * maps for values and expected values.
100 */
101 public static void synchronize(ParentContextAware parent) {
102 Map<String, Object> expectedValuesCommon = new TreeMap<String, Object>(
103 parent.getExpectedValues());
104 synchronize(parent, expectedValuesCommon);
105 if (log.isDebugEnabled())
106 log.debug("Synchonized context " + parent);
107
108 }
109
110 private static void synchronize(ParentContextAware parent,
111 Map<String, Object> expectedValuesCommon) {
112 for (ContextAware child : parent.getChildContexts()) {
113 // Values
114 putNotContained(parent.getValues(), child.getValues());
115 child.setValues(parent.getValues());
116
117 // Expected Values
118 // Expected values reference is not overridden: each child has its
119 // own expected values map.
120 overrideContained(expectedValuesCommon, child.getExpectedValues());
121
122 // Creates a new Map in order not to disturb other context using the
123 // same keys
124 Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(
125 expectedValuesCommon);
126 putNotContained(expectedValuesCommonChild, child
127 .getExpectedValues());
128
129 if (child instanceof ParentContextAware) {
130 // Recursive sync
131 synchronize((ParentContextAware) child,
132 expectedValuesCommonChild);
133 }
134 }
135
136 }
137
138 /**
139 * Put into common map the values from child map which are not already
140 * defined in common map.
141 */
142 public static void putNotContained(Map<String, Object> commonMap,
143 Map<String, Object> childMap) {
144 for (String key : childMap.keySet()) {
145 if (!commonMap.containsKey(key)) {
146 commonMap.put(key, childMap.get(key));
147 }
148 }
149 }
150
151 /** Overrides child map values with the values already set in common map */
152 public static void overrideContained(Map<String, Object> commonMap,
153 Map<String, Object> childMap) {
154 for (String key : childMap.keySet()) {
155 if (commonMap.containsKey(key)) {
156 childMap.put(key, commonMap.get(key));
157 }
158 }
159 }
160
161 /** Makes sure this cannot be instantiated. */
162 private ContextUtils() {
163
164 }
165 }