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