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

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

commit 764ac792aba88bd4c5b31c43208a91396f3085b1
Author: Otavio R. Piske <angusyo...@gmail.com>
AuthorDate: Sun May 26 14:06:52 2024 +0200

    (chores) camel-file: simplify truncating a file
    
    Also improve test to cover truncating scenarios and make it stricter
    
    Signed-off-by: Otavio R. Piske <angusyo...@gmail.com>
---
 .../camel/component/file/FileOperations.java       |  5 +--
 .../file/FileProducerAllowNullBodyTest.java        | 39 ++++++++++++++++++++--
 2 files changed, 38 insertions(+), 6 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 a4efa079cbf..18d8a320d86 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
@@ -541,10 +541,7 @@ public class FileOperations implements 
GenericFileOperations<File> {
             FileUtil.createNewFile(target);
         } else if (endpoint.getFileExist() == GenericFileExist.Override) {
             LOG.debug("Truncating existing file: {}", target);
-            try (SeekableByteChannel out
-                    = Files.newByteChannel(target.toPath(), 
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
-                // nothing to write
-            }
+            Files.write(target.toPath(), new byte[0], 
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
         }
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
index 63317f2f842..72abdf2a89c 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
@@ -17,12 +17,19 @@
 package org.apache.camel.component.file;
 
 import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -31,17 +38,45 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  * exception When the allowNullBody option is set to false it will throw an 
exception of "Cannot write null body to
  * file."
  */
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 public class FileProducerAllowNullBodyTest extends ContextTestSupport {
 
+    @Order(1)
     @Test
+    @DisplayName("Tests that an empty file named allowNullBody.txt will be 
created")
     public void testAllowNullBodyTrue() {
         
template.sendBody(fileUri("?allowNullBody=true&fileName=allowNullBody.txt"), 
null);
-        assertFileExists(testFile("allowNullBody.txt"));
+
+        final Path path = testFile("allowNullBody.txt");
+        assertFileExists(path);
+
+        final long size = path.toFile().length();
+        assertEquals(0, size);
     }
 
+    @Order(2)
     @Test
-    public void testAllowNullBodyFalse() {
+    @DisplayName("Tests that a non-empty file named allowNullBody.txt will be 
created and then truncated")
+    public void testAllowNullBodyTrueTruncate() {
+        
template.sendBody(fileUri("?allowNullBody=true&fileName=allowNullBody.txt"), 
"Hello");
 
+        final Path path = testFile("allowNullBody.txt");
+        assertFileExists(path);
+
+        final long sizeBeforeTruncate = path.toFile().length();
+        assertNotEquals(0, sizeBeforeTruncate);
+
+        
template.sendBody(fileUri("?allowNullBody=true&fileName=allowNullBody.txt"), 
null);
+        assertFileExists(path);
+
+        final long sizeAfterTruncate = path.toFile().length();
+        assertEquals(0, sizeAfterTruncate);
+    }
+
+    @Order(3)
+    @Test
+    @DisplayName("Tests that an exception will be thrown if allowNullBody is 
absent and a null body is sent")
+    public void testAllowNullBodyFalse() {
         CamelExecutionException e = assertThrows(CamelExecutionException.class,
                 () -> 
template.sendBody(fileUri("?fileName=allowNullBody.txt"), null),
                 "Should have thrown a GenericFileOperationFailedException");

Reply via email to