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