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 private static void registerInStructure(TestResult testResult
,
67 TreeSRelated treeSRelated
, String key
) {
68 if (treeSRelated
!= null) {
69 if (treeSRelated
.getBasePath() != null) {
70 TreeSPath path
= treeSRelated
.getBasePath().createChild(key
);
71 StructureRegistry
<TreeSPath
> registry
= treeSRelated
73 final StructureElement element
= treeSRelated
74 .getStructureElement(key
);
75 registry
.register(path
, element
);
76 if (testResult
instanceof StructureAware
)
77 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
80 if (log
.isDebugEnabled())
81 log
.debug("Checking key " + key
+ " for path " + path
);
86 private static void resetStructure(TestResult testResult
,
87 TreeSRelated treeSRelated
) {
88 if (treeSRelated
!= null) {
89 if (treeSRelated
.getBasePath() != null) {
90 if (testResult
instanceof StructureAware
) {
91 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
92 treeSRelated
.getRegistry(), treeSRelated
100 * Makes sure that all children and sub-children of parent share the same
101 * maps for values and expected values.
103 public static void synchronize(ParentContextAware parent
) {
104 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
105 parent
.getExpectedValues());
106 synchronize(parent
, expectedValuesCommon
);
107 if (log
.isDebugEnabled())
108 log
.debug("Synchonized context " + parent
);
112 private static void synchronize(ParentContextAware parent
,
113 Map
<String
, Object
> expectedValuesCommon
) {
114 for (ContextAware child
: parent
.getChildContexts()) {
116 putNotContained(parent
.getValues(), child
.getValues());
117 child
.setValues(parent
.getValues());
120 // Expected values reference is not overridden: each child has its
121 // own expected values map.
122 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
124 // Creates a new Map in order not to disturb other context using the
126 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
127 expectedValuesCommon
);
128 putNotContained(expectedValuesCommonChild
, child
129 .getExpectedValues());
131 if (child
instanceof ParentContextAware
) {
133 synchronize((ParentContextAware
) child
,
134 expectedValuesCommonChild
);
141 * Put into common map the values from child map which are not already
142 * defined in common map.
144 public static void putNotContained(Map
<String
, Object
> commonMap
,
145 Map
<String
, Object
> childMap
) {
146 for (String key
: childMap
.keySet()) {
147 if (!commonMap
.containsKey(key
)) {
148 commonMap
.put(key
, childMap
.get(key
));
153 /** Overrides child map values with the values already set in common map */
154 public static void overrideContained(Map
<String
, Object
> commonMap
,
155 Map
<String
, Object
> childMap
) {
156 for (String key
: childMap
.keySet()) {
157 if (commonMap
.containsKey(key
)) {
158 childMap
.put(key
, commonMap
.get(key
));
163 /** Makes sure this cannot be instantiated. */
164 private ContextUtils() {