INT-4231: Add FileExistsMode.REPLACE_IF_MODIFIED

JIRA: https://jira.spring.io/browse/INT-4231

For `FileWritingMessageHandler` and (S)FTP outbound gateways, support
`FileExistsMode.REPLACE_IF_MODIFIED` to allow overwriting an existing file if
the source file modified time is different to the existing file.

Polishing
This commit is contained in:
Gary Russell
2017-02-21 15:32:09 -05:00
committed by Artem Bilan
parent b30f404173
commit 2d4a7cf92f
18 changed files with 217 additions and 44 deletions

View File

@@ -518,6 +518,61 @@ public class FileWritingMessageHandlerTests {
handler.stop();
}
@Test
public void replaceIfDifferent() throws IOException {
QueueChannel output = new QueueChannel();
this.handler.setOutputChannel(output);
this.handler.setPreserveTimestamp(true);
this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
this.handler.handleMessage(MessageBuilder.withPayload("foo")
.setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
.setHeader(FileHeaders.SET_MODIFIED, 42_000_000)
.build());
Message<?> result = output.receive(0);
assertFileContentIs(result, "foo");
assertLastModifiedIs(result, 42_000_000);
this.handler.handleMessage(MessageBuilder.withPayload("bar")
.setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
.setHeader(FileHeaders.SET_MODIFIED, 42_000_000)
.build());
result = output.receive(0);
assertFileContentIs(result, "foo"); // no overwrite - timestamp same
assertLastModifiedIs(result, 42_000_000);
this.handler.handleMessage(MessageBuilder.withPayload("bar")
.setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
.setHeader(FileHeaders.SET_MODIFIED, 43_000_000)
.build());
result = output.receive(0);
assertFileContentIs(result, "bar");
assertLastModifiedIs(result, 43_000_000);
}
@Test
public void replaceIfDifferentFile() throws IOException {
File file = new File(this.temp.newFolder(), "foo.txt");
FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(file));
file.setLastModified(42_000_000);
QueueChannel output = new QueueChannel();
this.handler.setOutputChannel(output);
this.handler.setPreserveTimestamp(true);
this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
this.handler.handleMessage(MessageBuilder.withPayload(file).build());
Message<?> result = output.receive(0);
assertFileContentIs(result, "foo");
assertLastModifiedIs(result, 42_000_000);
FileCopyUtils.copy("bar".getBytes(), new FileOutputStream(file));
file.setLastModified(42_000_000);
this.handler.handleMessage(MessageBuilder.withPayload(file).build());
result = output.receive(0);
assertFileContentIs(result, "foo"); // no overwrite - timestamp same
assertLastModifiedIs(result, 42_000_000);
file.setLastModified(43_000_000);
this.handler.handleMessage(MessageBuilder.withPayload(file).build());
result = output.receive(0);
assertFileContentIs(result, "bar");
assertLastModifiedIs(result, 43_000_000);
}
void assertFileContentIsMatching(Message<?> result) throws IOException {
assertFileContentIs(result, SAMPLE_CONTENT);
}