1 package org
.argeo
.slc
.runtime
.test
;
4 import java
.util
.TreeMap
;
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
;
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()) {
16 // Compare expected values with reached ones
17 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
19 if (expectedValue
.toString().equals(contextAware
.getContextSkipFlag())) {
20 // if (log.isDebugEnabled())
21 // log.debug("Skipped check for key '" + key + "'");
25 if (contextAware
.getValues().containsKey(key
)) {
26 Object reachedValue
= contextAware
.getValues().get(key
);
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
+ "'"));
35 testResult
.addResultPart(new SimpleResultPart(TestStatus
.FAILED
, "Mismatch for key '" + key
36 + "': expected '" + expectedValue
+ "' but reached '" + reachedValue
+ "'"));
39 testResult
.addResultPart(
40 new SimpleResultPart(TestStatus
.FAILED
, "No value reached for key '" + key
+ "'"));
46 * Makes sure that all children and sub-children of parent share the same maps
47 * for values and expected values.
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);
57 private static void synchronize(ParentContextAware parent
, Map
<String
, Object
> expectedValuesCommon
) {
58 for (ContextAware child
: parent
.getChildContexts()) {
60 putNotContained(parent
.getValues(), child
.getValues());
61 child
.setValues(parent
.getValues());
64 // Expected values reference is not overridden: each child has its
65 // own expected values map.
66 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
68 // Creates a new Map in order not to disturb other context using the
70 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(expectedValuesCommon
);
71 putNotContained(expectedValuesCommonChild
, child
.getExpectedValues());
73 if (child
instanceof ParentContextAware
) {
75 synchronize((ParentContextAware
) child
, expectedValuesCommonChild
);
82 * Put into common map the values from child map which are not already defined
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
));
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
));
102 /** Makes sure this cannot be instantiated. */
103 private ContextUtils() {