Improved tests, added checks on output directory

This commit is contained in:
Iwein Fuld
2009-02-03 14:11:45 +00:00
parent fb1e68cd4f
commit 918ee6545f
2 changed files with 65 additions and 22 deletions

View File

@@ -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.
* <p>
* 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 <code>toString()</code>
* 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
* <code>toString()</code> 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) {

View File

@@ -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<Integer>(99));
subject.handleMessage(new GenericMessage<Integer>(99));
assertThat(outputDirectoryFile.listFiles()[0], nullValue());
}
@Test
public void supportedType() throws Exception {
subject.handleMessage(new GenericMessage<String>("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();
}
}