From a7d325adc159763821d942e121299744fae7fa3e Mon Sep 17 00:00:00 2001 From: Tony Falabella Date: Wed, 4 Feb 2015 23:18:37 -0500 Subject: [PATCH] INT-3621: Add Namespace support for appendNewLine JIRA: https://jira.springsource.org/browse/INT-3621 Polishing for code formatting and simple polishing to Docs --- ...ngMessageHandlerBeanDefinitionBuilder.java | 17 +++- .../FileWritingMessageHandlerFactoryBean.java | 17 +++- .../config/spring-integration-file-4.2.xsd | 10 +- ...boundChannelAdapterParserTests-context.xml | 43 +++++--- ...FileOutboundChannelAdapterParserTests.java | 97 +++++++++++++++---- ...FileOutboundGatewayParserTests-context.xml | 61 ++++++------ .../FileOutboundGatewayParserTests.java | 57 +++++++---- src/reference/docbook/file.xml | 9 ++ src/reference/docbook/whats-new.xml | 9 ++ 9 files changed, 233 insertions(+), 87 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerBeanDefinitionBuilder.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerBeanDefinitionBuilder.java index f12275158d..771385d20d 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerBeanDefinitionBuilder.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerBeanDefinitionBuilder.java @@ -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) { diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerFactoryBean.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerFactoryBean.java index 2e8fb87d4c..f125e89ae5 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerFactoryBean.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileWritingMessageHandlerFactoryBean.java @@ -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{ +public class FileWritingMessageHandlerFactoryBean + extends AbstractSimpleMessageHandlerFactoryBean{ 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; } + } diff --git a/spring-integration-file/src/main/resources/org/springframework/integration/file/config/spring-integration-file-4.2.xsd b/spring-integration-file/src/main/resources/org/springframework/integration/file/config/spring-integration-file-4.2.xsd index 2fdb30fd9b..4c7f1852f2 100644 --- a/spring-integration-file/src/main/resources/org/springframework/integration/file/config/spring-integration-file-4.2.xsd +++ b/spring-integration-file/src/main/resources/org/springframework/integration/file/config/spring-integration-file-4.2.xsd @@ -446,7 +446,15 @@ Only files matching this regular expression will be picked up by this adapter. ]]> - + + + + + Set to 'true' to append a new-line after each write. + It is 'false' by default. + + + diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml index 058ca13978..3d8a4b11a4 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml @@ -1,11 +1,11 @@ + + - - + + + + + + + @@ -62,7 +81,7 @@ @@ -75,7 +94,7 @@ diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java index 2c490175d3..49af763b83 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java @@ -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("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("Initial File Content:")); + adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage("String content:")); + adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage("byte[] content:".getBytes())); + adapterUsageWithAppendAndAppendNewLineTrue.send(new GenericMessage(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("Initial File Content:")); + adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage("String content:")); + adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage("byte[] content:".getBytes())); + adapterUsageWithAppendAndAppendNewLineFalse.send(new GenericMessage(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(aString)); usageChannelConcurrent.send(new GenericMessage(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(); } + } + } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests-context.xml b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests-context.xml index ae2ce6fc6a..cea4dc3c9f 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests-context.xml +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests-context.xml @@ -1,54 +1,59 @@ + + + request-channel="someChannel" reply-timeout="777" directory="${java.io.tmpdir}" + auto-startup="false" order="777" filename-generator-expression="'foo.txt'"/> + request-channel="someChannel" directory-expression="'build/foo'" + auto-startup="false" order="777" filename-generator-expression="'foo.txt'" + requires-reply="false"> - + + request-channel="gatewayWithReplaceModeChannel" + filename-generator-expression="'fileToAppend.txt'" mode="REPLACE" + directory="test" requires-reply="false"/> + request-channel="gatewayWithAppendModeChannel" + filename-generator-expression="'fileToAppend.txt'" mode="APPEND" + directory="test"/> + request-channel="gatewayWithFailModeChannel" + filename-generator-expression="'fileToAppend.txt'" mode="FAIL" + directory="test"/> + request-channel="gatewayWithIgnoreModeChannel" + filename-generator-expression="'fileToAppend.txt'" mode="IGNORE" + directory="test"/> - + request-channel="gatewayWithFailModeLowercaseChannel" + filename-generator-expression="'fileToAppend.txt'" mode="fail" + directory="test"/> + + diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java index 170403cebb..c0660b8c6c 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java @@ -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("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 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 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 append-new-line="true". + */ + @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 { } } + } diff --git a/src/reference/docbook/file.xml b/src/reference/docbook/file.xml index 4b3ff0abc1..bbede74085 100644 --- a/src/reference/docbook/file.xml +++ b/src/reference/docbook/file.xml @@ -478,6 +478,15 @@ Message has a File payload or if the FileHeaders.ORIGINAL_FILE header value contains either the source File instance or a String representing the original file path. + + Starting with version 4.2 The FileWritingMessageHandler + supports an append-new-line option. + If set to true, a new line is appended to the file after a message is written. + The default attribute value is false. + + ]]>
Outbound Gateway diff --git a/src/reference/docbook/whats-new.xml b/src/reference/docbook/whats-new.xml index 0a19625a06..57cbb75efb 100644 --- a/src/reference/docbook/whats-new.xml +++ b/src/reference/docbook/whats-new.xml @@ -22,5 +22,14 @@ attribute.
+
+ File Outbound Channel Adapter + + The <int-file:outbound-channel-adapter> and + <int-file:outbound-gateway> now support an append-new-line attribute. + If set to true, a new line is appended to the file after a message is written. + The default attribute value is false. + +