BATCH-1918: First effort at @EnableBatchProcessing
Example:
@Configuration
@EnableBatchProcessing
public class RetrySampleConfiguration {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Job retrySample() throws Exception {
return jobs.get("retrySample").start(step()).build();
}
@Bean
protected Step step() throws Exception {
return steps.get("step").<Trade, Object> chunk(1).reader(reader()).writer(writer()).faultTolerant()
.retry(Exception.class).retryLimit(3).build();
}
@Bean
protected ItemReader<Trade> reader() {
GeneratingTradeItemReader reader = new GeneratingTradeItemReader();
reader.setLimit(10);
return reader;
}
@Bean
protected ItemWriter<Object> writer() {
return new RetrySampleItemWriter<Object>();
}
}
This commit is contained in:
committed by
Michael Minella
parent
96de2dc4e9
commit
25cd6d6826
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.core.configuration.annotation;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
|
||||
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@PostConstruct
|
||||
protected void initialize() {
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.addScript(resourceLoader.getResource(ClassUtils.addResourcePathToPackagePath(Step.class, "schema-hsqldb.sql")));
|
||||
populator.setContinueOnError(true);
|
||||
DatabasePopulatorUtils.execute(populator, dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return new EmbeddedDatabaseFactory().getDatabase();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* Copyright 2006-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.core.configuration.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.AbstractStep;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobBuilderConfigurationTests {
|
||||
|
||||
public static boolean fail = false;
|
||||
|
||||
private JobExecution execution;
|
||||
|
||||
@Test
|
||||
public void testVanillaBatchConfiguration() throws Exception {
|
||||
testJob(BatchStatus.COMPLETED, 2, TestConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigurerAsConfiguration() throws Exception {
|
||||
testJob(BatchStatus.COMPLETED, 1, TestConfigurer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigurerAsBean() throws Exception {
|
||||
testJob(BatchStatus.COMPLETED, 1, BeansConfigurer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConfigurations() throws Exception {
|
||||
testJob("testJob", BatchStatus.COMPLETED, 2, TestConfiguration.class, AnotherConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConfigurationsAndConfigurer() throws Exception {
|
||||
testJob("testJob", BatchStatus.COMPLETED, 2, TestConfiguration.class, TestConfigurer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConfigurationsAndBeansConfigurer() throws Exception {
|
||||
testJob("testJob", BatchStatus.COMPLETED, 2, TestConfiguration.class, BeansConfigurer.class);
|
||||
}
|
||||
|
||||
private void testJob(BatchStatus status, int stepExecutionCount, Class<?> config) throws Exception {
|
||||
testJob(null, status, stepExecutionCount, config);
|
||||
}
|
||||
|
||||
private void testJob(String jobName, BatchStatus status, int stepExecutionCount, Class<?>... config)
|
||||
throws Exception {
|
||||
|
||||
Class<?>[] configs = new Class<?>[config.length + 1];
|
||||
System.arraycopy(config, 0, configs, 1, config.length);
|
||||
configs[0] = DataSourceConfiguration.class;
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configs);
|
||||
Job job = jobName == null ? context.getBean(Job.class) : context.getBean(jobName, Job.class);
|
||||
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
||||
execution = jobLauncher
|
||||
.run(job, new JobParametersBuilder().addLong("run.id", (long) (Math.random() * Long.MAX_VALUE))
|
||||
.toJobParameters());
|
||||
assertEquals(status, execution.getStatus());
|
||||
assertEquals(stepExecutionCount, execution.getStepExecutions().size());
|
||||
context.close();
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public static class TestConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
@Bean
|
||||
public Job testJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step1() throws Exception {
|
||||
return steps.get("step1").tasklet(tasklet()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step2() throws Exception {
|
||||
return steps.get("step2").tasklet(tasklet()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Tasklet tasklet() {
|
||||
return new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext context) throws Exception {
|
||||
if (fail) {
|
||||
throw new RuntimeException("Planned!");
|
||||
}
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public static class AnotherConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
@Autowired
|
||||
private Tasklet tasklet;
|
||||
|
||||
@Bean
|
||||
public Job anotherJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.get("another").start(step3());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step3() throws Exception {
|
||||
return steps.get("step3").tasklet(tasklet).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public static class TestConfigurer extends DefaultBatchConfigurer {
|
||||
|
||||
@Autowired
|
||||
private SimpleBatchConfiguration jobs;
|
||||
|
||||
@Bean
|
||||
public Job testConfigererJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.jobBuilders().get("configurer").start(step1());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step1() throws Exception {
|
||||
AbstractStep step = new AbstractStep("step1") {
|
||||
@Override
|
||||
protected void doExecute(StepExecution stepExecution) throws Exception {
|
||||
stepExecution.setExitStatus(ExitStatus.COMPLETED);
|
||||
stepExecution.setStatus(BatchStatus.COMPLETED);
|
||||
}
|
||||
};
|
||||
step.setJobRepository(getJobRepository());
|
||||
return step;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public static class BeansConfigurer {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
@Bean
|
||||
public Job beansConfigurerJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.get("beans").start(step1());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step1() throws Exception {
|
||||
return steps.get("step1").tasklet(new Tasklet() {
|
||||
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
return null;
|
||||
}
|
||||
}).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Autowired
|
||||
protected BatchConfigurer configurer(DataSource dataSource) {
|
||||
return new DefaultBatchConfigurer(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.core.configuration.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.configuration.JobLocator;
|
||||
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
|
||||
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
|
||||
import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory;
|
||||
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobLoaderConfigurationTests {
|
||||
|
||||
private JobExecution execution;
|
||||
|
||||
@Test
|
||||
public void testJobLoader() throws Exception {
|
||||
testJob("test", BatchStatus.COMPLETED, 2, LoaderFactoryConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobLoaderWithArray() throws Exception {
|
||||
testJob("test", BatchStatus.COMPLETED, 2, LoaderRegistrarConfiguration.class);
|
||||
}
|
||||
|
||||
private void testJob(String jobName, BatchStatus status, int stepExecutionCount, Class<?>... config)
|
||||
throws Exception {
|
||||
|
||||
Class<?>[] configs = new Class<?>[config.length + 1];
|
||||
System.arraycopy(config, 0, configs, 1, config.length);
|
||||
configs[0] = DataSourceConfiguration.class;
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configs);
|
||||
Job job = jobName == null ? context.getBean(Job.class) : context.getBean(JobLocator.class).getJob(jobName);
|
||||
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
||||
execution = jobLauncher
|
||||
.run(job, new JobParametersBuilder().addLong("run.id", (long) (Math.random() * Long.MAX_VALUE))
|
||||
.toJobParameters());
|
||||
assertEquals(status, execution.getStatus());
|
||||
assertEquals(stepExecutionCount, execution.getStepExecutions().size());
|
||||
context.close();
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing(modular=true)
|
||||
public static class LoaderFactoryConfiguration {
|
||||
|
||||
@Bean
|
||||
public ApplicationContextFactory jobContextFactory() {
|
||||
return new GenericApplicationContextFactory(TestConfiguration.class);
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationContextFactory vanillaContextFactory() {
|
||||
return new GenericApplicationContextFactory(VanillaConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing(modular=true)
|
||||
public static class LoaderRegistrarConfiguration {
|
||||
|
||||
@Autowired
|
||||
private AutomaticJobRegistrar registrar;
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
registrar.addApplicationContextFactory(new GenericApplicationContextFactory(TestConfiguration.class));
|
||||
registrar.addApplicationContextFactory(new GenericApplicationContextFactory(VanillaConfiguration.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
@Bean
|
||||
public Job testJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step1() throws Exception {
|
||||
return steps.get("step1").tasklet(tasklet()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step2() throws Exception {
|
||||
return steps.get("step2").tasklet(tasklet()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Tasklet tasklet() {
|
||||
return new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext context) throws Exception {
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class VanillaConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
@Bean
|
||||
public Job vanillaJob() throws Exception {
|
||||
SimpleJobBuilder builder = jobs.get("vanilla").start(step3());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
protected Step step3() throws Exception {
|
||||
return steps.get("step3").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext context) throws Exception {
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -190,8 +190,7 @@ public class AutomaticJobRegistrarTests {
|
||||
private void setUpApplicationContextFactories(Resource[] jobPaths, ApplicationContext parent) {
|
||||
Collection<ApplicationContextFactory> applicationContextFactories = new ArrayList<ApplicationContextFactory>();
|
||||
for (Resource resource : jobPaths) {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory();
|
||||
factory.setResource(resource);
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource);
|
||||
factory.setApplicationContext(parent);
|
||||
applicationContextFactories.add(factory);
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class DefaultJobLoaderTests {
|
||||
|
||||
@Test
|
||||
public void testLoadWithExplicitName() throws Exception {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
|
||||
new ByteArrayResource(JOB_XML.getBytes()));
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ByteArrayResource(
|
||||
JOB_XML.getBytes()));
|
||||
jobLoader.load(factory);
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
jobLoader.reload(factory);
|
||||
@@ -48,8 +48,8 @@ public class DefaultJobLoaderTests {
|
||||
|
||||
@Test
|
||||
public void testReload() throws Exception {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
|
||||
new ClassPathResource("trivial-context.xml", getClass()));
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource(
|
||||
"trivial-context.xml", getClass()));
|
||||
jobLoader.load(factory);
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
jobLoader.reload(factory);
|
||||
@@ -58,8 +58,8 @@ public class DefaultJobLoaderTests {
|
||||
|
||||
@Test
|
||||
public void testReloadWithAutoRegister() throws Exception {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
|
||||
new ClassPathResource("trivial-context-autoregister.xml", getClass()));
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource(
|
||||
"trivial-context-autoregister.xml", getClass()));
|
||||
jobLoader.load(factory);
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
jobLoader.reload(factory);
|
||||
@@ -67,9 +67,8 @@ public class DefaultJobLoaderTests {
|
||||
}
|
||||
|
||||
private static final String JOB_XML = String
|
||||
.format(
|
||||
"<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
|
||||
+ "xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd'><bean class='%s$StubJob'/></beans>",
|
||||
.format("<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
|
||||
+ "xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd'><bean class='%s$StubJob'/></beans>",
|
||||
DefaultJobLoaderTests.class.getName());
|
||||
|
||||
public static class StubJob implements Job {
|
||||
|
||||
@@ -32,32 +32,30 @@ import org.springframework.util.ClassUtils;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ClassPathXmlApplicationContextFactoryTests {
|
||||
|
||||
private ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory();
|
||||
public class GenericApplicationContextFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testCreateJob() {
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"trivial-context.xml")));
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(
|
||||
new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
|
||||
ConfigurableApplicationContext context = factory.createApplicationContext();
|
||||
assertNotNull(context);
|
||||
assertTrue("Wrong id: "+context, context.getId().contains("trivial-context.xml"));
|
||||
assertTrue("Wrong id: " + context, context.getId().contains("trivial-context.xml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobName() {
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"trivial-context.xml")));
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(
|
||||
new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
|
||||
assertEquals("test-job", factory.createApplicationContext().getBeanNamesForType(Job.class)[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentConfigurationInherited() {
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(
|
||||
new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
|
||||
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
|
||||
getClass(), "parent-context.xml")));
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"child-context.xml")));
|
||||
ConfigurableApplicationContext context = factory.createApplicationContext();
|
||||
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
|
||||
assertEquals("bar", ((Job) context.getBean("test-job", Job.class)).getName());
|
||||
@@ -66,10 +64,10 @@ public class ClassPathXmlApplicationContextFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testBeanFactoryPostProcessorOrderRespected() {
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(
|
||||
new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "placeholder-context.xml")));
|
||||
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
|
||||
getClass(), "parent-context.xml")));
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"placeholder-context.xml")));
|
||||
ConfigurableApplicationContext context = factory.createApplicationContext();
|
||||
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
|
||||
assertEquals("spam", ((Job) context.getBean("test-job", Job.class)).getName());
|
||||
@@ -77,10 +75,10 @@ public class ClassPathXmlApplicationContextFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testBeanFactoryPostProcessorsNotCopied() {
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(
|
||||
new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
|
||||
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
|
||||
getClass(), "parent-context.xml")));
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"child-context.xml")));
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends BeanFactoryPostProcessor>[] classes = (Class<? extends BeanFactoryPostProcessor>[]) new Class<?>[0];
|
||||
factory.setBeanFactoryPostProcessorClasses(classes);
|
||||
@@ -92,10 +90,10 @@ public class ClassPathXmlApplicationContextFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testBeanFactoryConfigurationNotCopied() {
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"child-context.xml")));
|
||||
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
|
||||
getClass(), "parent-context.xml")));
|
||||
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"child-context.xml")));
|
||||
factory.setCopyConfiguration(false);
|
||||
ConfigurableApplicationContext context = factory.createApplicationContext();
|
||||
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
|
||||
@@ -109,9 +107,8 @@ public class ClassPathXmlApplicationContextFactoryTests {
|
||||
public void testEquals() throws Exception {
|
||||
Resource resource = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"child-context.xml"));
|
||||
factory.setResource(resource);
|
||||
ClassPathXmlApplicationContextFactory other = new ClassPathXmlApplicationContextFactory();
|
||||
other.setResource(resource);
|
||||
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource);
|
||||
GenericApplicationContextFactory other = new GenericApplicationContextFactory(resource);
|
||||
assertEquals(other, factory);
|
||||
assertEquals(other.hashCode(), factory.hashCode());
|
||||
}
|
||||
Reference in New Issue
Block a user