RESOLVED BATCH-939: Make step scope work with aop-scoped-proxy

This commit is contained in:
dsyer
2008-11-24 08:24:51 +00:00
parent 925e7d1e16
commit fb9b662160
17 changed files with 319 additions and 84 deletions

View File

@@ -0,0 +1,72 @@
package org.springframework.batch.core.scope;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
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;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeDestructionCallbackIntegrationTests {
@Autowired
@Qualifier("proxied")
private Step proxied;
@Autowired
@Qualifier("nested")
private Step nested;
@Autowired
@Qualifier("foo")
private Collaborator foo;
@Before
@After
public void resetMessage() throws Exception {
TestDisposableCollaborator.message = "none";
TestAdvice.names.clear();
}
@Test
public void testDisposableScopedProxy() throws Exception {
assertNotNull(proxied);
proxied.execute(new StepExecution("step", new JobExecution(0L), 1L));
assertEquals("destroyed", TestDisposableCollaborator.message);
}
@Test
public void testDisposableInnerScopedProxy() throws Exception {
assertNotNull(nested);
nested.execute(new StepExecution("step", new JobExecution(0L), 1L));
assertEquals("destroyed", TestDisposableCollaborator.message);
}
@Test
public void testProxiedScopedProxy() throws Exception {
assertNotNull(nested);
nested.execute(new StepExecution("step", new JobExecution(0L), 1L));
assertEquals(2, TestAdvice.names.size());
assertEquals("bar", TestAdvice.names.get(0));
assertEquals("destroyed", TestDisposableCollaborator.message);
}
@Test
public void testProxiedNormalBean() throws Exception {
assertNotNull(nested);
String name = foo.getName();
assertEquals(1, TestAdvice.names.size());
assertEquals(name, TestAdvice.names.get(0));
}
}

View File

@@ -0,0 +1,33 @@
package org.springframework.batch.core.scope;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Step;
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;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeNestedIntegrationTests {
@Autowired
@Qualifier("proxied")
private Step proxied;
@Autowired
@Qualifier("parent")
private Collaborator parent;
@Test
public void testNestedScopedProxy() throws Exception {
assertNotNull(proxied);
assertEquals("foo", parent.getName());
}
}

View File

@@ -0,0 +1,20 @@
package org.springframework.batch.core.scope;
import java.util.ArrayList;
import java.util.List;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class TestAdvice {
public static List<String> names = new ArrayList<String>();
@AfterReturning(pointcut="execution(String org.springframework.batch.core.scope.Collaborator+.getName(..))", returning="name")
public void registerCollaborator(String name) {
names.add(name);
}
}

View File

@@ -17,9 +17,6 @@ public class TestCollaborator implements Collaborator, Serializable {
this.parent = parent;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.scope.Collaborator#getName()
*/
public String getName() {
return name;
}

View File

@@ -0,0 +1,14 @@
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";
}
}

View File

@@ -31,7 +31,8 @@ public class TestStep implements Step {
private void setContextFromCollaborator() {
if (context != null) {
context.setAttribute("collaborator", collaborator.getName());
String name = collaborator.getName();
context.setAttribute("collaborator", name);
}
}