2 * Copyright (C) 2007-2012 Argeo GmbH
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
.runtime
.test
;
19 import java
.util
.TreeMap
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.test
.TestResult
;
24 import org
.argeo
.slc
.test
.TestStatus
;
25 import org
.argeo
.slc
.test
.context
.ContextAware
;
26 import org
.argeo
.slc
.test
.context
.ParentContextAware
;
28 /** Utilities for comparing and synchronising contexts. */
29 public class ContextUtils
{
30 private final static Log log
= LogFactory
.getLog(ContextUtils
.class);
32 public static void compareReachedExpected(ContextAware contextAware
,
33 TestResult testResult
) {
34 for (String key
: contextAware
.getExpectedValues().keySet()) {
36 // Compare expected values with reached ones
37 Object expectedValue
= contextAware
.getExpectedValues().get(key
);
39 if (expectedValue
.toString().equals(
40 contextAware
.getContextSkipFlag())) {
41 if (log
.isDebugEnabled())
42 log
.debug("Skipped check for key '" + key
+ "'");
46 if (contextAware
.getValues().containsKey(key
)) {
47 Object reachedValue
= contextAware
.getValues().get(key
);
49 if (expectedValue
.equals(contextAware
.getContextAnyFlag())) {
50 testResult
.addResultPart(new SimpleResultPart(
51 TestStatus
.PASSED
, "Expected any value for key '"
53 } else if (expectedValue
.equals(reachedValue
)) {
54 testResult
.addResultPart(new SimpleResultPart(
55 TestStatus
.PASSED
, "Values matched for key '" + key
58 testResult
.addResultPart(new SimpleResultPart(
59 TestStatus
.FAILED
, "Mismatch for key '" + key
60 + "': expected '" + expectedValue
61 + "' but reached '" + reachedValue
+ "'"));
64 testResult
.addResultPart(new SimpleResultPart(
65 TestStatus
.FAILED
, "No value reached for key '" + key
72 * Makes sure that all children and sub-children of parent share the same
73 * maps for values and expected values.
75 public static void synchronize(ParentContextAware parent
) {
76 Map
<String
, Object
> expectedValuesCommon
= new TreeMap
<String
, Object
>(
77 parent
.getExpectedValues());
78 synchronize(parent
, expectedValuesCommon
);
79 if (log
.isDebugEnabled())
80 log
.debug("Synchronized context " + parent
);
84 private static void synchronize(ParentContextAware parent
,
85 Map
<String
, Object
> expectedValuesCommon
) {
86 for (ContextAware child
: parent
.getChildContexts()) {
88 putNotContained(parent
.getValues(), child
.getValues());
89 child
.setValues(parent
.getValues());
92 // Expected values reference is not overridden: each child has its
93 // own expected values map.
94 overrideContained(expectedValuesCommon
, child
.getExpectedValues());
96 // Creates a new Map in order not to disturb other context using the
98 Map
<String
, Object
> expectedValuesCommonChild
= new TreeMap
<String
, Object
>(
99 expectedValuesCommon
);
100 putNotContained(expectedValuesCommonChild
,
101 child
.getExpectedValues());
103 if (child
instanceof ParentContextAware
) {
105 synchronize((ParentContextAware
) child
,
106 expectedValuesCommonChild
);
113 * Put into common map the values from child map which are not already
114 * defined in common map.
116 public static void putNotContained(Map
<String
, Object
> commonMap
,
117 Map
<String
, Object
> childMap
) {
118 for (String key
: childMap
.keySet()) {
119 if (!commonMap
.containsKey(key
)) {
120 commonMap
.put(key
, childMap
.get(key
));
125 /** Overrides child map values with the values already set in common map */
126 public static void overrideContained(Map
<String
, Object
> commonMap
,
127 Map
<String
, Object
> childMap
) {
128 for (String key
: childMap
.keySet()) {
129 if (commonMap
.containsKey(key
)) {
130 childMap
.put(key
, commonMap
.get(key
));
135 /** Makes sure this cannot be instantiated. */
136 private ContextUtils() {