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:
robokaso
2008-08-11 11:35:07 +00:00
parent 452591e580
commit 8dc8d253f5
4 changed files with 51 additions and 16 deletions

View File

@@ -5,8 +5,11 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.util.Assert;
@@ -15,7 +18,7 @@ import org.springframework.util.Assert;
*
* @author Robert Kasanicky
*/
public class FileUtilsTests extends TestCase {
public class FileUtilsTests {
private File file = new File("FileUtilsTests.tmp");
@@ -23,6 +26,7 @@ public class FileUtilsTests extends TestCase {
* No restart + file should not be overwritten => file is created if it does
* not exist, exception is thrown if it already exists
*/
@Test
public void testNoRestart() throws Exception {
FileUtils.setUpOutputFile(file, false, false);
assertTrue(file.exists());
@@ -58,6 +62,7 @@ public class FileUtilsTests extends TestCase {
* In case of restart, the file is supposed to exist and exception is thrown
* if it does not.
*/
@Test
public void testRestart() throws Exception {
try {
FileUtils.setUpOutputFile(file, true, false);
@@ -86,6 +91,7 @@ public class FileUtilsTests extends TestCase {
/**
* If the directories on the file path do not exist, they should be created
*/
@Test
public void testCreateDirectoryStructure() {
File file = new File("testDirectory/testDirectory2/testFile.tmp");
File dir1 = new File("testDirectory");
@@ -104,6 +110,7 @@ public class FileUtilsTests extends TestCase {
}
}
@Test
public void testBadFile(){
File file = new File("new file"){
@@ -120,12 +127,35 @@ public class FileUtilsTests extends TestCase {
file.delete();
}
}
@Test
public void testCouldntCreateFile(){
protected void setUp() throws Exception {
File file = new File("new file"){
@Override
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();
}
}
@Before
public void setUp() throws Exception {
Assert.state(!file.exists());
}
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
file.delete();
}

View File

@@ -1,5 +1,14 @@
package org.springframework.batch.item.xml;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
@@ -8,14 +17,11 @@ import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
import static org.junit.Assert.*;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.xml.oxm.MarshallingEventWriterSerializer;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.Marshaller;
@@ -239,13 +245,11 @@ public class StaxEventItemWriterTests {
@Test
public void testNonExistantResource() throws Exception {
Resource doesntExist = new DescriptiveResource("") {
public boolean exists() {
return false;
}
};
Resource doesntExist = createMock(Resource.class);
expect(doesntExist.getFile()).andReturn(new File("does not exist"));
expect(doesntExist.exists()).andReturn(false);
replay(doesntExist);
writer.setResource(doesntExist);
try {