diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobParserTests.java index 3d0e3a9d8..afb9f2f9d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobParserTests.java @@ -18,7 +18,6 @@ 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; @@ -28,10 +27,11 @@ import org.springframework.aop.framework.Advised; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecutionListener; import org.springframework.batch.core.job.AbstractJob; -import org.springframework.batch.core.listener.CompositeJobExecutionListener; import org.springframework.batch.core.listener.JobExecutionListenerSupport; +import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.util.ReflectionTestUtils; /** * @author Dan Garrette @@ -39,16 +39,11 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; */ public class JobParserTests { - @SuppressWarnings("unchecked") @Test - public void testJobParserParentAttribute() throws Exception { + public void testInheritListeners() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/xml/JobParserParentAttributeTests-context.xml"); - Map beans = ctx.getBeansOfType(Job.class); - - assertTrue(beans.containsKey("job1")); - Job job1 = (Job) ctx.getBean("job1"); - List job1Listeners = getListeners(job1); + List job1Listeners = getListeners("job1", ctx); assertEquals(2, job1Listeners.size()); boolean a = false; boolean b = false; @@ -62,10 +57,13 @@ public class JobParserTests { } assertTrue(a); assertTrue(b); + } - assertTrue(beans.containsKey("job2")); - Job job2 = (Job) ctx.getBean("job2"); - List job2Listeners = getListeners(job2); + @Test + public void testInheritListeners_NoMerge() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "org/springframework/batch/core/configuration/xml/JobParserParentAttributeTests-context.xml"); + List job2Listeners = getListeners("job2", ctx); assertEquals(1, job2Listeners.size()); boolean c = false; for (Object l : job2Listeners) { @@ -77,20 +75,15 @@ public class JobParserTests { } @SuppressWarnings("unchecked") - private List getListeners(Job job) throws Exception { + private List getListeners(String jobName, ApplicationContext ctx) throws Exception { + Map beans = ctx.getBeansOfType(Job.class); + assertTrue(beans.containsKey(jobName)); + Job job = (Job) ctx.getBean(jobName); + assertTrue(job instanceof AbstractJob); - Field listenerField = AbstractJob.class.getDeclaredField("listener"); - listenerField.setAccessible(true); - Object compositeListener = listenerField.get(job); - - Field compositeField = CompositeJobExecutionListener.class.getDeclaredField("listeners"); - compositeField.setAccessible(true); - Object composite = compositeField.get(compositeListener); - - Class cls = Class.forName("org.springframework.batch.core.listener.OrderedComposite"); - Field listField = cls.getDeclaredField("list"); - listField.setAccessible(true); - List list = (List) listField.get(composite); + Object compositeListener = ReflectionTestUtils.getField(job, "listener"); + Object composite = ReflectionTestUtils.getField(compositeListener, "listeners"); + List list = (List) ReflectionTestUtils.getField(composite, "list"); List listeners = new ArrayList(); for (Object listener : list) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepListenerParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepListenerParserTests.java index 9b60ca4a2..b9a199c1c 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepListenerParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepListenerParserTests.java @@ -18,36 +18,30 @@ 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; import org.junit.Test; -import org.junit.runner.RunWith; 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.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; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.util.ReflectionTestUtils; /** * @author Dan Garrette * @since 2.0 */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) public class StepListenerParserTests { @Test - public void testStepListenerParser() throws Exception { + public void testInheritListeners() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml"); List list = getListeners("s1", ctx); @@ -73,10 +67,10 @@ public class StepListenerParserTests { } @Test - public void testStepListenerParserNoMerge() throws Exception { + public void testInheritListeners_NoMerge() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( - "org/springframework/batch/core/configuration/xml/StepListenerParserNoMergeTests-context.xml"); - List list = getListeners("s1", ctx); + "org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml"); + List list = getListeners("s2", ctx); assertEquals(2, list.size()); boolean a = false; @@ -100,18 +94,10 @@ public class StepListenerParserTests { Object step = ctx.getBean(stepName); assertTrue(step instanceof TaskletStep); - Field listenerField = AbstractStep.class.getDeclaredField("stepExecutionListener"); - listenerField.setAccessible(true); - Object compositeListener = listenerField.get(step); - - Field compositeField = CompositeStepExecutionListener.class.getDeclaredField("list"); - compositeField.setAccessible(true); - Object composite = compositeField.get(compositeListener); - - Class cls = Class.forName("org.springframework.batch.core.listener.OrderedComposite"); - Field listField = cls.getDeclaredField("list"); - listField.setAccessible(true); - List proxiedListeners = (List) listField.get(composite); + Object compositeListener = ReflectionTestUtils.getField(step, "stepExecutionListener"); + Object composite = ReflectionTestUtils.getField(compositeListener, "list"); + List proxiedListeners = (List) ReflectionTestUtils.getField( + composite, "list"); List r = new ArrayList(); for (Object listener : proxiedListeners) { while (listener instanceof Advised) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java index 726dbc552..0c262cf83 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.lang.reflect.Field; import java.util.List; import java.util.Map; @@ -27,22 +26,15 @@ import org.junit.Test; 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.StepExecutionListenerSupport; -import org.springframework.batch.core.step.AbstractStep; -import org.springframework.batch.core.step.item.ChunkOrientedTasklet; -import org.springframework.batch.core.step.item.ChunkProvider; import org.springframework.batch.core.step.item.FaultTolerantStepFactoryBean; -import org.springframework.batch.core.step.item.SimpleChunkProvider; -import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.core.step.tasklet.TaskletStep; import org.springframework.batch.repeat.CompletionPolicy; -import org.springframework.batch.repeat.RepeatOperations; import org.springframework.batch.repeat.policy.SimpleCompletionPolicy; -import org.springframework.batch.repeat.support.RepeatTemplate; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.util.ReflectionTestUtils; /** * @author Thomas Risberg @@ -89,9 +81,7 @@ public class StepParserTests { Step s1 = (Step) ctx.getBean("s1"); CompletionPolicy completionPolicy = getCompletionPolicy(s1); assertTrue(completionPolicy instanceof SimpleCompletionPolicy); - Field chunkSizeField = SimpleCompletionPolicy.class.getDeclaredField("chunkSize"); - chunkSizeField.setAccessible(true); - assertEquals(25, chunkSizeField.get(completionPolicy)); + assertEquals(25, ReflectionTestUtils.getField(completionPolicy, "chunkSize")); } @SuppressWarnings("unchecked") @@ -106,20 +96,11 @@ public class StepParserTests { assertTrue(completionPolicy instanceof DummyCompletionPolicy); } - @SuppressWarnings("unchecked") private CompletionPolicy getCompletionPolicy(Step s1) throws NoSuchFieldException, IllegalAccessException { - Field taskletField = TaskletStep.class.getDeclaredField("tasklet"); - taskletField.setAccessible(true); - Tasklet tasklet = (Tasklet) taskletField.get(s1); - Field chunkProviderField = ChunkOrientedTasklet.class.getDeclaredField("chunkProvider"); - chunkProviderField.setAccessible(true); - ChunkProvider chunkProvider = (ChunkProvider) chunkProviderField.get(tasklet); - Field repeatOperationsField = SimpleChunkProvider.class.getDeclaredField("repeatOperations"); - repeatOperationsField.setAccessible(true); - RepeatOperations repeatOperations = (RepeatOperations) repeatOperationsField.get(chunkProvider); - Field completionPolicyField = RepeatTemplate.class.getDeclaredField("completionPolicy"); - completionPolicyField.setAccessible(true); - return (CompletionPolicy) completionPolicyField.get(repeatOperations); + Object tasklet = ReflectionTestUtils.getField(s1, "tasklet"); + Object chunkProvider = ReflectionTestUtils.getField(tasklet, "chunkProvider"); + Object repeatOperations = ReflectionTestUtils.getField(chunkProvider, "repeatOperations"); + return (CompletionPolicy) ReflectionTestUtils.getField(repeatOperations, "completionPolicy"); } @Test(expected = BeanDefinitionParsingException.class) @@ -142,8 +123,7 @@ public class StepParserTests { try { new ClassPathXmlApplicationContext(contextLocation); fail("Context should not load!"); - } - catch (BeanDefinitionParsingException e) { + } catch (BeanDefinitionParsingException e) { assertTrue(e.getMessage().contains("'ref' and 'class'")); } } @@ -156,55 +136,65 @@ public class StepParserTests { @SuppressWarnings("unchecked") @Test - public void testStepParserParentAttribute() throws Exception { + public void testParentOnInlineStep() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/xml/StepParserParentAttributeTests-context.xml"); Map beans = ctx.getBeansOfType(Step.class); assertTrue(beans.containsKey("s1")); Step s1 = (Step) ctx.getBean("s1"); - assertTrue(beans.containsKey("s2")); - Step s2 = (Step) ctx.getBean("s2"); - assertTrue(beans.containsKey("s3")); - Step s3 = (Step) ctx.getBean("s3"); - assertTrue(beans.containsKey("s4")); - Step s4 = (Step) ctx.getBean("s4"); - assertTrue(s1 instanceof TaskletStep); assertTrue(getListener((TaskletStep) s1) instanceof StepExecutionListenerSupport); + } + @SuppressWarnings("unchecked") + @Test + public void testParentOnStandaloneStep() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "org/springframework/batch/core/configuration/xml/StepParserParentAttributeTests-context.xml"); + Map beans = ctx.getBeansOfType(Step.class); + assertTrue(beans.containsKey("s2")); + Step s2 = (Step) ctx.getBean("s2"); assertTrue(s2 instanceof DelegatingStep); assertTrue(getListener((DelegatingStep) s2) instanceof StepExecutionListenerSupport); + } + @SuppressWarnings("unchecked") + @Test + public void testParentOnInlineWithTaskletAttributeStep() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "org/springframework/batch/core/configuration/xml/StepParserParentAttributeTests-context.xml"); + Map beans = ctx.getBeansOfType(Step.class); + assertTrue(beans.containsKey("s3")); + Step s3 = (Step) ctx.getBean("s3"); assertTrue(s3 instanceof TaskletStep); assertTrue(getListener((TaskletStep) s3) instanceof StepExecutionListenerSupport); + } + @SuppressWarnings("unchecked") + @Test + public void testParentOnStandaloneWithTaskletAttributeStep() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "org/springframework/batch/core/configuration/xml/StepParserParentAttributeTests-context.xml"); + Map beans = ctx.getBeansOfType(Step.class); + assertTrue(beans.containsKey("s4")); + Step s4 = (Step) ctx.getBean("s4"); assertTrue(s4 instanceof DelegatingStep); assertTrue(getListener((DelegatingStep) s4) instanceof StepExecutionListenerSupport); } private StepExecutionListener getListener(DelegatingStep step) throws Exception { assertTrue(step instanceof DelegatingStep); - Field delegateField = DelegatingStep.class.getDeclaredField("delegate"); - delegateField.setAccessible(true); - Object delegate = delegateField.get(step); + Object delegate = ReflectionTestUtils.getField(step, "delegate"); assertTrue(delegate instanceof TaskletStep); return getListener((TaskletStep) delegate); } @SuppressWarnings("unchecked") private StepExecutionListener getListener(TaskletStep step) throws Exception { - Field listenerField = AbstractStep.class.getDeclaredField("stepExecutionListener"); - listenerField.setAccessible(true); - Object compositeListener = listenerField.get(step); - - Field compositeField = CompositeStepExecutionListener.class.getDeclaredField("list"); - compositeField.setAccessible(true); - Object composite = compositeField.get(compositeListener); - - Class cls = Class.forName("org.springframework.batch.core.listener.OrderedComposite"); - Field listField = cls.getDeclaredField("list"); - listField.setAccessible(true); - List list = (List) listField.get(composite); + Object compositeListener = ReflectionTestUtils.getField(step, "stepExecutionListener"); + Object composite = ReflectionTestUtils.getField(compositeListener, "list"); + List list = (List) ReflectionTestUtils + .getField(composite, "list"); assertEquals(1, list.size()); StepExecutionListener listener = list.get(0); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TaskletElementParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TaskletElementParserTests.java index 9b5d1cd5b..1fa32b6f8 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TaskletElementParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TaskletElementParserTests.java @@ -24,8 +24,6 @@ import java.util.Map; import java.util.Set; import org.junit.Test; -import org.junit.internal.runners.JUnit4ClassRunner; -import org.junit.runner.RunWith; import org.springframework.batch.core.Step; import org.springframework.batch.core.step.tasklet.TaskletStep; import org.springframework.batch.item.ItemStream; @@ -43,7 +41,6 @@ import org.springframework.test.util.ReflectionTestUtils; * @author Dan Garrette * @since 2.0 */ -@RunWith(JUnit4ClassRunner.class) public class TaskletElementParserTests { @Test diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserNoMergeTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserNoMergeTests-context.xml deleted file mode 100644 index c5b6662df..000000000 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserNoMergeTests-context.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml index 1fdd89c24..7b8f6685b 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepListenerParserTests-context.xml @@ -10,12 +10,20 @@ - + + + + + + + + - + +