From 4444e465a65381a57978f1bbd86350d07317f816 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Tue, 18 May 2021 12:56:58 -0400 Subject: [PATCH] Add URL to error message in StaxEventItemReader when resource does not exist Resolves #1171 --- .../batch/item/xml/StaxEventItemReader.java | 7 +++++-- .../batch/item/xml/StaxEventItemReaderTests.java | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java index 13f2454981..c10cb1e75c 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java @@ -59,6 +59,7 @@ * * @author Robert Kasanicky * @author Mahmoud Ben Hassine + * @author Glenn Renfro */ public class StaxEventItemReader extends AbstractItemCountingItemStreamItemReader implements ResourceAwareItemReaderItemStream, InitializingBean { @@ -225,14 +226,16 @@ protected void doOpen() throws Exception { noInput = true; if (!resource.exists()) { if (strict) { - throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)"); + throw new IllegalStateException( + "Input resource " + resource.getURL() + " must exist (reader is in 'strict' mode)"); } logger.warn("Input resource does not exist " + resource.getDescription()); return; } if (!resource.isReadable()) { if (strict) { - throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)"); + throw new IllegalStateException( + "Input resource " + resource.getURL() + " must be readable (reader is in 'strict' mode)"); } logger.warn("Input resource is not readable " + resource.getDescription()); return; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java index 2afc86738b..19bfb27626 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java @@ -47,6 +47,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -71,6 +72,7 @@ * @author Robert Kasanicky * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Glenn Renfro */ class StaxEventItemReaderTests { @@ -582,7 +584,9 @@ void testStrictness() throws Exception { source.setStrict(true); source.afterPropertiesSet(); - assertThrows(ItemStreamException.class, () -> source.open(executionContext)); + ItemStreamException exception = assertThrows(ItemStreamException.class, () -> source.open(executionContext)); + assertEquals("Input resource file:/non/existent/file must exist (reader is in 'strict' mode)", + exception.getCause().getMessage()); } @@ -834,6 +838,11 @@ public InputStream getInputStream() throws IOException { return null; } + @Override + public URL getURL() throws IOException { + return new URL("file:/non/existent/file"); + } + } }