]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextHelper.java
Start adding enhancement toward v0.8.1
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / context / ContextHelper.java
1 package org.argeo.slc.core.test.context;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import org.argeo.slc.core.structure.StructureAware;
7 import org.argeo.slc.core.structure.StructureElement;
8 import org.argeo.slc.core.structure.StructureRegistry;
9 import org.argeo.slc.core.structure.tree.TreeSPath;
10 import org.argeo.slc.core.structure.tree.TreeSRelated;
11 import org.argeo.slc.core.test.SimpleResultPart;
12 import org.argeo.slc.core.test.TestResult;
13 import org.argeo.slc.core.test.TestStatus;
14
15 public class ContextHelper {
16 private final static Log log = LogFactory.getLog(ContextHelper.class);
17
18 public static void compareReachedExpected(ContextAware contextAware,
19 TestResult testResult, TreeSRelated treeSRelated) {
20 for (String key : contextAware.getExpectedValues().keySet()) {
21
22 // Register in structure
23 if (treeSRelated != null) {
24 if (treeSRelated.getBasePath() != null) {
25 TreeSPath path = treeSRelated.getBasePath()
26 .createChild(key);
27 StructureRegistry<TreeSPath> registry = treeSRelated
28 .getRegistry();
29 final StructureElement element = treeSRelated
30 .getStructureElement(key);
31 registry.register(path, element);
32 if (testResult instanceof StructureAware)
33 ((StructureAware<TreeSPath>) testResult)
34 .notifyCurrentPath(registry, path);
35
36 if (log.isDebugEnabled())
37 log.debug("Checking key " + key + " for path " + path);
38 }
39 }
40
41 // Compare expected values with reached ones
42 Object expectedValue = contextAware.getExpectedValues().get(key);
43
44 if (contextAware.getValues().containsKey(key)) {
45 Object reachedValue = contextAware.getValues().get(key);
46
47 if (expectedValue.equals(reachedValue)) {
48 testResult.addResultPart(new SimpleResultPart(
49 TestStatus.PASSED, "Values matched for key '" + key
50 + "'"));
51 } else {
52 testResult.addResultPart(new SimpleResultPart(
53 TestStatus.FAILED, "Mismatch for key '" + key
54 + "': expected '" + expectedValue
55 + "' but reached '" + reachedValue + "'"));
56 }
57 } else {
58 testResult.addResultPart(new SimpleResultPart(
59 TestStatus.FAILED, "No value reached for key '" + key
60 + "'"));
61 }
62
63 if (treeSRelated != null) {
64 if (treeSRelated.getBasePath() != null) {
65 if (testResult instanceof StructureAware) {
66 ((StructureAware<TreeSPath>) testResult)
67 .notifyCurrentPath(treeSRelated.getRegistry(),
68 treeSRelated.getBasePath());
69 }
70 }
71 }
72 }
73 }
74
75 }