From 5cd89c0d086e1ed5999096ba358f8fc9352d322d Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 30 Jan 2008 16:36:31 +0000 Subject: [PATCH] Introduce Context Unit Tests. Introduce skip and any values. git-svn-id: https://svn.argeo.org/slc/trunk@935 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../slc/core/test/context/ContextAware.java | 4 + .../slc/core/test/context/ContextUtils.java | 73 +++++++++------ .../core/test/context/ParentContextAware.java | 1 + .../core/test/context/SimpleContextAware.java | 32 +++++-- .../context/SimpleParentContextAware.java | 5 +- .../slc/core/test/context/ContextTest.java | 17 ++++ .../core/test/context/applicationContext.xml | 88 +++++++++++++++++++ .../src/test/resources/log4j.properties | 2 +- 8 files changed, 188 insertions(+), 34 deletions(-) create mode 100644 org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/ContextTest.java create mode 100644 org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/applicationContext.xml diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextAware.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextAware.java index a9b8cb5e7..f882679db 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextAware.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextAware.java @@ -10,4 +10,8 @@ public interface ContextAware { public Map getExpectedValues(); public void setExpectedValues(Map expectedValues); + + public String getContextSkipFlag(); + + public String getContextAnyFlag(); } diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java index f157ac78c..ccf34fdcd 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java @@ -22,32 +22,27 @@ public class ContextUtils { TestResult testResult, TreeSRelated treeSRelated) { for (String key : contextAware.getExpectedValues().keySet()) { - // Register in structure - if (treeSRelated != null) { - if (treeSRelated.getBasePath() != null) { - TreeSPath path = treeSRelated.getBasePath() - .createChild(key); - StructureRegistry registry = treeSRelated - .getRegistry(); - final StructureElement element = treeSRelated - .getStructureElement(key); - registry.register(path, element); - if (testResult instanceof StructureAware) - ((StructureAware) testResult) - .notifyCurrentPath(registry, path); - - if (log.isDebugEnabled()) - log.debug("Checking key " + key + " for path " + path); - } - } - // Compare expected values with reached ones Object expectedValue = contextAware.getExpectedValues().get(key); + if (expectedValue.toString().equals( + contextAware.getContextSkipFlag())) { + if (log.isDebugEnabled()) + log.debug("Skipped check for key '" + key + "'"); + continue; + } + + // Register in structure + registerInStructure(testResult, treeSRelated, key); + if (contextAware.getValues().containsKey(key)) { Object reachedValue = contextAware.getValues().get(key); - if (expectedValue.equals(reachedValue)) { + if (expectedValue.equals(contextAware.getContextAnyFlag())) { + testResult.addResultPart(new SimpleResultPart( + TestStatus.PASSED, "Expected any value for key '" + + key + "'")); + } else if (expectedValue.equals(reachedValue)) { testResult.addResultPart(new SimpleResultPart( TestStatus.PASSED, "Values matched for key '" + key + "'")); @@ -62,14 +57,38 @@ public class ContextUtils { TestStatus.FAILED, "No value reached for key '" + key + "'")); } + resetStructure(testResult, treeSRelated); + } + } + + private static void registerInStructure(TestResult testResult, + TreeSRelated treeSRelated, String key) { + if (treeSRelated != null) { + if (treeSRelated.getBasePath() != null) { + TreeSPath path = treeSRelated.getBasePath().createChild(key); + StructureRegistry registry = treeSRelated + .getRegistry(); + final StructureElement element = treeSRelated + .getStructureElement(key); + registry.register(path, element); + if (testResult instanceof StructureAware) + ((StructureAware) testResult).notifyCurrentPath( + registry, path); + + if (log.isDebugEnabled()) + log.debug("Checking key " + key + " for path " + path); + } + } + } - if (treeSRelated != null) { - if (treeSRelated.getBasePath() != null) { - if (testResult instanceof StructureAware) { - ((StructureAware) testResult) - .notifyCurrentPath(treeSRelated.getRegistry(), - treeSRelated.getBasePath()); - } + private static void resetStructure(TestResult testResult, + TreeSRelated treeSRelated) { + if (treeSRelated != null) { + if (treeSRelated.getBasePath() != null) { + if (testResult instanceof StructureAware) { + ((StructureAware) testResult).notifyCurrentPath( + treeSRelated.getRegistry(), treeSRelated + .getBasePath()); } } } diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ParentContextAware.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ParentContextAware.java index 4022f9427..a8fc8b2be 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ParentContextAware.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ParentContextAware.java @@ -4,4 +4,5 @@ import java.util.Collection; public interface ParentContextAware extends ContextAware { public Collection getChildContexts(); + public void addChildContext(ContextAware contextAware); } diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleContextAware.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleContextAware.java index 9ce69cdee..eee918c64 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleContextAware.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleContextAware.java @@ -1,17 +1,21 @@ package org.argeo.slc.core.test.context; -import java.beans.BeanInfo; import java.util.Map; import java.util.TreeMap; import org.springframework.beans.factory.InitializingBean; +import org.argeo.slc.core.SlcException; + public class SimpleContextAware implements ContextAware, InitializingBean { - private SimpleParentContextAware parentContext; + private ParentContextAware parentContext; private Map values = new TreeMap(); private Map expectedValues = new TreeMap(); + private String contextSkipFlag = "!"; + private String contextAnyFlag = "*"; + public Map getValues() { return values; } @@ -29,12 +33,14 @@ public class SimpleContextAware implements ContextAware, InitializingBean { } /** Used to add this context as a child by setting a property. */ - public void setParentContext(SimpleParentContextAware parentContextAware) { - parentContextAware.addChildContext(this); + public void setParentContext(ParentContextAware parentContextAware) { + if (parentContext != null) + throw new SlcException("Parent context already set"); this.parentContext = parentContextAware; + this.parentContext.addChildContext(this); } - protected SimpleParentContextAware getParentContext() { + protected ParentContextAware getParentContext() { return parentContext; } @@ -44,4 +50,20 @@ public class SimpleContextAware implements ContextAware, InitializingBean { } } + public String getContextSkipFlag() { + return contextSkipFlag; + } + + public void setContextSkipFlag(String contextSkipFlag) { + this.contextSkipFlag = contextSkipFlag; + } + + public String getContextAnyFlag() { + return contextAnyFlag; + } + + public void setContextAnyFlag(String contextAnyFlag) { + this.contextAnyFlag = contextAnyFlag; + } + } diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleParentContextAware.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleParentContextAware.java index 08f54ac87..2dfa12346 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleParentContextAware.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/SimpleParentContextAware.java @@ -24,7 +24,10 @@ public class SimpleParentContextAware extends SimpleContextAware implements // If has a parent, sync it. super.afterPropertiesSet(); } else { - ContextUtils.synchronize(this); + if(children.size()>0){ + // No need to synchronize if no children + ContextUtils.synchronize(this); + } } } } 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 index 000000000..1d1b64fd6 --- /dev/null +++ b/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/ContextTest.java @@ -0,0 +1,17 @@ +package org.argeo.slc.core.test.context; + +import org.argeo.slc.core.test.SimpleTestResult; +import org.argeo.slc.unit.AbstractSpringTestCase; + +public class ContextTest extends AbstractSpringTestCase { + + public void testComplexContext() { + SimpleTestResult testResult = new SimpleTestResult(); + ContextUtils.compareReachedExpected( + (ContextAware) getBean("context.c1"), testResult, null); + ContextUtils.compareReachedExpected( + (ContextAware) getBean("context.c2"), testResult, null); + ContextUtils.compareReachedExpected( + (ContextAware) getBean("context.c3"), testResult, null); + } +} 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 index 000000000..f42523595 --- /dev/null +++ b/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/applicationContext.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.argeo.slc.core/src/test/resources/log4j.properties b/org.argeo.slc.core/src/test/resources/log4j.properties index 6d9a400d7..b92b4cca7 100644 --- a/org.argeo.slc.core/src/test/resources/log4j.properties +++ b/org.argeo.slc.core/src/test/resources/log4j.properties @@ -19,5 +19,5 @@ log4j.appender.console=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.console.layout=org.apache.log4j.PatternLayout -log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c %F%L%n +log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c%n -- 2.39.5