BATCH-1686: Add additional test for item listeners

This commit is contained in:
Dave Syer
2011-02-01 21:17:22 +00:00
parent cf3523f177
commit 2ce298d6c1
3 changed files with 81 additions and 11 deletions

View File

@@ -1,8 +1,9 @@
#Mon Jan 31 13:34:48 GMT 2011
#Tue Feb 01 21:15:00 GMT 2011
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/JobExecutionListenerMethodAttributeParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element type\="job">\n<structure end\="995" endstart\="989" start\="511" startend\="586"/>\n<bounds height\="118" width\="77" x\="15" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/JobRepositoryDefaultParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph/>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerInStepParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element clazz\="JobModelElement" type\="job">\n<structure end\="1832" endstart\="1826" start\="510" startend\="524"/>\n<bounds height\="268" width\="77" x\="15" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerMethodAttributeParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element type\="job">\n<structure end\="854" endstart\="848" start\="511" startend\="525"/>\n<bounds height\="118" width\="77" x\="15" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element clazz\="JobModelElement" type\="job">\n<structure end\="1650" endstart\="1644" start\="510" startend\="524"/>\n<bounds height\="218" width\="77" x\="15" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithPojoListenerJobParserTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element type\="job">\n<structure end\="769" endstart\="763" start\="510" startend\="524"/>\n<bounds height\="118" width\="79" x\="15" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/TaskletParserAdapterTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element type\="job">\n<structure end\="556" endstart\="550" start\="461" startend\="476"/>\n<bounds height\="118" width\="86" x\="15" y\="17"/>\n</element>\n<element type\="job">\n<structure end\="740" endstart\="734" start\="559" startend\="574"/>\n<bounds height\="118" width\="86" x\="113" y\="17"/>\n</element>\n</graph>
//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/batch\:/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/TaskletParserBeanPropertiesTests-context.xml=<?xml version\="1.0" encoding\="UTF-8"?>\n<graph>\n<element type\="job">\n<structure end\="594" endstart\="588" start\="513" startend\="528"/>\n<bounds height\="118" width\="86" x\="15" y\="17"/>\n</element>\n<element type\="job">\n<structure end\="777" endstart\="771" start\="597" startend\="612"/>\n<bounds height\="118" width\="86" x\="113" y\="17"/>\n</element>\n</graph>

View File

@@ -27,6 +27,7 @@ import org.springframework.aop.framework.Advised;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.listener.CompositeStepExecutionListener;
import org.springframework.batch.core.listener.ItemListenerSupport;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,6 +52,10 @@ public class StepListenerParserTests {
@Qualifier("s2")
private Step step2;
@Autowired
@Qualifier("s3")
private Step step3;
@Test
public void testInheritListeners() throws Exception {
@@ -96,6 +101,26 @@ public class StepListenerParserTests {
assertTrue(b);
}
@Test
public void testInheritListenersNoMergeFaultTolerant() throws Exception {
List<?> list = getListeners(step3);
assertEquals(2, list.size());
boolean a = false;
boolean b = false;
for (Object listener : list) {
if (listener instanceof DummyAnnotationStepExecutionListener) {
a = true;
}
else if (listener instanceof ItemListenerSupport) {
b = true;
}
}
assertTrue(a);
assertTrue(b);
}
@SuppressWarnings("unchecked")
private List<?> getListeners(Step step) throws Exception {
assertTrue(step instanceof TaskletStep);
@@ -111,6 +136,32 @@ public class StepListenerParserTests {
}
r.add(listener);
}
compositeListener = ReflectionTestUtils.getField(step, "chunkListener");
composite = ReflectionTestUtils.getField(compositeListener, "listeners");
proxiedListeners = (List<StepExecutionListener>) ReflectionTestUtils.getField(composite, "list");
for (Object listener : proxiedListeners) {
while (listener instanceof Advised) {
listener = ((Advised) listener).getTargetSource().getTarget();
}
r.add(listener);
}
try {
compositeListener = ReflectionTestUtils.getField(
ReflectionTestUtils.getField(ReflectionTestUtils.getField(
ReflectionTestUtils.getField(step, "tasklet"), "chunkProvider"), "listener"),
"itemReadListener");
composite = ReflectionTestUtils.getField(compositeListener, "listeners");
proxiedListeners = (List<StepExecutionListener>) ReflectionTestUtils.getField(composite, "list");
for (Object listener : proxiedListeners) {
while (listener instanceof Advised) {
listener = ((Advised) listener).getTargetSource().getTarget();
}
r.add(listener);
}
}
catch (IllegalArgumentException e) {
// ignore (not a chunk oriented step)
}
return r;
}

View File

@@ -5,43 +5,61 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans:import resource="common-context.xml" />
<job id="job">
<step id="s1" parent="baseStep" next="s2">
<tasklet ref="dummyTasklet">
<listeners merge="true">
<listener>
<beans:bean class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener"/>
<beans:bean class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener" />
</listener>
<listener ref="toplevel1"/>
<listener ref="toplevel1" />
</listeners>
</tasklet>
</step>
<step id="s2" parent="baseStep">
<step id="s2" parent="baseStep" next="s3">
<tasklet ref="dummyTasklet">
<listeners>
<listener>
<beans:bean class="org.springframework.batch.core.listener.StepExecutionListenerSupport"/>
<beans:bean class="org.springframework.batch.core.listener.StepExecutionListenerSupport" />
</listener>
<listener ref="toplevel2"/>
<listener ref="toplevel2" />
</listeners>
</tasklet>
</step>
<step id="s3" parent="baseStep">
<tasklet>
<chunk reader="reader" writer="writer" skip-limit="1">
<skippable-exception-classes>
<include class="java.lang.Exception" />
</skippable-exception-classes>
<listeners>
<listener>
<beans:bean class="org.springframework.batch.core.listener.ItemListenerSupport" />
</listener>
</listeners>
</chunk>
<listeners>
<listener ref="toplevel2" />
</listeners>
</tasklet>
</step>
</job>
<step-listener id="toplevel1">
<beans:bean class="org.springframework.batch.core.listener.StepExecutionListenerSupport"/>
<beans:bean class="org.springframework.batch.core.listener.StepExecutionListenerSupport" />
</step-listener>
<step-listener id="toplevel2">
<beans:bean class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener"/>
<beans:bean class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener" />
</step-listener>
<beans:bean id="baseStep" abstract="true">
<beans:property name="listeners">
<beans:list>
<step-listener>
<beans:bean class="org.springframework.batch.core.listener.CompositeStepExecutionListener"/>
<beans:bean class="org.springframework.batch.core.listener.CompositeStepExecutionListener" />
</step-listener>
</beans:list>
</beans:property>