From a28a4c3f765434672e085eca2c514599c045de7a Mon Sep 17 00:00:00 2001 From: dsyer Date: Fri, 9 Apr 2010 09:54:26 +0000 Subject: [PATCH] Add additional test cases for step-scoped proxies --- ...peDestructionCallbackIntegrationTests.java | 22 ++++++++++++++++--- .../StepScopePlaceholderIntegrationTests.java | 20 +++++++++++++++-- .../scope/TestDisposableCollaborator.java | 5 ++--- ...uctionCallbackIntegrationTests-context.xml | 16 ++++++++++++++ ...opePlaceholderIntegrationTests-context.xml | 9 ++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests.java index 00a86b51b..d1739cba9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests.java @@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.StringUtils; @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @@ -27,6 +28,10 @@ public class StepScopeDestructionCallbackIntegrationTests { @Qualifier("nested") private Step nested; + @Autowired + @Qualifier("ref") + private Step ref; + @Autowired @Qualifier("foo") private Collaborator foo; @@ -42,14 +47,14 @@ public class StepScopeDestructionCallbackIntegrationTests { public void testDisposableScopedProxy() throws Exception { assertNotNull(proxied); proxied.execute(new StepExecution("step", new JobExecution(0L), 1L)); - assertEquals("destroyed", TestDisposableCollaborator.message); + assertEquals(1, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "destroyed")); } @Test public void testDisposableInnerScopedProxy() throws Exception { assertNotNull(nested); nested.execute(new StepExecution("step", new JobExecution(0L), 1L)); - assertEquals("destroyed", TestDisposableCollaborator.message); + assertEquals(1, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "destroyed")); } @Test @@ -58,7 +63,18 @@ public class StepScopeDestructionCallbackIntegrationTests { nested.execute(new StepExecution("step", new JobExecution(0L), 1L)); assertEquals(4, TestAdvice.names.size()); assertEquals("bar", TestAdvice.names.get(0)); - assertEquals("destroyed", TestDisposableCollaborator.message); + assertEquals(1, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "destroyed")); + } + + @Test + public void testRefScopedProxy() throws Exception { + assertNotNull(ref); + ref.execute(new StepExecution("step", new JobExecution(0L), 1L)); + assertEquals(4, TestAdvice.names.size()); + assertEquals("spam", TestAdvice.names.get(0)); + assertEquals(2, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "destroyed")); + assertEquals(1, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "bar:destroyed")); + assertEquals(1, StringUtils.countOccurrencesOf(TestDisposableCollaborator.message, "spam:destroyed")); } @Test diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests.java index b710b869e..21e216fbd 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests.java @@ -39,6 +39,10 @@ public class StepScopePlaceholderIntegrationTests implements BeanFactoryAware { @Qualifier("ref") private Collaborator ref; + @Autowired + @Qualifier("scopedRef") + private Collaborator scopedRef; + @Autowired @Qualifier("list") private Collaborator list; @@ -63,12 +67,16 @@ public class StepScopePlaceholderIntegrationTests implements BeanFactoryAware { @Before public void start() { + start("bar"); + } + + private void start(String foo) { StepSynchronizationManager.close(); stepExecution = new StepExecution("foo", new JobExecution(11L), 123L); ExecutionContext executionContext = new ExecutionContext(); - executionContext.put("foo", "bar"); + executionContext.put("foo", foo); executionContext.put("parent", bar); stepExecution.setExecutionContext(executionContext); @@ -79,7 +87,7 @@ public class StepScopePlaceholderIntegrationTests implements BeanFactoryAware { } @After - public void cleanUp() { + public void stop() { StepSynchronizationManager.close(); // Check that all temporary bean definitions are cleaned up assertEquals(beanCount, beanFactory.getBeanDefinitionCount()); @@ -137,4 +145,12 @@ public class StepScopePlaceholderIntegrationTests implements BeanFactoryAware { assertEquals("bar", nested.getParent().getName()); } + @Test + public void testScopedRef() throws Exception { + assertEquals("bar", scopedRef.getParent().getName()); + stop(); + start("spam"); + assertEquals("spam", scopedRef.getParent().getName()); + } + } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/TestDisposableCollaborator.java b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/TestDisposableCollaborator.java index 70596dfd3..7277647da 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/TestDisposableCollaborator.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/TestDisposableCollaborator.java @@ -2,13 +2,12 @@ package org.springframework.batch.core.scope; import org.springframework.beans.factory.DisposableBean; - public class TestDisposableCollaborator extends TestCollaborator implements DisposableBean { - + public static volatile String message = "none"; public void destroy() throws Exception { - message = "destroyed"; + message = (message.equals("none") ? "" : message + ",") + getName() + ":destroyed"; } } diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests-context.xml index 039312d0d..4cd6e7ae7 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeDestructionCallbackIntegrationTests-context.xml @@ -21,6 +21,17 @@ + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests-context.xml index d7e7d6b8c..c8bcf9d8a 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopePlaceholderIntegrationTests-context.xml @@ -42,10 +42,19 @@ + + + + + + + + + \ No newline at end of file