This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 778cc4f  CAMEL-14127: avoid file by file target replacement when 
fileExist=Append (#3309)
778cc4f is described below

commit 778cc4fe8c3abe4dad8fa3fde29a3e922b68c8ec
Author: Marco Collovati <mcollov...@gmail.com>
AuthorDate: Mon Nov 4 05:52:14 2019 +0100

    CAMEL-14127: avoid file by file target replacement when fileExist=Append 
(#3309)
    
    When file producer applies file based optimization it ignores
    fileExist=Append mode and always replaces target file.
    This change avoids file based optimization when fileExist=Append.
---
 .../camel/component/file/FileOperations.java       |  5 +++--
 .../file/FileProducerFileExistAppendTest.java      | 22 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
index c74746e..08b4bdc 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
@@ -278,10 +278,11 @@ public class FileOperations implements 
GenericFileOperations<File> {
             String charset = endpoint.getCharset();
 
             // we can optimize and use file based if no charset must be used, 
and the input body is a file
+            // however optimization cannot be applied when content should be 
appended to target file
             File source = null;
             boolean fileBased = false;
-            if (charset == null) {
-                // if no charset, then we can try using file directly 
(optimized)
+            if (charset == null && endpoint.getFileExist() != 
GenericFileExist.Append) {
+                // if no charset and not in appending mode, then we can try 
using file directly (optimized)
                 Object body = exchange.getIn().getBody();
                 if (body instanceof WrappedFile) {
                     body = ((WrappedFile<?>) body).getFile();
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistAppendTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistAppendTest.java
index 33ccc93..27de435 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistAppendTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistAppendTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.file;
 
+import java.io.File;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
@@ -47,6 +49,26 @@ public class FileProducerFileExistAppendTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testAppendFileByFile() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+
+        // Create some test files
+        template.sendBodyAndHeader("file://target/data/file", "Row 1\n", 
Exchange.FILE_NAME, "test1.txt");
+        template.sendBodyAndHeader("file://target/data/file", "Row 2\n", 
Exchange.FILE_NAME, "test2.txt");
+
+        // Append test files to the target one
+        template.sendBodyAndHeader("file://target/data/file?fileExist=Append", 
new File("target/data/file/test1.txt"), Exchange.FILE_NAME, "out.txt");
+        template.sendBodyAndHeader("file://target/data/file?fileExist=Append", 
new File("target/data/file/test2.txt"), Exchange.FILE_NAME, "out.txt");
+
+        mock.expectedFileExists("target/data/file/out.txt", "Row 1\nRow 2\n");
+
+        context.getRouteController().startAllRoutes();
+
+        assertMockEndpointsSatisfied();
+
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

Reply via email to