From fc49245664cb0f92e998cb2b23058569a6074035 Mon Sep 17 00:00:00 2001 From: lucasward Date: Fri, 5 Sep 2008 16:40:34 +0000 Subject: [PATCH] OPEN - issue BATCH-811: StepExecutionResourceProxy should throw an exception if a job paramter key in the path isn't found http://jira.springframework.org/browse/BATCH-811 StepExecutionResourceProxy will now throw an Exception if any "%" signs are left after pattern matching. --- .../resource/StepExecutionResourceProxy.java | 9 ++++++++- .../StepExecutionResourceProxyTests.java | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionResourceProxy.java b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionResourceProxy.java index 7e58f0d41..ebd7735f5 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionResourceProxy.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionResourceProxy.java @@ -284,7 +284,14 @@ public class StepExecutionResourceProxy extends StepExecutionListenerSupport imp String jobName = execution.getJobExecution().getJobInstance().getJobName(); Properties properties = jobParametersConverter.getProperties(execution.getJobExecution().getJobInstance() .getJobParameters()); - delegate = resourceLoader.getResource(createFileName(jobName, stepName, properties)); + String fileName = createFileName(jobName, stepName, properties); + if(fileName.indexOf("%") > -1){ + //if a % is still left in the fileName after matching, we have to assume that either no job parameter was found, + //or an invalid path was used. + throw new IllegalStateException("Invalid file pattern provided: [" + this.filePattern + "], tokens still remain after parameter matching: [" + + fileName + "]"); + } + delegate = resourceLoader.getResource(fileName); } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java index 88effa843..beaa951b4 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java @@ -141,6 +141,22 @@ public class StepExecutionResourceProxyTests extends TestCase { resource.setFilePattern("arbitrary pattern"); assertEquals(filePattern, resource.toString()); } + + public void testNonExistentJobParameter() throws Exception{ + + resource.setFilePattern("foo/data/%JOB_NAME%/%non.key%-foo"); + jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam") + .toJobParameters(), "testJob"); + JobExecution jobExecution = new JobExecution(jobInstance); + Step step = new StepSupport("bar"); + try{ + resource.beforeStep(jobExecution.createStepExecution(step)); + fail(); + } + catch(Exception ex){ + //expected, if there isn't a JobParameter for that key, it should throw an exception + } + } private void doTestPathName(String filename, String path) throws Exception, IOException { String returnedPath = resource.getFile().getAbsolutePath();