]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce Context Unit Tests.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 30 Jan 2008 16:36:31 +0000 (16:36 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 30 Jan 2008 16:36:31 +0000 (16:36 +0000)
Introduce skip and any values.

git-svn-id: https://svn.argeo.org/slc/trunk@935 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextAware.java
org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java
org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ParentContextAware.java
org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleContextAware.java
org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleParentContextAware.java
org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/ContextTest.java [new file with mode: 0644]
org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/applicationContext.xml [new file with mode: 0644]
org.argeo.slc.core/src/test/resources/log4j.properties

index a9b8cb5e7481a9899b60dd07adf76ca7756117ba..f882679db2d0e87a6d5ecf62341272a6bafd9a03 100644 (file)
@@ -10,4 +10,8 @@ public interface ContextAware {
        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
index f157ac78c17eacf2bfca95aebbc94c035d9ca7e9..ccf34fdcd937064dcec734220fb4c8329ab366c7 100644 (file)
@@ -22,32 +22,27 @@ public class ContextUtils {
                        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
@@ -62,14 +57,38 @@ public class ContextUtils {
                                                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
index 4022f94275586188f313cb61be7c2e28c7467382..a8fc8b2bed1f22ccf5634eab265c92ff4e42719f 100644 (file)
@@ -4,4 +4,5 @@ import java.util.Collection;
 \r
 public interface ParentContextAware extends ContextAware {\r
        public Collection<ContextAware> getChildContexts();\r
+       public void addChildContext(ContextAware contextAware);\r
 }\r
index 9ce69cdee1e83ae5121a44fad632aca377e962b5..eee918c64509103df3ed0f8266246bee55167cd5 100644 (file)
@@ -1,17 +1,21 @@
 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
@@ -29,12 +33,14 @@ public class SimpleContextAware implements ContextAware, InitializingBean {
        }\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
@@ -44,4 +50,20 @@ public class SimpleContextAware implements ContextAware, InitializingBean {
                }\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
index 08f54ac8763146bf92c1a19d78ad46a35268b6bd..2dfa12346382ec99a740530f020adcadd65f41fa 100644 (file)
@@ -24,7 +24,10 @@ public class SimpleParentContextAware extends SimpleContextAware implements
                        // 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
diff --git a/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/ContextTest.java b/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/ContextTest.java
new file mode 100644 (file)
index 0000000..1d1b64f
--- /dev/null
@@ -0,0 +1,17 @@
+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
diff --git a/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/applicationContext.xml b/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/applicationContext.xml
new file mode 100644 (file)
index 0000000..f425235
--- /dev/null
@@ -0,0 +1,88 @@
+<?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
index 6d9a400d7a53e7ba82f057615d5e891f4300195b..b92b4cca706aee6a4cdaea54bcbf9f44b17230cb 100644 (file)
@@ -19,5 +19,5 @@ log4j.appender.console=org.apache.log4j.ConsoleAppender
 \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