]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/slc/runtime/test/ContextUtils.java
Prepare next development cycle
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / test / ContextUtils.java
1 package org.argeo.slc.runtime.test;
2
3 import java.util.Map;
4 import java.util.TreeMap;
5
6 import org.argeo.api.slc.test.TestResult;
7 import org.argeo.api.slc.test.TestStatus;
8 import org.argeo.api.slc.test.context.ContextAware;
9 import org.argeo.api.slc.test.context.ParentContextAware;
10
11 /** Utilities for comparing and synchronising contexts. */
12 public class ContextUtils {
13 public static void compareReachedExpected(ContextAware contextAware, TestResult testResult) {
14 for (String key : contextAware.getExpectedValues().keySet()) {
15
16 // Compare expected values with reached ones
17 Object expectedValue = contextAware.getExpectedValues().get(key);
18
19 if (expectedValue.toString().equals(contextAware.getContextSkipFlag())) {
20 // if (log.isDebugEnabled())
21 // log.debug("Skipped check for key '" + key + "'");
22 continue;
23 }
24
25 if (contextAware.getValues().containsKey(key)) {
26 Object reachedValue = contextAware.getValues().get(key);
27
28 if (expectedValue.equals(contextAware.getContextAnyFlag())) {
29 testResult.addResultPart(
30 new SimpleResultPart(TestStatus.PASSED, "Expected any value for key '" + key + "'"));
31 } else if (expectedValue.equals(reachedValue)) {
32 testResult.addResultPart(
33 new SimpleResultPart(TestStatus.PASSED, "Values matched for key '" + key + "'"));
34 } else {
35 testResult.addResultPart(new SimpleResultPart(TestStatus.FAILED, "Mismatch for key '" + key
36 + "': expected '" + expectedValue + "' but reached '" + reachedValue + "'"));
37 }
38 } else {
39 testResult.addResultPart(
40 new SimpleResultPart(TestStatus.FAILED, "No value reached for key '" + key + "'"));
41 }
42 }
43 }
44
45 /**
46 * Makes sure that all children and sub-children of parent share the same maps
47 * for values and expected values.
48 */
49 public static void synchronize(ParentContextAware parent) {
50 Map<String, Object> expectedValuesCommon = new TreeMap<String, Object>(parent.getExpectedValues());
51 synchronize(parent, expectedValuesCommon);
52 // if (log.isDebugEnabled())
53 // log.debug("Synchronized context " + parent);
54
55 }
56
57 private static void synchronize(ParentContextAware parent, Map<String, Object> expectedValuesCommon) {
58 for (ContextAware child : parent.getChildContexts()) {
59 // Values
60 putNotContained(parent.getValues(), child.getValues());
61 child.setValues(parent.getValues());
62
63 // Expected Values
64 // Expected values reference is not overridden: each child has its
65 // own expected values map.
66 overrideContained(expectedValuesCommon, child.getExpectedValues());
67
68 // Creates a new Map in order not to disturb other context using the
69 // same keys
70 Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(expectedValuesCommon);
71 putNotContained(expectedValuesCommonChild, child.getExpectedValues());
72
73 if (child instanceof ParentContextAware) {
74 // Recursive sync
75 synchronize((ParentContextAware) child, expectedValuesCommonChild);
76 }
77 }
78
79 }
80
81 /**
82 * Put into common map the values from child map which are not already defined
83 * in common map.
84 */
85 public static void putNotContained(Map<String, Object> commonMap, Map<String, Object> childMap) {
86 for (String key : childMap.keySet()) {
87 if (!commonMap.containsKey(key)) {
88 commonMap.put(key, childMap.get(key));
89 }
90 }
91 }
92
93 /** Overrides child map values with the values already set in common map */
94 public static void overrideContained(Map<String, Object> commonMap, Map<String, Object> childMap) {
95 for (String key : childMap.keySet()) {
96 if (commonMap.containsKey(key)) {
97 childMap.put(key, commonMap.get(key));
98 }
99 }
100 }
101
102 /** Makes sure this cannot be instantiated. */
103 private ContextUtils() {
104
105 }
106 }