]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/slc/runtime/test/ContextUtils.java
Continue to remove dependencies with Spring.
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / test / ContextUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.runtime.test;
17
18 import java.util.Map;
19 import java.util.TreeMap;
20
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;
27
28 /** Utilities for comparing and synchronising contexts. */
29 public class ContextUtils {
30 private final static Log log = LogFactory.getLog(ContextUtils.class);
31
32 public static void compareReachedExpected(ContextAware contextAware,
33 TestResult testResult) {
34 for (String key : contextAware.getExpectedValues().keySet()) {
35
36 // Compare expected values with reached ones
37 Object expectedValue = contextAware.getExpectedValues().get(key);
38
39 if (expectedValue.toString().equals(
40 contextAware.getContextSkipFlag())) {
41 if (log.isDebugEnabled())
42 log.debug("Skipped check for key '" + key + "'");
43 continue;
44 }
45
46 if (contextAware.getValues().containsKey(key)) {
47 Object reachedValue = contextAware.getValues().get(key);
48
49 if (expectedValue.equals(contextAware.getContextAnyFlag())) {
50 testResult.addResultPart(new SimpleResultPart(
51 TestStatus.PASSED, "Expected any value for key '"
52 + key + "'"));
53 } else if (expectedValue.equals(reachedValue)) {
54 testResult.addResultPart(new SimpleResultPart(
55 TestStatus.PASSED, "Values matched for key '" + key
56 + "'"));
57 } else {
58 testResult.addResultPart(new SimpleResultPart(
59 TestStatus.FAILED, "Mismatch for key '" + key
60 + "': expected '" + expectedValue
61 + "' but reached '" + reachedValue + "'"));
62 }
63 } else {
64 testResult.addResultPart(new SimpleResultPart(
65 TestStatus.FAILED, "No value reached for key '" + key
66 + "'"));
67 }
68 }
69 }
70
71 /**
72 * Makes sure that all children and sub-children of parent share the same
73 * maps for values and expected values.
74 */
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);
81
82 }
83
84 private static void synchronize(ParentContextAware parent,
85 Map<String, Object> expectedValuesCommon) {
86 for (ContextAware child : parent.getChildContexts()) {
87 // Values
88 putNotContained(parent.getValues(), child.getValues());
89 child.setValues(parent.getValues());
90
91 // Expected Values
92 // Expected values reference is not overridden: each child has its
93 // own expected values map.
94 overrideContained(expectedValuesCommon, child.getExpectedValues());
95
96 // Creates a new Map in order not to disturb other context using the
97 // same keys
98 Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(
99 expectedValuesCommon);
100 putNotContained(expectedValuesCommonChild,
101 child.getExpectedValues());
102
103 if (child instanceof ParentContextAware) {
104 // Recursive sync
105 synchronize((ParentContextAware) child,
106 expectedValuesCommonChild);
107 }
108 }
109
110 }
111
112 /**
113 * Put into common map the values from child map which are not already
114 * defined in common map.
115 */
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));
121 }
122 }
123 }
124
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));
131 }
132 }
133 }
134
135 /** Makes sure this cannot be instantiated. */
136 private ContextUtils() {
137
138 }
139 }