BATCH-1132: Added "merge" element to <listeners/> defined within <step/> so that they can be added to listeners defined on a parent step.

BATCH-1135: Updated unit tests to test merging of listener lists
This commit is contained in:
dhgarrette
2009-03-12 15:52:34 +00:00
parent a117b3d21c
commit 9729ca23fd
6 changed files with 118 additions and 34 deletions

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.batch.core.configuration.xml;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.core.step.tasklet.TaskletStep;
@@ -125,16 +124,15 @@ public abstract class AbstractStepParser {
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(listenersElement.getTagName(),
parserContext.extractSource(element));
parserContext.pushContainingComponent(compositeDef);
List<Object> listenerBeans = new ArrayList<Object>();
ManagedList listenerBeans = new ManagedList();
listenerBeans.setMergeEnabled(Boolean.valueOf(listenersElement.getAttribute("merge")));
List<Element> listenerElements = DomUtils.getChildElementsByTagName(listenersElement, "listener");
if (listenerElements != null) {
for (Element listenerElement : listenerElements) {
listenerBeans.add(stepListenerParser.parse(listenerElement, parserContext));
}
}
ManagedList arguments = new ManagedList();
arguments.addAll(listenerBeans);
bd.getPropertyValues().addPropertyValue(propertyName, arguments);
bd.getPropertyValues().addPropertyValue(propertyName, listenerBeans);
parserContext.popAndRegisterContainingComponent();
}
}

View File

@@ -594,6 +594,14 @@
<xsd:sequence>
<xsd:element name="listener" type="stepListenerType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="merge" type="xsd:boolean" use="optional" default="false">
<xsd:annotation>
<xsd:documentation>
Should this list of listeners be merged with the list provided
by the parent? If not, it will overwrite the parent list.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:sequence>

View File

@@ -0,0 +1,15 @@
package org.springframework.batch.core.configuration.xml;
import org.springframework.batch.core.annotation.BeforeStep;
/**
* @author Dan Garrette
* @since 2.0
*/
public class DummyAnnotationStepExecutionListener {
@BeforeStep
public void execute() {
}
}

View File

@@ -15,9 +15,11 @@
*/
package org.springframework.batch.core.configuration.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -30,6 +32,7 @@ import org.springframework.batch.core.listener.CompositeStepExecutionListener;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
@@ -43,19 +46,63 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
public class StepListenerParserTests {
@SuppressWarnings("unchecked")
@Test
public void testStepListenerParser() throws Exception {
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml");
List<?> list = getListeners("s1", ctx);
assertEquals(3, list.size());
boolean a = false;
boolean b = false;
boolean c = false;
for (Object listener : list) {
if (listener instanceof DummyAnnotationStepExecutionListener) {
a = true;
}
else if (listener instanceof StepExecutionListenerSupport) {
b = true;
}
else if (listener instanceof CompositeStepExecutionListener) {
c = true;
}
}
assertTrue(a);
assertTrue(b);
assertTrue(c);
}
@Test
public void testStepListenerParserNoMerge() throws Exception {
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/springframework/batch/core/configuration/xml/StepListenerParserNoMergeTests-context.xml");
List<?> list = getListeners("s1", ctx);
assertEquals(2, list.size());
boolean a = false;
boolean b = false;
for (Object listener : list) {
if (listener instanceof DummyAnnotationStepExecutionListener) {
a = true;
}
else if (listener instanceof StepExecutionListenerSupport) {
b = true;
}
}
assertTrue(a);
assertTrue(b);
}
@SuppressWarnings("unchecked")
private List<?> getListeners(String stepName, ApplicationContext ctx) throws Exception {
Map<String, Object> beans = ctx.getBeansOfType(Step.class);
assertTrue(beans.containsKey("s1"));
Step s1 = (Step) ctx.getBean("s1");
assertTrue(s1 instanceof TaskletStep);
assertTrue(beans.containsKey(stepName));
Object step = ctx.getBean(stepName);
assertTrue(step instanceof TaskletStep);
Field listenerField = AbstractStep.class.getDeclaredField("stepExecutionListener");
listenerField.setAccessible(true);
Object compositeListener = listenerField.get(s1);
Object compositeListener = listenerField.get(step);
Field compositeField = CompositeStepExecutionListener.class.getDeclaredField("list");
compositeField.setAccessible(true);
@@ -64,29 +111,15 @@ public class StepListenerParserTests {
Class cls = Class.forName("org.springframework.batch.core.listener.OrderedComposite");
Field listField = cls.getDeclaredField("list");
listField.setAccessible(true);
List<StepExecutionListener> list = (List<StepExecutionListener>) listField.get(composite);
// assertEquals(3, list.size());
boolean a = false;
boolean b = false;
boolean c = false;
for (StepExecutionListener listener : list) {
if (listener instanceof Advised) {
listener = (StepExecutionListener) ((Advised) listener).getTargetSource().getTarget();
}
if (listener instanceof TestListener) {
a = true;
}
if (listener instanceof StepExecutionListenerSupport) {
b = true;
}
if (listener instanceof CompositeStepExecutionListener) {
c = true;
List<StepExecutionListener> proxiedListeners = (List<StepExecutionListener>) listField.get(composite);
List<Object> r = new ArrayList<Object>();
for (Object listener : proxiedListeners) {
while (listener instanceof Advised) {
listener = ((Advised) listener).getTargetSource().getTarget();
}
r.add(listener);
}
assertTrue(a);
assertTrue(b);
// assertTrue(c);
return r;
}
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
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" tasklet="dummyTasklet" parent="baseStep">
<listeners>
<listener class="org.springframework.batch.core.listener.StepExecutionListenerSupport"/>
<listener ref="toplevel"/>
</listeners>
</step>
</job>
<step-listener id="toplevel" class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener"/>
<beans:bean id="baseStep" abstract="true">
<beans:property name="stepExecutionListeners">
<beans:list>
<step-listener class="org.springframework.batch.core.listener.CompositeStepExecutionListener"/>
</beans:list>
</beans:property>
</beans:bean>
</beans:beans>

View File

@@ -8,8 +8,8 @@
<job id="job">
<step id="s1" tasklet="dummyTasklet" parent="baseStep">
<listeners>
<listener class="org.springframework.batch.core.configuration.xml.TestListener"/>
<listeners merge="true">
<listener class="org.springframework.batch.core.configuration.xml.DummyAnnotationStepExecutionListener"/>
<listener ref="toplevel"/>
</listeners>
</step>
@@ -19,7 +19,9 @@
<beans:bean id="baseStep" abstract="true">
<beans:property name="stepExecutionListeners">
<step-listener class="org.springframework.batch.core.listener.CompositeStepExecutionListener"/>
<beans:list>
<step-listener class="org.springframework.batch.core.listener.CompositeStepExecutionListener"/>
</beans:list>
</beans:property>
</beans:bean>