From 2eda8c2aebede85b1e63b6cee34c88246fbdfaa7 Mon Sep 17 00:00:00 2001 From: Chris Schaefer Date: Mon, 21 Oct 2013 14:51:06 -0400 Subject: [PATCH] Catch BeanCreationException on start/start of a job in JsrJobOperator, rethrowing it as a JobStartException --- .../batch/core/jsr/launch/JsrJobOperator.java | 15 +++++++-- .../core/jsr/launch/JsrJobOperatorTests.java | 32 +++++++++++++++++++ ...srJobOperatorTestBeanCreationException.xml | 7 ++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 spring-batch-core/src/test/resources/META-INF/batch-jobs/jsrJobOperatorTestBeanCreationException.xml diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java index b549088f1..90468713e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java @@ -49,6 +49,7 @@ import org.springframework.batch.core.jsr.JsrJobParametersConverter; import org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext; import org.springframework.batch.core.jsr.configuration.support.JobParameterResolvingBeanFactoryPostProcessor; import org.springframework.batch.core.repository.JobRepository; +import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; @@ -454,7 +455,12 @@ public class JsrJobOperator implements JobOperator, InitializingBean { batchContext.addBeanFactoryPostProcessor(new JobParameterResolvingBeanFactoryPostProcessor(params)); batchContext.setParent(baseContext); - batchContext.refresh(); + + try { + batchContext.refresh(); + } catch (BeanCreationException e) { + throw new JobStartException(e); + } final Job job = batchContext.getBean(Job.class); @@ -555,7 +561,12 @@ public class JsrJobOperator implements JobOperator, InitializingBean { batchContext.addBeanFactoryPostProcessor(new JobParameterResolvingBeanFactoryPostProcessor(params)); batchContext.setParent(baseContext); - batchContext.refresh(); + + try { + batchContext.refresh(); + } catch (BeanCreationException e) { + throw new JobStartException(e); + } final Job job = batchContext.getBean(Job.class); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java index e5a62cbb1..0e867b3ea 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -18,6 +19,7 @@ import java.util.Set; import javax.batch.operations.JobExecutionIsRunningException; import javax.batch.operations.JobOperator; import javax.batch.operations.JobRestartException; +import javax.batch.operations.JobStartException; import javax.batch.operations.NoSuchJobException; import javax.batch.operations.NoSuchJobExecutionException; import javax.batch.operations.NoSuchJobInstanceException; @@ -41,6 +43,7 @@ import org.springframework.batch.core.jsr.JsrJobParametersConverter; import org.springframework.batch.core.launch.support.SimpleJobOperator; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.JobRepositorySupport; +import org.springframework.beans.factory.BeanCreationException; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.core.task.SyncTaskExecutor; @@ -452,4 +455,33 @@ public class JsrJobOperatorTests { assertTrue(properties.getProperty("userKey1").equals("userVal1")); assertTrue(properties.getProperty("overrideTest").equals("userProperties")); } + + @Test(expected = JobStartException.class) + public void testBeanCreationExceptionOnStart() throws Exception { + jsrJobOperator = BatchRuntime.getJobOperator(); + + try { + jsrJobOperator.start("jsrJobOperatorTestBeanCreationException", null); + } catch (JobStartException e) { + assertTrue(e.getCause() instanceof BeanCreationException); + throw e; + } + + fail("Should have failed"); + } + + @Test(expected = JobStartException.class) + public void testBeanCreationExceptionOnRestart() throws Exception { + JsrJobOperator jsrJobOperator1 = mock(JsrJobOperator.class); + when(jsrJobOperator1.restart(0l, null)).thenThrow(new JobStartException(new BeanCreationException("Bean creation exception"))); + + try { + jsrJobOperator1.restart(0l, null); + } catch (JobStartException e) { + assertTrue(e.getCause() instanceof BeanCreationException); + throw e; + } + + fail("Should have failed"); + } } diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/jsrJobOperatorTestBeanCreationException.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/jsrJobOperatorTestBeanCreationException.xml new file mode 100644 index 000000000..58d9d2b75 --- /dev/null +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/jsrJobOperatorTestBeanCreationException.xml @@ -0,0 +1,7 @@ + + + + + + +