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