]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java
Improve If
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / context / ContextUtils.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.test.context;
18
19 import java.util.Map;
20 import java.util.TreeMap;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
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;
35
36 public class ContextUtils {
37 private final static Log log = LogFactory.getLog(ContextUtils.class);
38
39 public static void compareReachedExpected(ContextAware contextAware,
40 TestResult testResult, TreeSRelated treeSRelated) {
41 for (String key : contextAware.getExpectedValues().keySet()) {
42
43 // Compare expected values with reached ones
44 Object expectedValue = contextAware.getExpectedValues().get(key);
45
46 if (expectedValue.toString().equals(
47 contextAware.getContextSkipFlag())) {
48 if (log.isDebugEnabled())
49 log.debug("Skipped check for key '" + key + "'");
50 continue;
51 }
52
53 // Register in structure
54 registerInStructure(testResult, treeSRelated, key);
55
56 if (contextAware.getValues().containsKey(key)) {
57 Object reachedValue = contextAware.getValues().get(key);
58
59 if (expectedValue.equals(contextAware.getContextAnyFlag())) {
60 testResult.addResultPart(new SimpleResultPart(
61 TestStatus.PASSED, "Expected any value for key '"
62 + key + "'"));
63 } else if (expectedValue.equals(reachedValue)) {
64 testResult.addResultPart(new SimpleResultPart(
65 TestStatus.PASSED, "Values matched for key '" + key
66 + "'"));
67 } else {
68 testResult.addResultPart(new SimpleResultPart(
69 TestStatus.FAILED, "Mismatch for key '" + key
70 + "': expected '" + expectedValue
71 + "' but reached '" + reachedValue + "'"));
72 }
73 } else {
74 testResult.addResultPart(new SimpleResultPart(
75 TestStatus.FAILED, "No value reached for key '" + key
76 + "'"));
77 }
78 resetStructure(testResult, treeSRelated);
79 }
80 }
81
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
89 .getRegistry();
90 final StructureElement element = treeSRelated
91 .getStructureElement(key);
92 registry.register(path, element);
93 if (testResult instanceof StructureAware)
94 ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(
95 registry, path);
96
97 if (log.isDebugEnabled())
98 log.debug("Checking key " + key + " for path " + path);
99 }
100 }
101 }
102
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
111 .getBasePath());
112 }
113 }
114 }
115 }
116
117 /**
118 * Makes sure that all children and sub-children of parent share the same
119 * maps for values and expected values.
120 */
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);
127
128 }
129
130 private static void synchronize(ParentContextAware parent,
131 Map<String, Object> expectedValuesCommon) {
132 for (ContextAware child : parent.getChildContexts()) {
133 // Values
134 putNotContained(parent.getValues(), child.getValues());
135 child.setValues(parent.getValues());
136
137 // Expected Values
138 // Expected values reference is not overridden: each child has its
139 // own expected values map.
140 overrideContained(expectedValuesCommon, child.getExpectedValues());
141
142 // Creates a new Map in order not to disturb other context using the
143 // same keys
144 Map<String, Object> expectedValuesCommonChild = new TreeMap<String, Object>(
145 expectedValuesCommon);
146 putNotContained(expectedValuesCommonChild, child
147 .getExpectedValues());
148
149 if (child instanceof ParentContextAware) {
150 // Recursive sync
151 synchronize((ParentContextAware) child,
152 expectedValuesCommonChild);
153 }
154 }
155
156 }
157
158 /**
159 * Put into common map the values from child map which are not already
160 * defined in common map.
161 */
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));
167 }
168 }
169 }
170
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));
177 }
178 }
179 }
180
181 /** Makes sure this cannot be instantiated. */
182 private ContextUtils() {
183
184 }
185 }