Skip to content

Add URL to error message when StaxEventItemReader does not exist #3915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,6 +58,7 @@
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*/
public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {
Expand Down Expand Up @@ -223,14 +224,14 @@ 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2020 the original author or authors.
* Copyright 2008-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.Unmarshaller;
Expand Down Expand Up @@ -57,13 +58,15 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests for {@link StaxEventItemReader}.
*
* @author Robert Kasanicky
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*/
public class StaxEventItemReaderTests {

Expand Down Expand Up @@ -110,6 +113,41 @@ public void testAfterPropertiesSet() throws Exception {
source.afterPropertiesSet();
}

/**
* Regular usage scenario. ItemReader should pass XML fragments to unmarshaller wrapped with StartDocument and
* EndDocument events.
*/
@Test
public void testNoResource() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test named testStrictness that uses a NonExistentResource for the use case related to the original issue. So I think there is no need to add this new test and its related getBadResource() method. We can update the existent test to assert that the exception's message now contains the URL of the resource.

I will take care of this on merge.

final String FILE_NOT_HERE = "file.not.here";
final String CLASSPATH_NOT_HERE = "classpath.not.here";

StaxEventItemReader<List<XMLEvent>> newSource = getBadResource(new FileSystemResource(new File(FILE_NOT_HERE)));
verifyStrictResult(newSource, FILE_NOT_HERE);
newSource = getBadResource(new ClassPathResource(CLASSPATH_NOT_HERE));
verifyStrictResult(newSource, CLASSPATH_NOT_HERE);
}

private void verifyStrictResult(StaxEventItemReader<List<XMLEvent>> newSource, String message) {
Exception exception = assertThrows(ItemStreamException.class, () -> {
newSource.open(executionContext);
});
String actualMessage = exception.getCause().getMessage();
assertTrue(actualMessage.contains(message));
newSource.close();
}
private StaxEventItemReader<List<XMLEvent>> getBadResource(Resource resource) throws Exception{
StaxEventItemReader<List<XMLEvent>> newSource = new StaxEventItemReader<>();
newSource.setResource(resource);

newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setUnmarshaller(unmarshaller);
newSource.setSaveState(true);

newSource.afterPropertiesSet();
return newSource;
}

@Test
public void testAfterPropertesSetException() throws Exception {

Expand Down