1 package org
.argeo
.slc
.runtime
.test
;
4 import java
.util
.TreeMap
;
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
;
13 /** Utilities for comparing and synchronising contexts. */
14 public class ContextUtils
{
15 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
17 public static void compareReachedExpected(ContextAware contextAware
,
18 TestResult testResult
) {
19 for (String key
: contextAware
.getExpectedValues().keySet()) {
21 // Compare expected values with reached ones
22 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
24 if (expectedValue
.toString().equals(
25 contextAware
.getContextSkipFlag())) {
26 if (log
.isDebugEnabled())
27 log
.debug("Skipped check for key '" + key
+ "'");
31 if (contextAware
.getValues().containsKey(key
)) {
32 Object reachedValue
= contextAware
.getValues().get(key
);
34 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
35 testResult
.addResultPart(new SimpleResultPart(
36 TestStatus
.PASSED
, "Expected any value for key '"
38 } else if (expectedValue
.equals(reachedValue
)) {
39 testResult
.addResultPart(new SimpleResultPart(
40 TestStatus
.PASSED
, "Values matched for key '" + key
43 testResult
.addResultPart(new SimpleResultPart(
44 TestStatus
.FAILED
, "Mismatch for key '" + key
45 + "': expected '" + expectedValue
46 + "' but reached '" + reachedValue
+ "'"));
49 testResult
.addResultPart(new SimpleResultPart(
50 TestStatus
.FAILED
, "No value reached for key '" + key
57 * Makes sure that all children and sub-children of parent share the same
58 * maps for values and expected values.
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
);
69 private static void synchronize(ParentContextAware parent
,
70 Map
<String
, Object
> expectedValuesCommon
) {
71 for (ContextAware child
: parent
.getChildContexts()) {
73 putNotContained(parent
.getValues(), child
.getValues());
74 child
.setValues(parent
.getValues());
77 // Expected values reference is not overridden: each child has its
78 // own expected values map.
79 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
81 // Creates a new Map in order not to disturb other context using the
83 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
84 expectedValuesCommon
);
85 putNotContained(expectedValuesCommonChild
,
86 child
.getExpectedValues());
88 if (child
instanceof ParentContextAware
) {
90 synchronize((ParentContextAware
) child
,
91 expectedValuesCommonChild
);
98 * Put into common map the values from child map which are not already
99 * defined in common map.
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
));
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
));
120 /** Makes sure this cannot be instantiated. */
121 private ContextUtils() {