BATCH-2212: Updated CommandLineJobRunner to better communicate exceptions

This commit is contained in:
Michael Minella
2014-05-21 17:49:30 -05:00
parent cd03bc13a7
commit 9affdaf04f
2 changed files with 28 additions and 5 deletions

View File

@@ -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()]);

View File

@@ -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";
}
}
}