RESOLVED - BATCH-766: Insufficient error handling in case of a missing resource for a org.springframework.batch.item.xml.StaxEventItemWriter
moved the 'exists' check after possible output file creation
This commit is contained in:
@@ -52,6 +52,7 @@ public class FileUtils {
|
||||
new File(file.getParent()).mkdirs();
|
||||
}
|
||||
file.createNewFile();
|
||||
Assert.state(file.exists(), "Output file must exist");
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
throw new ItemStreamException(
|
||||
|
||||
@@ -255,8 +255,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements
|
||||
*/
|
||||
public void open(ExecutionContext executionContext) {
|
||||
|
||||
Assert.state(resource.exists(), "Output resource must exist");
|
||||
|
||||
long startAtPosition = 0;
|
||||
|
||||
// if restart data is provided, restart from provided offset
|
||||
@@ -287,6 +285,7 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements
|
||||
try {
|
||||
file = resource.getFile();
|
||||
FileUtils.setUpOutputFile(file, restarted, overwriteOutput);
|
||||
Assert.state(resource.exists(), "Output resource must exist");
|
||||
os = new FileOutputStream(file, true);
|
||||
channel = os.getChannel();
|
||||
setPosition(position);
|
||||
|
||||
@@ -120,6 +120,23 @@ public class FileUtilsTests extends TestCase {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public void testCouldntCreateFile(){
|
||||
|
||||
File file = new File("new file"){
|
||||
public boolean exists() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
try{
|
||||
FileUtils.setUpOutputFile(file, false, false);
|
||||
fail();
|
||||
}catch(IllegalStateException ex){
|
||||
assertEquals("Output file must exist", ex.getMessage());
|
||||
}finally{
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Assert.state(!file.exists());
|
||||
|
||||
@@ -227,6 +227,10 @@ public class StaxEventItemWriterTests extends TestCase {
|
||||
public void testNonExistantResource() throws Exception {
|
||||
Resource doesntExist = new DescriptiveResource("") {
|
||||
|
||||
public File getFile() throws IOException {
|
||||
return new File("does not exist");
|
||||
}
|
||||
|
||||
public boolean exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user