From 9affdaf04ffaf4fa4dbc6b7c691454f5c600a1b1 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Wed, 21 May 2014 17:49:30 -0500 Subject: [PATCH] BATCH-2212: Updated CommandLineJobRunner to better communicate exceptions --- .../launch/support/CommandLineJobRunner.java | 8 +++--- .../support/CommandLineJobRunnerTests.java | 25 ++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java index a63c8a7c1..c59701ec1 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java @@ -36,7 +36,6 @@ import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.JobParametersNotFoundException; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.repository.JobRepository; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ConfigurableApplicationContext; @@ -286,11 +285,11 @@ public class CommandLineJobRunner { try { try { - context = new ClassPathXmlApplicationContext(jobPath); - } catch (BeansException e) { - logger.info("No XML-based context named " + jobPath + ". Trying class-based configuration."); context = new AnnotationConfigApplicationContext(Class.forName(jobPath)); + } catch (ClassNotFoundException cnfe) { + context = new ClassPathXmlApplicationContext(jobPath); } + context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); @@ -583,6 +582,7 @@ public class CommandLineJobRunner { logger.error(message); CommandLineJobRunner.message = message; command.exit(1); + return; } String[] parameters = params.toArray(new String[params.size()]); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java index a4011a287..4f66cf220 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java @@ -33,6 +33,8 @@ import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.step.JobRepositorySupport; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.util.ClassUtils; import java.io.IOException; @@ -118,7 +120,7 @@ public class CommandLineJobRunnerTests { CommandLineJobRunner.main(args); assertEquals(1, StubSystemExiter.status); String errorMessage = CommandLineJobRunner.getErrorMessage(); - assertTrue("Wrong error message: " + errorMessage, errorMessage.contains("Config locations must not be null")); + assertTrue("Wrong error message: " + errorMessage, errorMessage.contains("At least 2 arguments are required: JobPath/JobClass and jobIdentifier.")); } @Test @@ -355,6 +357,19 @@ public class CommandLineJobRunnerTests { assertTrue(StubJobLauncher.destroyed); } + @Test + public void testJavaConfig() throws Exception { + String[] args = + new String[] { "org.springframework.batch.core.launch.support.CommandLineJobRunnerTests$Configuration1", + "invalidJobName"}; + CommandLineJobRunner.presetSystemExiter(new StubSystemExiter()); + CommandLineJobRunner.main(args); + assertEquals(1, StubSystemExiter.status); + String errorMessage = CommandLineJobRunner.getErrorMessage(); + assertTrue("Wrong error message: " + errorMessage, + errorMessage.contains("A JobLauncher must be provided. Please add one to the configuration.")); + } + public static class StubSystemExiter implements SystemExiter { private static int status; @@ -530,4 +545,12 @@ public class CommandLineJobRunnerTests { } + @Configuration + public static class Configuration1 { + @Bean + public String bean1() { + return "bean1"; + } + } + }