Added unit test to verify JobScope works when using both @EnableBatchProcessing and XML namespace

This commit is contained in:
thebignet
2014-11-28 22:28:44 +01:00
committed by Michael Minella
parent 808121923b
commit ea01ea6152
2 changed files with 61 additions and 4 deletions

View File

@@ -16,28 +16,34 @@
package org.springframework.batch.core.configuration.annotation;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.Callable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.JobSynchronizationManager;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.Callable;
import static org.junit.Assert.assertEquals;
/**
* @author Dave Syer
* @author Michael Minella
@@ -87,6 +93,16 @@ public class JobScopeConfigurationTests {
assertEquals("JOB", value.call());
}
@Test
public void testStepScopeXmlImportUsingNamespace() throws Exception {
init(JobScopeConfigurationXmlImportUsingNamespace.class);
SimpleHolder value = (SimpleHolder) context.getBean("xmlValue");
assertEquals("JOB", value.call());
value = (SimpleHolder) context.getBean("javaValue");
assertEquals("JOB", value.call());
}
@Test
public void testJobScopeWithProxyTargetClassInjected() throws Exception {
init(JobScopeConfigurationInjectingProxy.class);
@@ -201,6 +217,27 @@ public class JobScopeConfigurationTests {
}
public static class TaskletSupport implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
return RepeatStatus.FINISHED;
}
}
@Configuration
@ImportResource("org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsXmlImportUsingNamespace-context.xml")
@EnableBatchProcessing
public static class JobScopeConfigurationXmlImportUsingNamespace {
@Bean
@JobScope
protected SimpleHolder javaValue(@Value("#{jobName}") final String value) {
return new SimpleHolder(value);
}
}
@Configuration
@EnableBatchProcessing
public static class JobScopeConfigurationInjectingProxy {

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<batch:step id="step1">
<batch:tasklet>
<bean class="org.springframework.batch.core.configuration.annotation.JobScopeConfigurationTests$TaskletSupport"/>
</batch:tasklet>
</batch:step>
<bean id="xmlValue" class="org.springframework.batch.core.configuration.annotation.JobScopeConfigurationTests.SimpleHolder" scope="job">
<constructor-arg value="#{jobName}" />
</bean>
</beans>