2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.core
.test
.context
;
19 import java
.util
.TreeMap
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
25 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRelated
;
26 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
27 import org
.argeo
.slc
.structure
.StructureAware
;
28 import org
.argeo
.slc
.structure
.StructureElement
;
29 import org
.argeo
.slc
.structure
.StructureRegistry
;
30 import org
.argeo
.slc
.test
.TestResult
;
31 import org
.argeo
.slc
.test
.TestStatus
;
32 import org
.argeo
.slc
.test
.context
.ContextAware
;
33 import org
.argeo
.slc
.test
.context
.ParentContextAware
;
35 public class ContextUtils
{
36 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
38 public static void compareReachedExpected(ContextAware contextAware
,
39 TestResult testResult
, TreeSRelated treeSRelated
) {
40 for (String key
: contextAware
.getExpectedValues().keySet()) {
42 // Compare expected values with reached ones
43 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
45 if (expectedValue
.toString().equals(
46 contextAware
.getContextSkipFlag())) {
47 if (log
.isDebugEnabled())
48 log
.debug("Skipped check for key '" + key
+ "'");
52 // Register in structure
53 registerInStructure(testResult
, treeSRelated
, key
);
55 if (contextAware
.getValues().containsKey(key
)) {
56 Object reachedValue
= contextAware
.getValues().get(key
);
58 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
59 testResult
.addResultPart(new SimpleResultPart(
60 TestStatus
.PASSED
, "Expected any value for key '"
62 } else if (expectedValue
.equals(reachedValue
)) {
63 testResult
.addResultPart(new SimpleResultPart(
64 TestStatus
.PASSED
, "Values matched for key '" + key
67 testResult
.addResultPart(new SimpleResultPart(
68 TestStatus
.FAILED
, "Mismatch for key '" + key
69 + "': expected '" + expectedValue
70 + "' but reached '" + reachedValue
+ "'"));
73 testResult
.addResultPart(new SimpleResultPart(
74 TestStatus
.FAILED
, "No value reached for key '" + key
77 resetStructure(testResult
, treeSRelated
);
81 @SuppressWarnings("unchecked")
82 private static void registerInStructure(TestResult testResult
,
83 TreeSRelated treeSRelated
, String key
) {
84 if (treeSRelated
!= null) {
85 if (treeSRelated
.getBasePath() != null) {
86 TreeSPath path
= treeSRelated
.getBasePath().createChild(key
);
87 StructureRegistry
<TreeSPath
> registry
= treeSRelated
89 final StructureElement element
= treeSRelated
90 .getStructureElement(key
);
91 registry
.register(path
, element
);
92 if (testResult
instanceof StructureAware
)
93 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
96 if (log
.isDebugEnabled())
97 log
.debug("Checking key " + key
+ " for path " + path
);
102 @SuppressWarnings("unchecked")
103 private static void resetStructure(TestResult testResult
,
104 TreeSRelated treeSRelated
) {
105 if (treeSRelated
!= null) {
106 if (treeSRelated
.getBasePath() != null) {
107 if (testResult
instanceof StructureAware
) {
108 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
109 treeSRelated
.getRegistry(), treeSRelated
117 * Makes sure that all children and sub-children of parent share the same
118 * maps for values and expected values.
120 public static void synchronize(ParentContextAware parent
) {
121 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
122 parent
.getExpectedValues());
123 synchronize(parent
, expectedValuesCommon
);
124 if (log
.isDebugEnabled())
125 log
.debug("Synchronized context " + parent
);
129 private static void synchronize(ParentContextAware parent
,
130 Map
<String
, Object
> expectedValuesCommon
) {
131 for (ContextAware child
: parent
.getChildContexts()) {
133 putNotContained(parent
.getValues(), child
.getValues());
134 child
.setValues(parent
.getValues());
137 // Expected values reference is not overridden: each child has its
138 // own expected values map.
139 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
141 // Creates a new Map in order not to disturb other context using the
143 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
144 expectedValuesCommon
);
145 putNotContained(expectedValuesCommonChild
, child
146 .getExpectedValues());
148 if (child
instanceof ParentContextAware
) {
150 synchronize((ParentContextAware
) child
,
151 expectedValuesCommonChild
);
158 * Put into common map the values from child map which are not already
159 * defined in common map.
161 public static void putNotContained(Map
<String
, Object
> commonMap
,
162 Map
<String
, Object
> childMap
) {
163 for (String key
: childMap
.keySet()) {
164 if (!commonMap
.containsKey(key
)) {
165 commonMap
.put(key
, childMap
.get(key
));
170 /** Overrides child map values with the values already set in common map */
171 public static void overrideContained(Map
<String
, Object
> commonMap
,
172 Map
<String
, Object
> childMap
) {
173 for (String key
: childMap
.keySet()) {
174 if (commonMap
.containsKey(key
)) {
175 childMap
.put(key
, commonMap
.get(key
));
180 /** Makes sure this cannot be instantiated. */
181 private ContextUtils() {