public Map<String, Object> getExpectedValues();\r
\r
public void setExpectedValues(Map<String, Object> expectedValues);\r
+\r
+ public String getContextSkipFlag();\r
+\r
+ public String getContextAnyFlag();\r
}\r
TestResult testResult, TreeSRelated treeSRelated) {\r
for (String key : contextAware.getExpectedValues().keySet()) {\r
\r
- // Register in structure\r
- if (treeSRelated != null) {\r
- if (treeSRelated.getBasePath() != null) {\r
- TreeSPath path = treeSRelated.getBasePath()\r
- .createChild(key);\r
- StructureRegistry<TreeSPath> registry = treeSRelated\r
- .getRegistry();\r
- final StructureElement element = treeSRelated\r
- .getStructureElement(key);\r
- registry.register(path, element);\r
- if (testResult instanceof StructureAware)\r
- ((StructureAware<TreeSPath>) testResult)\r
- .notifyCurrentPath(registry, path);\r
-\r
- if (log.isDebugEnabled())\r
- log.debug("Checking key " + key + " for path " + path);\r
- }\r
- }\r
-\r
// Compare expected values with reached ones\r
Object expectedValue = contextAware.getExpectedValues().get(key);\r
\r
+ if (expectedValue.toString().equals(\r
+ contextAware.getContextSkipFlag())) {\r
+ if (log.isDebugEnabled())\r
+ log.debug("Skipped check for key '" + key + "'");\r
+ continue;\r
+ }\r
+\r
+ // Register in structure\r
+ registerInStructure(testResult, treeSRelated, key);\r
+\r
if (contextAware.getValues().containsKey(key)) {\r
Object reachedValue = contextAware.getValues().get(key);\r
\r
- if (expectedValue.equals(reachedValue)) {\r
+ if (expectedValue.equals(contextAware.getContextAnyFlag())) {\r
+ testResult.addResultPart(new SimpleResultPart(\r
+ TestStatus.PASSED, "Expected any value for key '"\r
+ + key + "'"));\r
+ } else if (expectedValue.equals(reachedValue)) {\r
testResult.addResultPart(new SimpleResultPart(\r
TestStatus.PASSED, "Values matched for key '" + key\r
+ "'"));\r
TestStatus.FAILED, "No value reached for key '" + key\r
+ "'"));\r
}\r
+ resetStructure(testResult, treeSRelated);\r
+ }\r
+ }\r
+\r
+ private static void registerInStructure(TestResult testResult,\r
+ TreeSRelated treeSRelated, String key) {\r
+ if (treeSRelated != null) {\r
+ if (treeSRelated.getBasePath() != null) {\r
+ TreeSPath path = treeSRelated.getBasePath().createChild(key);\r
+ StructureRegistry<TreeSPath> registry = treeSRelated\r
+ .getRegistry();\r
+ final StructureElement element = treeSRelated\r
+ .getStructureElement(key);\r
+ registry.register(path, element);\r
+ if (testResult instanceof StructureAware)\r
+ ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(\r
+ registry, path);\r
+\r
+ if (log.isDebugEnabled())\r
+ log.debug("Checking key " + key + " for path " + path);\r
+ }\r
+ }\r
+ }\r
\r
- if (treeSRelated != null) {\r
- if (treeSRelated.getBasePath() != null) {\r
- if (testResult instanceof StructureAware) {\r
- ((StructureAware<TreeSPath>) testResult)\r
- .notifyCurrentPath(treeSRelated.getRegistry(),\r
- treeSRelated.getBasePath());\r
- }\r
+ private static void resetStructure(TestResult testResult,\r
+ TreeSRelated treeSRelated) {\r
+ if (treeSRelated != null) {\r
+ if (treeSRelated.getBasePath() != null) {\r
+ if (testResult instanceof StructureAware) {\r
+ ((StructureAware<TreeSPath>) testResult).notifyCurrentPath(\r
+ treeSRelated.getRegistry(), treeSRelated\r
+ .getBasePath());\r
}\r
}\r
}\r
\r
public interface ParentContextAware extends ContextAware {\r
public Collection<ContextAware> getChildContexts();\r
+ public void addChildContext(ContextAware contextAware);\r
}\r
package org.argeo.slc.core.test.context;\r
\r
-import java.beans.BeanInfo;\r
import java.util.Map;\r
import java.util.TreeMap;\r
\r
import org.springframework.beans.factory.InitializingBean;\r
\r
+import org.argeo.slc.core.SlcException;\r
+\r
public class SimpleContextAware implements ContextAware, InitializingBean {\r
- private SimpleParentContextAware parentContext;\r
+ private ParentContextAware parentContext;\r
\r
private Map<String, Object> values = new TreeMap<String, Object>();\r
private Map<String, Object> expectedValues = new TreeMap<String, Object>();\r
\r
+ private String contextSkipFlag = "!";\r
+ private String contextAnyFlag = "*";\r
+\r
public Map<String, Object> getValues() {\r
return values;\r
}\r
}\r
\r
/** Used to add this context as a child by setting a property. */\r
- public void setParentContext(SimpleParentContextAware parentContextAware) {\r
- parentContextAware.addChildContext(this);\r
+ public void setParentContext(ParentContextAware parentContextAware) {\r
+ if (parentContext != null)\r
+ throw new SlcException("Parent context already set");\r
this.parentContext = parentContextAware;\r
+ this.parentContext.addChildContext(this);\r
}\r
\r
- protected SimpleParentContextAware getParentContext() {\r
+ protected ParentContextAware getParentContext() {\r
return parentContext;\r
}\r
\r
}\r
}\r
\r
+ public String getContextSkipFlag() {\r
+ return contextSkipFlag;\r
+ }\r
+\r
+ public void setContextSkipFlag(String contextSkipFlag) {\r
+ this.contextSkipFlag = contextSkipFlag;\r
+ }\r
+\r
+ public String getContextAnyFlag() {\r
+ return contextAnyFlag;\r
+ }\r
+\r
+ public void setContextAnyFlag(String contextAnyFlag) {\r
+ this.contextAnyFlag = contextAnyFlag;\r
+ }\r
+\r
}\r
// If has a parent, sync it.\r
super.afterPropertiesSet();\r
} else {\r
- ContextUtils.synchronize(this);\r
+ if(children.size()>0){\r
+ // No need to synchronize if no children\r
+ ContextUtils.synchronize(this);\r
+ }\r
}\r
}\r
}\r
--- /dev/null
+package org.argeo.slc.core.test.context;\r
+\r
+import org.argeo.slc.core.test.SimpleTestResult;\r
+import org.argeo.slc.unit.AbstractSpringTestCase;\r
+\r
+public class ContextTest extends AbstractSpringTestCase {\r
+\r
+ public void testComplexContext() {\r
+ SimpleTestResult testResult = new SimpleTestResult();\r
+ ContextUtils.compareReachedExpected(\r
+ (ContextAware) getBean("context.c1"), testResult, null);\r
+ ContextUtils.compareReachedExpected(\r
+ (ContextAware) getBean("context.c2"), testResult, null);\r
+ ContextUtils.compareReachedExpected(\r
+ (ContextAware) getBean("context.c3"), testResult, null);\r
+ }\r
+}\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">\r
+\r
+ <bean id="parentContext.pc1" parent="parentContext.template">\r
+ <property name="values">\r
+ <map>\r
+ <entry key="rootvar" value="text" />\r
+ </map>\r
+ </property>\r
+ <property name="expectedValues">\r
+ <map>\r
+ <entry key="reference" value="20" />\r
+ </map>\r
+ </property>\r
+ </bean>\r
+\r
+ <bean id="context.c1" parent="context.template">\r
+ <property name="parentContext" ref="parentContext.pc1" />\r
+ <property name="values">\r
+ <map>\r
+ <entry key="reference" value="20" />\r
+ <entry key="any" value="999" />\r
+ <entry key="skipped" value="999" />\r
+ </map>\r
+ </property>\r
+ <property name="expectedValues">\r
+ <map>\r
+ <entry key="reference" value="21" />\r
+ <entry key="any" value="*" />\r
+ <entry key="skipped" value="!" />\r
+ </map>\r
+ </property>\r
+ </bean>\r
+\r
+ <bean id="context.c2" parent="context.template">\r
+ <property name="parentContext" ref="parentContext.pc1" />\r
+ <property name="values">\r
+ <map>\r
+ <entry key="var" value="37" />\r
+ <entry key="bad" value="56" />\r
+ </map>\r
+ </property>\r
+ <property name="expectedValues">\r
+ <map>\r
+ <entry key="var" value="37" />\r
+ <entry key="bad" value="57" />\r
+ </map>\r
+ </property>\r
+ </bean>\r
+\r
+ <bean id="parentContext.pc2" parent="parentContext.template">\r
+ <property name="parentContext" ref="parentContext.pc1" />\r
+ <property name="expectedValues">\r
+ <map>\r
+ <entry key="reference" value="40" />\r
+ </map>\r
+ </property>\r
+ </bean>\r
+\r
+ <bean id="context.c3" parent="context.template">\r
+ <property name="parentContext" ref="parentContext.pc2" />\r
+ <property name="values">\r
+ <map>\r
+ <entry key="reference" value="41" />\r
+ <entry key="rootvar" value="textX" />\r
+ </map>\r
+ </property>\r
+ <property name="expectedValues">\r
+ <map>\r
+ <entry key="reference" value="40" />\r
+ <entry key="rootvar" value="text" />\r
+ </map>\r
+ </property>\r
+ </bean>\r
+\r
+\r
+ <bean id="parentContext.template"\r
+ class="org.argeo.slc.core.test.context.SimpleParentContextAware"\r
+ abstract="true">\r
+ </bean>\r
+\r
+ <bean id="context.template"\r
+ class="org.argeo.slc.core.test.context.SimpleContextAware"\r
+ abstract="true">\r
+ </bean>\r
+</beans>
\ No newline at end of file
\r
# A1 uses PatternLayout.\r
log4j.appender.console.layout=org.apache.log4j.PatternLayout\r
-log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c %F%L%n\r
+log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c%n\r
\r