2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.core
.test
.context
;
20 import java
.util
.TreeMap
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
26 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRelated
;
27 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
28 import org
.argeo
.slc
.structure
.StructureAware
;
29 import org
.argeo
.slc
.structure
.StructureElement
;
30 import org
.argeo
.slc
.structure
.StructureRegistry
;
31 import org
.argeo
.slc
.test
.TestResult
;
32 import org
.argeo
.slc
.test
.TestStatus
;
33 import org
.argeo
.slc
.test
.context
.ContextAware
;
34 import org
.argeo
.slc
.test
.context
.ParentContextAware
;
36 public class ContextUtils
{
37 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
39 public static void compareReachedExpected(ContextAware contextAware
,
40 TestResult testResult
, TreeSRelated treeSRelated
) {
41 for (String key
: contextAware
.getExpectedValues().keySet()) {
43 // Compare expected values with reached ones
44 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
46 if (expectedValue
.toString().equals(
47 contextAware
.getContextSkipFlag())) {
48 if (log
.isDebugEnabled())
49 log
.debug("Skipped check for key '" + key
+ "'");
53 // Register in structure
54 registerInStructure(testResult
, treeSRelated
, key
);
56 if (contextAware
.getValues().containsKey(key
)) {
57 Object reachedValue
= contextAware
.getValues().get(key
);
59 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
60 testResult
.addResultPart(new SimpleResultPart(
61 TestStatus
.PASSED
, "Expected any value for key '"
63 } else if (expectedValue
.equals(reachedValue
)) {
64 testResult
.addResultPart(new SimpleResultPart(
65 TestStatus
.PASSED
, "Values matched for key '" + key
68 testResult
.addResultPart(new SimpleResultPart(
69 TestStatus
.FAILED
, "Mismatch for key '" + key
70 + "': expected '" + expectedValue
71 + "' but reached '" + reachedValue
+ "'"));
74 testResult
.addResultPart(new SimpleResultPart(
75 TestStatus
.FAILED
, "No value reached for key '" + key
78 resetStructure(testResult
, treeSRelated
);
82 @SuppressWarnings("unchecked")
83 private static void registerInStructure(TestResult testResult
,
84 TreeSRelated treeSRelated
, String key
) {
85 if (treeSRelated
!= null) {
86 if (treeSRelated
.getBasePath() != null) {
87 TreeSPath path
= treeSRelated
.getBasePath().createChild(key
);
88 StructureRegistry
<TreeSPath
> registry
= treeSRelated
90 final StructureElement element
= treeSRelated
91 .getStructureElement(key
);
92 registry
.register(path
, element
);
93 if (testResult
instanceof StructureAware
)
94 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
97 if (log
.isDebugEnabled())
98 log
.debug("Checking key " + key
+ " for path " + path
);
103 @SuppressWarnings("unchecked")
104 private static void resetStructure(TestResult testResult
,
105 TreeSRelated treeSRelated
) {
106 if (treeSRelated
!= null) {
107 if (treeSRelated
.getBasePath() != null) {
108 if (testResult
instanceof StructureAware
) {
109 ((StructureAware
<TreeSPath
>) testResult
).notifyCurrentPath(
110 treeSRelated
.getRegistry(), treeSRelated
118 * Makes sure that all children and sub-children of parent share the same
119 * maps for values and expected values.
121 public static void synchronize(ParentContextAware parent
) {
122 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
123 parent
.getExpectedValues());
124 synchronize(parent
, expectedValuesCommon
);
125 if (log
.isDebugEnabled())
126 log
.debug("Synchronized context " + parent
);
130 private static void synchronize(ParentContextAware parent
,
131 Map
<String
, Object
> expectedValuesCommon
) {
132 for (ContextAware child
: parent
.getChildContexts()) {
134 putNotContained(parent
.getValues(), child
.getValues());
135 child
.setValues(parent
.getValues());
138 // Expected values reference is not overridden: each child has its
139 // own expected values map.
140 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
142 // Creates a new Map in order not to disturb other context using the
144 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
145 expectedValuesCommon
);
146 putNotContained(expectedValuesCommonChild
, child
147 .getExpectedValues());
149 if (child
instanceof ParentContextAware
) {
151 synchronize((ParentContextAware
) child
,
152 expectedValuesCommonChild
);
159 * Put into common map the values from child map which are not already
160 * defined in common map.
162 public static void putNotContained(Map
<String
, Object
> commonMap
,
163 Map
<String
, Object
> childMap
) {
164 for (String key
: childMap
.keySet()) {
165 if (!commonMap
.containsKey(key
)) {
166 commonMap
.put(key
, childMap
.get(key
));
171 /** Overrides child map values with the values already set in common map */
172 public static void overrideContained(Map
<String
, Object
> commonMap
,
173 Map
<String
, Object
> childMap
) {
174 for (String key
: childMap
.keySet()) {
175 if (commonMap
.containsKey(key
)) {
176 childMap
.put(key
, commonMap
.get(key
));
181 /** Makes sure this cannot be instantiated. */
182 private ContextUtils() {