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
.tree
.TreeSPath
;
10 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRelated
;
11 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
12 import org
.argeo
.slc
.structure
.StructureAware
;
13 import org
.argeo
.slc
.structure
.StructureElement
;
14 import org
.argeo
.slc
.structure
.StructureRegistry
;
15 import org
.argeo
.slc
.test
.TestResult
;
16 import org
.argeo
.slc
.test
.TestStatus
;
17 import org
.argeo
.slc
.test
.context
.ContextAware
;
18 import org
.argeo
.slc
.test
.context
.ParentContextAware
;
20 public class ContextUtils
{
21 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
23 public static void compareReachedExpected(ContextAware contextAware
,
24 TestResult testResult
, TreeSRelated treeSRelated
) {
25 for (String key
: contextAware
.getExpectedValues().keySet()) {
27 // Compare expected values with reached ones
28 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
30 if (expectedValue
.toString().equals(
31 contextAware
.getContextSkipFlag())) {
32 if (log
.isDebugEnabled())
33 log
.debug("Skipped check for key '" + key
+ "'");
37 // Register in structure
38 registerInStructure(testResult
, treeSRelated
, key
);
40 if (contextAware
.getValues().containsKey(key
)) {
41 Object reachedValue
= contextAware
.getValues().get(key
);
43 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
44 testResult
.addResultPart(new SimpleResultPart(
45 TestStatus
.PASSED
, "Expected any value for key '"
47 } else if (expectedValue
.equals(reachedValue
)) {
48 testResult
.addResultPart(new SimpleResultPart(
49 TestStatus
.PASSED
, "Values matched for key '" + key
52 testResult
.addResultPart(new SimpleResultPart(
53 TestStatus
.FAILED
, "Mismatch for key '" + key
54 + "': expected '" + expectedValue
55 + "' but reached '" + reachedValue
+ "'"));
58 testResult
.addResultPart(new SimpleResultPart(
59 TestStatus
.FAILED
, "No value reached for key '" + key
62 resetStructure(testResult
, treeSRelated
);
66 @SuppressWarnings("unchecked")
67 private static void registerInStructure(TestResult testResult
,
68 TreeSRelated treeSRelated
, String key
) {
69 if (treeSRelated
!= null) {
70 if (treeSRelated
.getBasePath() != null) {
71 TreeSPath path
= treeSRelated
.getBasePath().createChild(key
);
72 StructureRegistry
<TreeSPath
> registry
= treeSRelated
74 final StructureElement element
= treeSRelated
75 .getStructureElement(key
);
76 registry
.register(path
, element
);
77 if (testResult
instanceof StructureAware
)
78 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
81 if (log
.isDebugEnabled())
82 log
.debug("Checking key " + key
+ " for path " + path
);
87 @SuppressWarnings("unchecked")
88 private static void resetStructure(TestResult testResult
,
89 TreeSRelated treeSRelated
) {
90 if (treeSRelated
!= null) {
91 if (treeSRelated
.getBasePath() != null) {
92 if (testResult
instanceof StructureAware
) {
93 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
94 treeSRelated
.getRegistry(), treeSRelated
102 * Makes sure that all children and sub-children of parent share the same
103 * maps for values and expected values.
105 public static void synchronize(ParentContextAware parent
) {
106 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
107 parent
.getExpectedValues());
108 synchronize(parent
, expectedValuesCommon
);
109 if (log
.isDebugEnabled())
110 log
.debug("Synchonized context " + parent
);
114 private static void synchronize(ParentContextAware parent
,
115 Map
<String
, Object
> expectedValuesCommon
) {
116 for (ContextAware child
: parent
.getChildContexts()) {
118 putNotContained(parent
.getValues(), child
.getValues());
119 child
.setValues(parent
.getValues());
122 // Expected values reference is not overridden: each child has its
123 // own expected values map.
124 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
126 // Creates a new Map in order not to disturb other context using the
128 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
129 expectedValuesCommon
);
130 putNotContained(expectedValuesCommonChild
, child
131 .getExpectedValues());
133 if (child
instanceof ParentContextAware
) {
135 synchronize((ParentContextAware
) child
,
136 expectedValuesCommonChild
);
143 * Put into common map the values from child map which are not already
144 * defined in common map.
146 public static void putNotContained(Map
<String
, Object
> commonMap
,
147 Map
<String
, Object
> childMap
) {
148 for (String key
: childMap
.keySet()) {
149 if (!commonMap
.containsKey(key
)) {
150 commonMap
.put(key
, childMap
.get(key
));
155 /** Overrides child map values with the values already set in common map */
156 public static void overrideContained(Map
<String
, Object
> commonMap
,
157 Map
<String
, Object
> childMap
) {
158 for (String key
: childMap
.keySet()) {
159 if (commonMap
.containsKey(key
)) {
160 childMap
.put(key
, commonMap
.get(key
));
165 /** Makes sure this cannot be instantiated. */
166 private ContextUtils() {