]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java
Introduce a factory bean to use execution resources
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / 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 private static void registerInStructure(TestResult testResult,
67 TreeSRelated treeSRelated, String key) {
68 if (treeSRelated != null) {
69 if (treeSRelated.getBasePath() != null) {
70 TreeSPath path = treeSRelated.getBasePath().createChild(key);
71 StructureRegistry<TreeSPath> registry = treeSRelated
72 .getRegistry();
73 final StructureElement element = treeSRelated
74 .getStructureElement(key);
75 registry.register(path, element);
76 if (testResult instanceof StructureAware)
77 ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(
78 registry, path);
79
80 if (log.isDebugEnabled())
81 log.debug("Checking key " + key + " for path " + path);
82 }
83 }
84 }
85
86 private static void resetStructure(TestResult testResult,
87 TreeSRelated treeSRelated) {
88 if (treeSRelated != null) {
89 if (treeSRelated.getBasePath() != null) {
90 if (testResult instanceof StructureAware) {
91 ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(
92 treeSRelated.getRegistry(), treeSRelated
93 .getBasePath());
94 }
95 }
96 }
97 }
98
99 /**
100 * Makes sure that all children and sub-children of parent share the same
101 * maps for values and expected values.
102 */
103 public static void synchronize(ParentContextAware parent) {
104 Map<String, Object> expectedValuesCommon = new TreeMap<String, Object>(
105 parent.getExpectedValues());
106 synchronize(parent, expectedValuesCommon);
107 if (log.isDebugEnabled())
108 log.debug("Synchonized context " + parent);
109
110 }
111
112 private static void synchronize(ParentContextAware parent,
113 Map<String, Object> expectedValuesCommon) {
114 for (ContextAware child : parent.getChildContexts()) {
115 // Values
116 putNotContained(parent.getValues(), child.getValues());
117 child.setValues(parent.getValues());
118
119 // Expected Values
120 // Expected values reference is not overridden: each child has its
121 // own expected values map.
122 overrideContained(expectedValuesCommon, child.getExpectedValues());
123
124 // Creates a new Map in order not to disturb other context using the
125 // same keys
126 Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(
127 expectedValuesCommon);
128 putNotContained(expectedValuesCommonChild, child
129 .getExpectedValues());
130
131 if (child instanceof ParentContextAware) {
132 // Recursive sync
133 synchronize((ParentContextAware) child,
134 expectedValuesCommonChild);
135 }
136 }
137
138 }
139
140 /**
141 * Put into common map the values from child map which are not already
142 * defined in common map.
143 */
144 public static void putNotContained(Map<String, Object> commonMap,
145 Map<String, Object> childMap) {
146 for (String key : childMap.keySet()) {
147 if (!commonMap.containsKey(key)) {
148 commonMap.put(key, childMap.get(key));
149 }
150 }
151 }
152
153 /** Overrides child map values with the values already set in common map */
154 public static void overrideContained(Map<String, Object> commonMap,
155 Map<String, Object> childMap) {
156 for (String key : childMap.keySet()) {
157 if (commonMap.containsKey(key)) {
158 childMap.put(key, commonMap.get(key));
159 }
160 }
161 }
162
163 /** Makes sure this cannot be instantiated. */
164 private ContextUtils() {
165
166 }
167 }