IN PROGRESS - issue BATCH-891: Create Annotations

http://jira.springframework.org/browse/BATCH-891

Added a ScopeMetaDataResolver for @BatchComponent.
This commit is contained in:
lucasward
2008-11-05 05:43:23 +00:00
parent 7fdfe6bd34
commit 9238319b73
13 changed files with 311 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
/**
*
*/
package org.springframework.batch.core.annotation;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Lucas Ward
*
*/
public class BatchComponentIntegrationTests {
@Test
public void testWithoutResolver(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context.xml");
String[] beanNames = context.getBeanNamesForType(TestComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals(BeanDefinition.SCOPE_SINGLETON, beanDef.getScope());
}
@Test
public void testWithResolver(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context-with-resolver.xml");
String[] beanNames = context.getBeanNamesForType(TestComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals("step", beanDef.getScope());
}
@Test
public void testWithResolverNonScanned(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context-with-resolver-nonscanned.xml");
String[] beanNames = context.getBeanNamesForType(TestComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals("step", beanDef.getScope());
}
@Test
public void testWithScopedComponent(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context-scoped.xml");
String[] beanNames = context.getBeanNamesForType(TestScopedComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals("step", beanDef.getScope());
}
@Test
public void testWithPostProcessor(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context-postprocessor.xml");
String[] beanNames = context.getBeanNamesForType(TestComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals("step", beanDef.getScope());
}
@Test
public void testWithPostProcessorScanned(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/batch/core/annotation/batch-component-context-postprocessor-scanned.xml");
String[] beanNames = context.getBeanNamesForType(TestComponent.class);
//There should be one instance of TextComponent in the Spring Container, because it's annotated with BatchComponent, and the filter
//is setup. However, the beanDefinition, should not have the scope on it, because there is no proxy set.
assertEquals(1, beanNames.length);
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory)context.getAutowireCapableBeanFactory();
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNames[0]);
//Even though BatchComponent is annotated with StepScope, the scope will still be singleton without the resolver.
assertEquals("step", beanDef.getScope());
}
}

View File

@@ -0,0 +1,51 @@
/**
*
*/
package org.springframework.batch.core.annotation;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
/**
* @author Lucas Ward
*
*/
public class BatchComponentScopeMetaDataResolverTests {
AnnotatedBeanDefinition annBeanDef;
AnnotationMetadata metaData;
BatchComponentScopeMetaDataResolver resolver = new BatchComponentScopeMetaDataResolver();
@Before
public void init(){
annBeanDef = createMock(AnnotatedBeanDefinition.class);
}
@Test
public void testNormalCase(){
expect(annBeanDef.getMetadata()).andReturn(new StandardAnnotationMetadata(StubScopedClass.class));
expect(annBeanDef.getMetadata()).andReturn(new StandardAnnotationMetadata(StubScopedClass.class));
replay(annBeanDef);
assertEquals("step",resolver.resolveScopeMetadata(annBeanDef).getScopeName());
verify(annBeanDef);
}
@Test
public void testBatchComponent(){
expect(annBeanDef.getMetadata()).andReturn(new StandardAnnotationMetadata(TestComponent.class));
replay(annBeanDef);
assertEquals("step",resolver.resolveScopeMetadata(annBeanDef).getScopeName());
verify(annBeanDef);
}
@Scope("step")
private class StubScopedClass{}
}

View File

@@ -0,0 +1,13 @@
/**
*
*/
package org.springframework.batch.core.annotation;
/**
* @author Lucas Ward
*
*/
@BatchComponent
public class TestComponent {
}

View File

@@ -0,0 +1,8 @@
package org.springframework.batch.core.annotation;
import org.springframework.context.annotation.Scope;
@Scope("step")
public class TestScopedComponent {
}

View File

@@ -13,7 +13,7 @@ public class TestStep implements Step {
public void setCollaborator(Collaborator collaborator) {
this.collaborator = collaborator;
}
public static StepContext getContext() {
return context;
}