INT-3621: Add Namespace support for appendNewLine

JIRA: https://jira.springsource.org/browse/INT-3621

Polishing for code formatting and simple polishing to Docs
This commit is contained in:
Tony Falabella
2015-02-04 23:18:37 -05:00
committed by Artem Bilan
parent 24beee9627
commit a7d325adc1
9 changed files with 233 additions and 87 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.integration.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.ExpressionFactoryBean;
@@ -32,6 +33,7 @@ import org.springframework.util.StringUtils;
* @author Mark Fisher
* @author Artem Bilan
* @author Gunnar Hillert
* @author Tony Falabella
*
* @since 1.0.3
*/
@@ -39,7 +41,8 @@ abstract class FileWritingMessageHandlerBeanDefinitionBuilder {
static BeanDefinitionBuilder configure(Element element, boolean expectReply, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FileWritingMessageHandlerFactoryBean.class);
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(FileWritingMessageHandlerFactoryBean.class);
String directory = element.getAttribute("directory");
String directoryExpression = element.getAttribute("directory-expression");
@@ -48,16 +51,19 @@ abstract class FileWritingMessageHandlerBeanDefinitionBuilder {
parserContext.getReaderContext().error("directory or directory-expression is required", element);
}
else if (StringUtils.hasText(directory) && StringUtils.hasText(directoryExpression)) {
parserContext.getReaderContext().error("Either directory or directory-expression must be provided but not both", element);
parserContext.getReaderContext()
.error("Either directory or directory-expression must be provided but not both", element);
}
if (StringUtils.hasText(directoryExpression)) {
BeanDefinitionBuilder expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
BeanDefinitionBuilder expressionBuilder =
BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
expressionBuilder.addConstructorArgValue(directoryExpression);
builder.addPropertyValue("directoryExpression", expressionBuilder.getBeanDefinition());
}
builder.addPropertyValue("expectReply", expectReply);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "append-new-line");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "directory");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-create-directory");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "delete-source-files");
@@ -70,7 +76,8 @@ abstract class FileWritingMessageHandlerBeanDefinitionBuilder {
boolean hasRemoteFileNameGeneratorExpression = StringUtils.hasText(remoteFileNameGeneratorExpression);
if (hasRemoteFileNameGenerator || hasRemoteFileNameGeneratorExpression) {
if (hasRemoteFileNameGenerator && hasRemoteFileNameGeneratorExpression) {
parserContext.getReaderContext().error("at most one of 'filename-generator-expression' or 'filename-generator' " +
parserContext.getReaderContext()
.error("at most one of 'filename-generator-expression' or 'filename-generator' " +
"is allowed on file outbound adapter/gateway", element);
}
if (hasRemoteFileNameGenerator) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,10 +33,12 @@ import org.springframework.integration.file.support.FileExistsMode;
* @author Gary Russell
* @author Artem Bilan
* @author Gunnar Hillert
* @author Tony Falabella
*
* @since 1.0.3
*/
public class FileWritingMessageHandlerFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<FileWritingMessageHandler>{
public class FileWritingMessageHandlerFactoryBean
extends AbstractSimpleMessageHandlerFactoryBean<FileWritingMessageHandler>{
private volatile File directory;
@@ -59,6 +61,8 @@ public class FileWritingMessageHandlerFactoryBean extends AbstractSimpleMessageH
private volatile FileExistsMode fileExistsMode;
private volatile boolean expectReply = true;
private volatile Boolean appendNewLine;
public void setFileExistsMode(String fileExistsModeAsString) {
this.fileExistsMode = FileExistsMode.getForString(fileExistsModeAsString);
@@ -104,6 +108,10 @@ public class FileWritingMessageHandlerFactoryBean extends AbstractSimpleMessageH
this.expectReply = expectReply;
}
public void setAppendNewLine(Boolean appendNewLine) {
this.appendNewLine = appendNewLine;
}
@Override
protected FileWritingMessageHandler createHandler() {
@@ -143,11 +151,14 @@ public class FileWritingMessageHandlerFactoryBean extends AbstractSimpleMessageH
handler.setTemporaryFileSuffix(this.temporaryFileSuffix);
}
handler.setExpectReply(this.expectReply);
if (this.appendNewLine != null) {
handler.setAppendNewLine(this.appendNewLine);
}
if (this.fileExistsMode != null) {
handler.setFileExistsMode(this.fileExistsMode);
}
return handler;
}
}

View File

@@ -446,7 +446,15 @@ Only files matching this regular expression will be picked up by this adapter.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:attribute>
<xsd:attribute name="append-new-line" type="xsd:string" default="false">
<xsd:annotation>
<xsd:documentation>
Set to 'true' to append a new-line after each write.
It is 'false' by default.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attributeGroup ref="integration:smartLifeCycleAttributeGroup"/>
</xsd:complexType>

View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
@@ -28,14 +28,19 @@
channel="testChannel"
directory-expression="'foo/bar'"/>
<file:outbound-channel-adapter id="adapterWithAppendNewLine"
channel="testChannel"
append-new-line="true"
directory="${java.io.tmpdir}"/>
<file:outbound-channel-adapter id="adapterWithDeleteFlag"
channel="testChannel"
delete-source-files="true"
directory="${java.io.tmpdir}"/>
<file:outbound-channel-adapter id="adapterWithCharset"
<file:outbound-channel-adapter id="adapterWithCharset"
channel="testChannel"
charset="UTF-8"
charset="UTF-8"
directory="${java.io.tmpdir}"/>
<file:outbound-channel-adapter id="adapterWithOrder"
@@ -45,14 +50,28 @@
directory="${java.io.tmpdir}"/>
<file:outbound-channel-adapter id="usageChannel"
filename-generator-expression="@fooString"
filename-generator-expression="@fooString"
mode="APPEND"
directory-expression="@barString">
<file:request-handler-advice-chain>
<bean class="org.springframework.integration.file.config.FileOutboundChannelAdapterParserTests$FooAdvice" />
<bean class="org.springframework.integration.file.config.FileOutboundChannelAdapterParserTests$FooAdvice"/>
</file:request-handler-advice-chain>
</file:outbound-channel-adapter>
<file:outbound-channel-adapter id="adapterUsageWithAppendAndAppendNewLineTrue"
filename-generator-expression="@fooString"
mode="APPEND"
append-new-line="true"
directory-expression="@barString">
</file:outbound-channel-adapter>
<file:outbound-channel-adapter id="adapterUsageWithAppendAndAppendNewLineFalse"
filename-generator-expression="@fooString"
mode="APPEND"
append-new-line="false"
directory-expression="@barString">
</file:outbound-channel-adapter>
<bean id="fooString" class="java.lang.String">
<constructor-arg value="fileToAppend.txt"/>
</bean>
@@ -62,7 +81,7 @@
</bean>
<file:outbound-channel-adapter id="usageChannelWithFailMode"
filename-generator-expression="'fileToFail.txt'"
filename-generator-expression="'fileToFail.txt'"
mode="FAIL"
directory="test"/>
@@ -75,7 +94,7 @@
<si:dispatcher task-executor="executor"/>
</si:channel>
<file:outbound-channel-adapter channel="usageChannelConcurrent"
filename-generator-expression="'fileToAppendConcurrent.txt'"
filename-generator-expression="'fileToAppendConcurrent.txt'"
mode="APPEND"
directory="test"/>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@ import org.springframework.util.ReflectionUtils;
* @author Gary Russell
* @author Oleg Zhurakousky
* @author Gunnar Hillert
* @author Tony Falabella
*
*/
@ContextConfiguration
@@ -78,9 +79,18 @@ public class FileOutboundChannelAdapterParserTests {
@Autowired
EventDrivenConsumer adapterWithDirectoryExpression;
@Autowired
EventDrivenConsumer adapterWithAppendNewLine;
@Autowired
MessageChannel usageChannel;
@Autowired
MessageChannel adapterUsageWithAppendAndAppendNewLineTrue;
@Autowired
MessageChannel adapterUsageWithAppendAndAppendNewLineFalse;
@Autowired
MessageChannel usageChannelWithFailMode;
@@ -100,11 +110,13 @@ public class FileOutboundChannelAdapterParserTests {
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
File expected = new File(System.getProperty("java.io.tmpdir"));
Expression destinationDirectoryExpression = (Expression)handlerAccessor.getPropertyValue("destinationDirectoryExpression");
Expression destinationDirectoryExpression =
(Expression) handlerAccessor.getPropertyValue("destinationDirectoryExpression");
File actual = new File(destinationDirectoryExpression.getExpressionString());
assertEquals(".foo", TestUtils.getPropertyValue(handler, "temporaryFileSuffix", String.class));
assertThat(actual, is(expected));
DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
DefaultFileNameGenerator fileNameGenerator =
(DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
assertNotNull(fileNameGenerator);
Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class);
assertNotNull(expression);
@@ -120,7 +132,8 @@ public class FileOutboundChannelAdapterParserTests {
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
File expected = new File(System.getProperty("java.io.tmpdir"));
Expression destinationDirectoryExpression = (Expression)handlerAccessor.getPropertyValue("destinationDirectoryExpression");
Expression destinationDirectoryExpression =
(Expression) handlerAccessor.getPropertyValue("destinationDirectoryExpression");
File actual = new File(destinationDirectoryExpression.getExpressionString());
assertEquals(expected, actual);
@@ -155,7 +168,7 @@ public class FileOutboundChannelAdapterParserTests {
@Test
public void adapterWithCharset() {
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapterWithCharset);
FileWritingMessageHandler handler = (FileWritingMessageHandler)
FileWritingMessageHandler handler = (FileWritingMessageHandler)
adapterAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
@@ -164,23 +177,26 @@ public class FileOutboundChannelAdapterParserTests {
@Test
public void adapterWithDirectoryExpression() {
FileWritingMessageHandler handler = TestUtils.getPropertyValue(adapterWithDirectoryExpression, "handler", FileWritingMessageHandler.class);
FileWritingMessageHandler handler =
TestUtils.getPropertyValue(adapterWithDirectoryExpression, "handler", FileWritingMessageHandler.class);
Method m = ReflectionUtils.findMethod(FileWritingMessageHandler.class, "getTemporaryFileSuffix");
ReflectionUtils.makeAccessible(m);
assertEquals(".writing", ReflectionUtils.invokeMethod(m, handler));
String expectedExpressionString = "'foo/bar'";
String actualExpressionString = TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class).getExpressionString();
String actualExpressionString =
TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class)
.getExpressionString();
assertEquals(expectedExpressionString, actualExpressionString);
}
@Test
public void adapterUsageWithAppend() throws Exception{
public void adapterUsageWithAppend() throws Exception {
String expectedFileContent = "Initial File Content:String content:byte[] content:File content";
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
usageChannel.send(new GenericMessage<String>("Initial File Content:"));
@@ -195,10 +211,49 @@ public class FileOutboundChannelAdapterParserTests {
}
@Test
public void adapterUsageWithFailMode() throws Exception{
public void adapterUsageWithAppendAndAppendNewLineTrue() throws Exception {
assertEquals(Boolean.TRUE, TestUtils.getPropertyValue(this.adapterWithAppendNewLine, "handler.appendNewLine"));
String newLine = System.getProperty("line.separator");
String expectedFileContent = "Initial File Content:" + newLine + "String content:" + newLine +
"byte[] content:" + newLine + "File content" + newLine;
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()) {
testFile.delete();
}
adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage<String>("Initial File Content:"));
adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage<String>("String content:"));
adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage<byte[]>("byte[] content:".getBytes()));
adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage<File>(new File("test/input.txt")));
String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
assertEquals(expectedFileContent, actualFileContent);
testFile.delete();
}
@Test
public void adapterUsageWithAppendAndAppendNewLineFalse() throws Exception {
String expectedFileContent = "Initial File Content:String content:byte[] content:File content";
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()) {
testFile.delete();
}
adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage<String>("Initial File Content:"));
adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage<String>("String content:"));
adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage<byte[]>("byte[] content:".getBytes()));
adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage<File>(new File("test/input.txt")));
String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
assertEquals(expectedFileContent, actualFileContent);
testFile.delete();
}
@Test
public void adapterUsageWithFailMode() throws Exception {
File testFile = new File("test/fileToFail.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -217,13 +272,13 @@ public class FileOutboundChannelAdapterParserTests {
}
@Test
public void adapterUsageWithIgnoreMode() throws Exception{
public void adapterUsageWithIgnoreMode() throws Exception {
String expectedFileContent = "Initial File Content:";
File testFile = new File("test/fileToIgnore.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -237,10 +292,10 @@ public class FileOutboundChannelAdapterParserTests {
}
@Test
public void adapterUsageWithAppendConcurrent() throws Exception{
public void adapterUsageWithAppendConcurrent() throws Exception {
File testFile = new File("test/fileToAppendConcurrent.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -253,7 +308,7 @@ public class FileOutboundChannelAdapterParserTests {
String aString = aBuffer.toString();
String bString = bBuffer.toString();
for (int i = 0; i < 1; i ++) {
for (int i = 0; i < 1; i++) {
usageChannelConcurrent.send(new GenericMessage<String>(aString));
usageChannelConcurrent.send(new GenericMessage<String>(bString));
}
@@ -262,26 +317,28 @@ public class FileOutboundChannelAdapterParserTests {
String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
int beginningIndex = 0;
for (int i = 0; i < 2; i++) {
assertAllCharactersAreSame(actualFileContent.substring(beginningIndex, beginningIndex+99999));
assertAllCharactersAreSame(actualFileContent.substring(beginningIndex, beginningIndex + 99999));
beginningIndex += 100000;
}
}
private void assertAllCharactersAreSame(String substring){
private void assertAllCharactersAreSame(String substring) {
char[] characters = substring.toCharArray();
char c = characters[0];
for (char character : characters) {
assertEquals(c, character);
}
}
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {
public static class FooAdvice extends AbstractRequestHandlerAdvice {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
adviceCalled++;
return callback.execute();
}
}
}

View File

@@ -1,54 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<context:property-placeholder/>
<int-file:outbound-gateway id="ordered"
request-channel="someChannel" reply-timeout="777" directory="${java.io.tmpdir}"
auto-startup="false" order="777" filename-generator-expression="'foo.txt'" />
request-channel="someChannel" reply-timeout="777" directory="${java.io.tmpdir}"
auto-startup="false" order="777" filename-generator-expression="'foo.txt'"/>
<int-file:outbound-gateway id="gatewayWithDirectoryExpression"
request-channel="someChannel" directory-expression="'build/foo'"
auto-startup="false" order="777" filename-generator-expression="'foo.txt'" requires-reply="false">
request-channel="someChannel" directory-expression="'build/foo'"
auto-startup="false" order="777" filename-generator-expression="'foo.txt'"
requires-reply="false">
<int-file:request-handler-advice-chain>
<beans:bean class="org.springframework.integration.file.config.FileOutboundGatewayParserTests$FooAdvice" />
<beans:bean class="org.springframework.integration.file.config.FileOutboundGatewayParserTests$FooAdvice"/>
</int-file:request-handler-advice-chain>
</int-file:outbound-gateway>
<int-file:outbound-gateway id="gatewayWithReplaceMode"
request-channel="gatewayWithReplaceModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="REPLACE"
directory="test" requires-reply="false"/>
request-channel="gatewayWithReplaceModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="REPLACE"
directory="test" requires-reply="false"/>
<int-file:outbound-gateway id="gatewayWithAppendMode"
request-channel="gatewayWithAppendModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="APPEND"
directory="test" />
request-channel="gatewayWithAppendModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="APPEND"
directory="test"/>
<int-file:outbound-gateway id="gatewayWithFailMode"
request-channel="gatewayWithFailModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="FAIL"
directory="test" />
request-channel="gatewayWithFailModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="FAIL"
directory="test"/>
<int-file:outbound-gateway id="gatewayWithIgnoreMode"
request-channel="gatewayWithIgnoreModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="IGNORE"
directory="test" />
request-channel="gatewayWithIgnoreModeChannel"
filename-generator-expression="'fileToAppend.txt'" mode="IGNORE"
directory="test"/>
<int-file:outbound-gateway id="gatewayWithFailModeLowercase"
request-channel="gatewayWithFailModeLowercaseChannel"
filename-generator-expression="'fileToAppend.txt'" mode="fail"
directory="test" />
<context:property-placeholder />
request-channel="gatewayWithFailModeLowercaseChannel"
filename-generator-expression="'fileToAppend.txt'" mode="fail"
directory="test"/>
<int-file:outbound-gateway id="gatewayWithAppendNewLine"
request-channel="gatewayWithAppendNewLineChannel"
filename-generator-expression="'fileToAppend.txt'"
append-new-line="true"
directory="test"/>
</beans:beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ import org.springframework.util.FileCopyUtils;
* @author Mark Fisher
* @author Gunnar Hillert
* @author Artem Bilan
* @author Tony Falabella
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@@ -74,12 +75,15 @@ public class FileOutboundGatewayParserTests {
MessageChannel gatewayWithReplaceModeChannel;
@Autowired
@Qualifier("gatewayWithReplaceMode.handler")
@Qualifier("gatewayWithReplaceMode.handler")
MessageHandler gatewayWithReplaceModeHandler;
@Autowired
MessageChannel gatewayWithFailModeLowercaseChannel;
@Autowired
EventDrivenConsumer gatewayWithAppendNewLine;
private volatile static int adviceCalled;
@Test
@@ -92,7 +96,8 @@ public class FileOutboundGatewayParserTests {
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(777, handlerAccessor.getPropertyValue("order"));
assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("requiresReply"));
DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
DefaultFileNameGenerator fileNameGenerator =
(DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
assertNotNull(fileNameGenerator);
Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class);
assertNotNull(expression);
@@ -105,8 +110,11 @@ public class FileOutboundGatewayParserTests {
@Test
public void testOutboundGatewayWithDirectoryExpression() throws Exception {
FileWritingMessageHandler handler = TestUtils.getPropertyValue(gatewayWithDirectoryExpression, "handler", FileWritingMessageHandler.class);
assertEquals("'build/foo'", TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class).getExpressionString());
FileWritingMessageHandler handler =
TestUtils.getPropertyValue(gatewayWithDirectoryExpression, "handler", FileWritingMessageHandler.class);
assertEquals("'build/foo'",
TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class)
.getExpressionString());
handler.handleMessage(new GenericMessage<String>("foo"));
assertEquals(1, adviceCalled);
}
@@ -121,7 +129,7 @@ public class FileOutboundGatewayParserTests {
*
*/
@Test
public void gatewayWithIgnoreMode() throws Exception{
public void gatewayWithIgnoreMode() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithIgnoreModeChannel);
@@ -129,7 +137,7 @@ public class FileOutboundGatewayParserTests {
final String expectedFileContent = "Initial File Content:";
final File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -157,7 +165,7 @@ public class FileOutboundGatewayParserTests {
*
*/
@Test
public void gatewayWithFailMode() throws Exception{
public void gatewayWithFailMode() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithFailModeChannel);
@@ -166,7 +174,7 @@ public class FileOutboundGatewayParserTests {
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -179,7 +187,8 @@ public class FileOutboundGatewayParserTests {
messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
} catch (MessageHandlingException e) {
}
catch (MessageHandlingException e) {
assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
return;
}
@@ -197,7 +206,7 @@ public class FileOutboundGatewayParserTests {
*
*/
@Test
public void gatewayWithFailModeLowercase() throws Exception{
public void gatewayWithFailModeLowercase() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithFailModeLowercaseChannel);
@@ -206,7 +215,7 @@ public class FileOutboundGatewayParserTests {
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -219,7 +228,8 @@ public class FileOutboundGatewayParserTests {
messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
} catch (MessageHandlingException e) {
}
catch (MessageHandlingException e) {
assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
return;
}
@@ -239,7 +249,7 @@ public class FileOutboundGatewayParserTests {
*
*/
@Test
public void gatewayWithAppendMode() throws Exception{
public void gatewayWithAppendMode() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithAppendModeChannel);
@@ -248,7 +258,7 @@ public class FileOutboundGatewayParserTests {
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -276,7 +286,7 @@ public class FileOutboundGatewayParserTests {
*
*/
@Test
public void gatewayWithReplaceMode() throws Exception{
public void gatewayWithReplaceMode() throws Exception {
assertFalse(TestUtils.getPropertyValue(this.gatewayWithReplaceModeHandler, "requiresReply", Boolean.class));
@@ -287,7 +297,7 @@ public class FileOutboundGatewayParserTests {
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()){
if (testFile.exists()) {
testFile.delete();
}
@@ -304,7 +314,17 @@ public class FileOutboundGatewayParserTests {
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {
/**
* Test that the underlying {@link FileWritingMessageHandler} bean of the File Outbound Gateway
* gets it's {@link FileWritingMessageHandler#setAppendNewLine(boolean)} called when XML
* config file has <code>append-new-line="true"</code>.
*/
@Test
public void gatewayWithAppendNewLine() {
assertEquals(Boolean.TRUE, TestUtils.getPropertyValue(this.gatewayWithAppendNewLine, "handler.appendNewLine"));
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
@@ -313,4 +333,5 @@ public class FileOutboundGatewayParserTests {
}
}
}

View File

@@ -478,6 +478,15 @@
Message has a File payload or if the <classname>FileHeaders.ORIGINAL_FILE</classname> header
value contains either the source File instance or a String representing the original file path.
</note>
<para>
Starting with <emphasis>version 4.2</emphasis> The <classname>FileWritingMessageHandler</classname>
supports an <code>append-new-line</code> option.
If set to <code>true</code>, a new line is appended to the file after a message is written.
The default attribute value is <code>false</code>.
</para>
<programlisting language="xml"><![CDATA[<int-file:outbound-channel-adapter id="newlineAdapter"
append-new-line="true"
directory="${output.directory}"/>]]></programlisting>
</section>
<section id="file-writing-output-gateway">
<title>Outbound Gateway</title>

View File

@@ -22,5 +22,14 @@
attribute.
</para>
</section>
<section id="4.2-file-outbound-channel-adapter">
<title>File Outbound Channel Adapter</title>
<para>
The <code>&lt;int-file:outbound-channel-adapter&gt;</code> and
<code>&lt;int-file:outbound-gateway&gt;</code> now support an <code>append-new-line</code> attribute.
If set to <code>true</code>, a new line is appended to the file after a message is written.
The default attribute value is <code>false</code>.
</para>
</section>
</section>
</chapter>