From 7531c5acfb163e19a7eb5b3320747ada7b4f24c8 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 21 May 2013 09:26:01 +0100 Subject: [PATCH] [bs-22] Add test for Batch auto config [#48127729] [bs-22] Add missing unit tests --- .../batch/BatchAutoConfigurationTests.java | 105 ++++++++++++++++++ .../JobExecutionExitCodeGeneratorTests.java | 54 +++++++++ 2 files changed, 159 insertions(+) create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfigurationTests.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/JobExecutionExitCodeGeneratorTests.java diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfigurationTests.java new file mode 100644 index 00000000000..62fd406f7fb --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -0,0 +1,105 @@ +/* + * 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.bootstrap.autoconfigure.batch; + +import java.util.Collection; +import java.util.Collections; + +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.JobExecutionException; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.job.AbstractJob; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.bootstrap.autoconfigure.jdbc.EmbeddedDatabaseConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; + +import static org.junit.Assert.assertNotNull; + +/** + * @author Dave Syer + * + */ +public class BatchAutoConfigurationTests { + + private AnnotationConfigApplicationContext context; + + @Test + public void testDefaultContext() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(TestConfiguration.class, BatchAutoConfiguration.class, + EmbeddedDatabaseConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertNotNull(this.context.getBean(JobLauncher.class)); + } + + @Test + public void testDefinesAndLaunchesJob() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(JobConfiguration.class, BatchAutoConfiguration.class, + EmbeddedDatabaseConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertNotNull(this.context.getBean(JobLauncher.class)); + this.context.getBean(JobLauncherCommandLineRunner.class).run(); + assertNotNull(this.context.getBean(JobRepository.class).getLastJobExecution( + "job", new JobParameters())); + } + + @EnableBatchProcessing + protected static class TestConfiguration { + } + + @EnableBatchProcessing + protected static class JobConfiguration { + @Autowired + private JobRepository jobRepository; + + @Bean + public Job job() { + AbstractJob job = new AbstractJob() { + + @Override + public Collection getStepNames() { + return Collections.emptySet(); + } + + @Override + public Step getStep(String stepName) { + return null; + } + + @Override + protected void doExecute(JobExecution execution) + throws JobExecutionException { + execution.setStatus(BatchStatus.COMPLETED); + } + }; + job.setJobRepository(this.jobRepository); + return job; + } + } + +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/JobExecutionExitCodeGeneratorTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/JobExecutionExitCodeGeneratorTests.java new file mode 100644 index 00000000000..06c74837e0b --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/batch/JobExecutionExitCodeGeneratorTests.java @@ -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.bootstrap.autoconfigure.batch; + +import org.junit.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.JobExecution; + +import static org.junit.Assert.assertEquals; + +/** + * @author Dave Syer + * + */ +public class JobExecutionExitCodeGeneratorTests { + + private JobExecutionExitCodeGenerator generator = new JobExecutionExitCodeGenerator(); + + @Test + public void testExitCodeForRunning() { + this.generator.onApplicationEvent(new JobExecutionEvent(new JobExecution(0L))); + assertEquals(1, this.generator.getExitCode()); + } + + @Test + public void testExitCodeForCompleted() { + JobExecution execution = new JobExecution(0L); + execution.setStatus(BatchStatus.COMPLETED); + this.generator.onApplicationEvent(new JobExecutionEvent(execution)); + assertEquals(0, this.generator.getExitCode()); + } + + @Test + public void testExitCodeForFailed() { + JobExecution execution = new JobExecution(0L); + execution.setStatus(BatchStatus.FAILED); + this.generator.onApplicationEvent(new JobExecutionEvent(execution)); + assertEquals(5, this.generator.getExitCode()); + } + +}