diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index de5d5b53d7..7fcbd02dff 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -32,15 +32,16 @@ import org.springframework.util.FileCopyUtils; /** * A {@link MessageHandler} implementation that writes the Message payload to a * file. If the payload is a File object, it will copy the File to this - * consumer's directory. If the payload is a byte array or String, it will - * write it directly. Otherwise, the payload type is unsupported, and an - * Exception will be thrown. + * consumer's directory. If the payload is a byte array or String, it will write + * it directly. Otherwise, the payload type is unsupported, and an Exception + * will be thrown. *

- * Other transformers may be useful to precede this handler. For example, - * any Serializable object payload can be converted into a byte array by the - * {@link org.springframework.integration.transformer.PayloadSerializingTransformer}. - * Likewise, any Object can be converted to a String based on its toString() - * method by the {@link org.springframework.integration.transformer.ObjectToStringTransformer}. + * Other transformers may be useful to precede this handler. For example, any + * Serializable object payload can be converted into a byte array by the + * {@link org.springframework.integration.transformer.PayloadSerializingTransformer} + * . Likewise, any Object can be converted to a String based on its + * toString() method by the + * {@link org.springframework.integration.transformer.ObjectToStringTransformer}. * * @author Mark Fisher * @author Iwein Fuld @@ -51,26 +52,27 @@ public class FileWritingMessageHandler implements MessageHandler { private final File parentDirectory; - private volatile Charset charset = Charset.defaultCharset(); + private volatile Charset charset = Charset.defaultCharset(); public FileWritingMessageHandler(Resource parentDirectory) { try { + Assert.isTrue(parentDirectory.exists(), "Output directory [" + parentDirectory + "] does not exist"); this.parentDirectory = parentDirectory.getFile(); + Assert.isTrue(this.parentDirectory.isDirectory(), "[" + this.parentDirectory + "] is not a directory"); + Assert.isTrue(this.parentDirectory.canWrite(), "[" + this.parentDirectory + "] should be writable"); } catch (IOException e) { - // TODO Auto-generated catch block - throw new RuntimeException(e); + throw new IllegalArgumentException("Inaccessable output directory", e); } } - public void setFileNameGenerator(FileNameGenerator fileNameGenerator) { this.fileNameGenerator = fileNameGenerator; } /** - * Set the charset name to use when writing a File from a - * String-based Message payload. + * Set the charset name to use when writing a File from a String-based + * Message payload. */ public void setCharset(String charset) { Assert.notNull(charset, "charset must not be null"); @@ -95,8 +97,8 @@ public class FileWritingMessageHandler implements MessageHandler { FileCopyUtils.copy((String) payload, writer); } else { - throw new IllegalArgumentException( - "unsupported Message payload type [" + payload.getClass().getName() + "]"); + throw new IllegalArgumentException("unsupported Message payload type [" + payload.getClass().getName() + + "]"); } } catch (Exception e) { diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java index 63676a3f93..6a3c8450cc 100644 --- a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java @@ -17,8 +17,15 @@ package org.springframework.integration.file; import static org.easymock.EasyMock.*; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + import java.io.File; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.Resource; @@ -30,16 +37,50 @@ import org.springframework.integration.message.MessageHandlingException; * @author Iwein Fuld */ public class FileWritingMessageHandlerTests { - + private Resource outputDirectory = createMock(Resource.class); + private FileWritingMessageHandler subject; + + private static File outputDirectoryFile = new File(System.getProperty("java.io.tmpdir") + "/" + + FileWritingMessageHandlerTests.class.getSimpleName());; + + @BeforeClass + public static void setupOutputDir() throws Exception { + outputDirectoryFile.mkdir(); + } + + + @Before + public void setup() throws Exception { + expect(outputDirectory.getFile()).andReturn(outputDirectoryFile).anyTimes(); + expect(outputDirectory.exists()).andReturn(true).anyTimes(); + replay(outputDirectory); + subject = new FileWritingMessageHandler(outputDirectory); + } + @Test(expected = MessageHandlingException.class) public void unsupportedType() throws Exception { - expect(outputDirectory.getFile()).andReturn(new File(System.getProperty("java.io.tmpdir"))).anyTimes(); - replay(outputDirectory); - FileWritingMessageHandler handler = new FileWritingMessageHandler( - outputDirectory ); - handler.handleMessage(new GenericMessage(99)); + subject.handleMessage(new GenericMessage(99)); + assertThat(outputDirectoryFile.listFiles()[0], nullValue()); } + @Test + public void supportedType() throws Exception { + subject.handleMessage(new GenericMessage("test")); + assertThat(outputDirectoryFile.listFiles()[0], notNullValue()); + } + + @After + public void emptyOutputDir() { + File[] files = outputDirectoryFile.listFiles(); + for (File file : files) { + file.delete(); + } + } + + @AfterClass + public static void cleanupOutputDir() throws Exception { + outputDirectoryFile.delete(); + } }