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

@@ -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;
}
}