1 package org
.argeo
.slc
.core
.test
.context
;
4 import java
.util
.TreeMap
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
9 import org
.argeo
.slc
.core
.structure
.StructureAware
;
10 import org
.argeo
.slc
.core
.structure
.StructureElement
;
11 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
12 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
13 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRelated
;
14 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
15 import org
.argeo
.slc
.core
.test
.TestResult
;
16 import org
.argeo
.slc
.core
.test
.TestStatus
;
18 public class ContextUtils
{
19 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
21 public static void compareReachedExpected(ContextAware contextAware
,
22 TestResult testResult
, TreeSRelated treeSRelated
) {
23 for (String key
: contextAware
.getExpectedValues().keySet()) {
25 // Compare expected values with reached ones
26 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
28 if (expectedValue
.toString().equals(
29 contextAware
.getContextSkipFlag())) {
30 if (log
.isDebugEnabled())
31 log
.debug("Skipped check for key '" + key
+ "'");
35 // Register in structure
36 registerInStructure(testResult
, treeSRelated
, key
);
38 if (contextAware
.getValues().containsKey(key
)) {
39 Object reachedValue
= contextAware
.getValues().get(key
);
41 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
42 testResult
.addResultPart(new SimpleResultPart(
43 TestStatus
.PASSED
, "Expected any value for key '"
45 } else if (expectedValue
.equals(reachedValue
)) {
46 testResult
.addResultPart(new SimpleResultPart(
47 TestStatus
.PASSED
, "Values matched for key '" + key
50 testResult
.addResultPart(new SimpleResultPart(
51 TestStatus
.FAILED
, "Mismatch for key '" + key
52 + "': expected '" + expectedValue
53 + "' but reached '" + reachedValue
+ "'"));
56 testResult
.addResultPart(new SimpleResultPart(
57 TestStatus
.FAILED
, "No value reached for key '" + key
60 resetStructure(testResult
, treeSRelated
);
64 private static void registerInStructure(TestResult testResult
,
65 TreeSRelated treeSRelated
, String key
) {
66 if (treeSRelated
!= null) {
67 if (treeSRelated
.getBasePath() != null) {
68 TreeSPath path
= treeSRelated
.getBasePath().createChild(key
);
69 StructureRegistry
<TreeSPath
> registry
= treeSRelated
71 final StructureElement element
= treeSRelated
72 .getStructureElement(key
);
73 registry
.register(path
, element
);
74 if (testResult
instanceof StructureAware
)
75 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
78 if (log
.isDebugEnabled())
79 log
.debug("Checking key " + key
+ " for path " + path
);
84 private static void resetStructure(TestResult testResult
,
85 TreeSRelated treeSRelated
) {
86 if (treeSRelated
!= null) {
87 if (treeSRelated
.getBasePath() != null) {
88 if (testResult
instanceof StructureAware
) {
89 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
90 treeSRelated
.getRegistry(), treeSRelated
98 * Makes sure that all children and sub-children of parent share the same
99 * maps for values and expected values.
101 public static void synchronize(ParentContextAware parent
) {
102 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
103 parent
.getExpectedValues());
104 synchronize(parent
, expectedValuesCommon
);
105 if (log
.isDebugEnabled())
106 log
.debug("Synchonized context " + parent
);
110 private static void synchronize(ParentContextAware parent
,
111 Map
<String
, Object
> expectedValuesCommon
) {
112 for (ContextAware child
: parent
.getChildContexts()) {
114 putNotContained(parent
.getValues(), child
.getValues());
115 child
.setValues(parent
.getValues());
118 // Expected values reference is not overridden: each child has its
119 // own expected values map.
120 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
122 // Creates a new Map in order not to disturb other context using the
124 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
125 expectedValuesCommon
);
126 putNotContained(expectedValuesCommonChild
, child
127 .getExpectedValues());
129 if (child
instanceof ParentContextAware
) {
131 synchronize((ParentContextAware
) child
,
132 expectedValuesCommonChild
);
139 * Put into common map the values from child map which are not already
140 * defined in common map.
142 public static void putNotContained(Map
<String
, Object
> commonMap
,
143 Map
<String
, Object
> childMap
) {
144 for (String key
: childMap
.keySet()) {
145 if (!commonMap
.containsKey(key
)) {
146 commonMap
.put(key
, childMap
.get(key
));
151 /** Overrides child map values with the values already set in common map */
152 public static void overrideContained(Map
<String
, Object
> commonMap
,
153 Map
<String
, Object
> childMap
) {
154 for (String key
: childMap
.keySet()) {
155 if (commonMap
.containsKey(key
)) {
156 childMap
.put(key
, commonMap
.get(key
));
161 /** Makes sure this cannot be instantiated. */
162 private ContextUtils() {