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

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


The following commit(s) were added to refs/heads/main by this push:
     new 72bbe88c4ed AMEL-20686 - set 7: review File tests for incorrectly 
shared resource (#16644)
72bbe88c4ed is described below

commit 72bbe88c4ed3a881835d4d6bab22f74b7c37993d
Author: Jang-Vijay Singh <[email protected]>
AuthorDate: Tue Dec 24 08:22:59 2024 +0000

    AMEL-20686 - set 7: review File tests for incorrectly shared resource 
(#16644)
---
 .../file/FileConsumeFilesAndDeleteTest.java        | 14 ++++---
 .../FromFileDoNotDeleteFileIfProcessFailsTest.java |  7 +++-
 .../FromFileDoNotMoveFileIfProcessFailsTest.java   |  7 +++-
 .../camel/component/file/NewFileConsumeTest.java   |  7 +++-
 .../apache/camel/converter/NIOConverterTest.java   |  6 ++-
 .../camel/issues/FilePollingConsumerIssueTest.java |  9 ++--
 .../apache/camel/language/FileLanguageTest.java    | 49 ++++++++++++----------
 7 files changed, 62 insertions(+), 37 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeFilesAndDeleteTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeFilesAndDeleteTest.java
index 47025ae9999..36626150938 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeFilesAndDeleteTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeFilesAndDeleteTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.nio.file.Files;
+import java.util.UUID;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -30,29 +31,32 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
  * Unit test for consuming the same filename only.
  */
 public class FileConsumeFilesAndDeleteTest extends ContextTestSupport {
+    private static final String TEST_FILE_NAME_1 = "report" + 
UUID.randomUUID() + ".txt";
+    private static final String TEST_FILE_NAME_2 = "report2" + 
UUID.randomUUID() + ".txt";
+    private static final String TEST_FILE_NAME_3 = "report2008" + 
UUID.randomUUID() + ".txt";
 
     @Test
     public void testConsumeAndDelete() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");
 
-        template.sendBodyAndHeader(fileUri(), "Bye World", Exchange.FILE_NAME, 
"report2.txt");
-        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, "report.txt");
-        template.sendBodyAndHeader(fileUri() + "/2008", "2008 Report", 
Exchange.FILE_NAME, "report2008.txt");
+        template.sendBodyAndHeader(fileUri(), "Bye World", Exchange.FILE_NAME, 
TEST_FILE_NAME_2);
+        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME_1);
+        template.sendBodyAndHeader(fileUri() + "/2008", "2008 Report", 
Exchange.FILE_NAME, TEST_FILE_NAME_3);
 
         assertMockEndpointsSatisfied();
 
         oneExchangeDone.matchesWaitTime();
 
         // file should not exists
-        assertFalse(Files.exists(testFile("report.txt")), "File should been 
deleted");
+        assertFalse(Files.exists(testFile(TEST_FILE_NAME_1)), "File should 
been deleted");
     }
 
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                
from(fileUri("?initialDelay=0&delay=10&fileName=report.txt&delete=true"))
+                from(fileUri("?initialDelay=0&delay=10&fileName=" + 
TEST_FILE_NAME_1 + "&delete=true"))
                         .convertBodyTo(String.class).to("mock:result");
             }
         };
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotDeleteFileIfProcessFailsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotDeleteFileIfProcessFailsTest.java
index 276d3ebfc7c..eb29c105d1d 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotDeleteFileIfProcessFailsTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotDeleteFileIfProcessFailsTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.file;
 
+import java.util.UUID;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -24,11 +26,12 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
 public class FromFileDoNotDeleteFileIfProcessFailsTest extends 
ContextTestSupport {
+    private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + 
".txt";
 
     @Test
     public void testPollFileAndShouldNotBeDeleted() throws Exception {
         String body = "Hello World this file will NOT be deleted";
-        template.sendBodyAndHeader(fileUri(), body, Exchange.FILE_NAME, 
"hello.txt");
+        template.sendBodyAndHeader(fileUri(), body, Exchange.FILE_NAME, 
TEST_FILE_NAME);
 
         MockEndpoint mock = getMockEndpoint("mock:error");
         // it could potentially retry the file on the 2nd poll and then fail
@@ -40,7 +43,7 @@ public class FromFileDoNotDeleteFileIfProcessFailsTest 
extends ContextTestSuppor
         oneExchangeDone.matchesWaitTime();
 
         // assert the file is deleted
-        assertFileExists(testFile("hello.txt"));
+        assertFileExists(testFile(TEST_FILE_NAME));
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotMoveFileIfProcessFailsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotMoveFileIfProcessFailsTest.java
index a2c0eb38359..bc606094838 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotMoveFileIfProcessFailsTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FromFileDoNotMoveFileIfProcessFailsTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.file;
 
+import java.util.UUID;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -24,11 +26,12 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
 public class FromFileDoNotMoveFileIfProcessFailsTest extends 
ContextTestSupport {
+    private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + 
".txt";
 
     @Test
     public void testPollFileAndShouldNotBeMoved() throws Exception {
         String body = "Hello World this file will NOT be moved";
-        template.sendBodyAndHeader(fileUri(), body, Exchange.FILE_NAME, 
"hello.txt");
+        template.sendBodyAndHeader(fileUri(), body, Exchange.FILE_NAME, 
TEST_FILE_NAME);
 
         MockEndpoint mock = getMockEndpoint("mock:error");
         // it could potentially retry the file on the 2nd poll and then fail
@@ -40,7 +43,7 @@ public class FromFileDoNotMoveFileIfProcessFailsTest extends 
ContextTestSupport
         oneExchangeDone.matchesWaitTime();
 
         // assert the file is not moved
-        assertFileExists(testFile("hello.txt"));
+        assertFileExists(testFile(TEST_FILE_NAME));
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
index e07235bafa9..7b9a3428ffb 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.file;
 
 import java.nio.file.Files;
 import java.util.HashMap;
+import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -33,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
  * Simple unit test to consume a new file
  */
 public class NewFileConsumeTest extends ContextTestSupport {
+    private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + 
".txt";
 
     private final CountDownLatch latch = new CountDownLatch(1);
 
@@ -48,7 +50,7 @@ public class NewFileConsumeTest extends ContextTestSupport {
 
         // create a file to consume
         Files.createDirectories(testDirectory());
-        Files.write(testFile("hello.txt"), "Hello World".getBytes());
+        Files.write(testFile(TEST_FILE_NAME), "Hello World".getBytes());
 
         Endpoint endpoint = comp.createEndpoint(fileUri(), 
testDirectory().toString(),
                 new HashMap<>());
@@ -59,12 +61,13 @@ public class NewFileConsumeTest extends ContextTestSupport {
             latch.countDown();
         });
 
-        assertFileExists(testFile("hello.txt"));
+        assertFileExists(testFile(TEST_FILE_NAME));
 
         consumer.start();
         latch.await(5, TimeUnit.SECONDS);
 
         consumer.stop();
+        comp.close();
     }
 
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
index dfa6d41cd2f..85a0e61e7b3 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.converter;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
+import java.util.UUID;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -28,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 public class NIOConverterTest extends ContextTestSupport {
+    private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + 
".txt";
 
     @Test
     public void testToByteArray() {
@@ -94,9 +96,9 @@ public class NIOConverterTest extends ContextTestSupport {
 
     @Test
     public void testToByteBufferFile() throws Exception {
-        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
-        ByteBuffer bb = 
NIOConverter.toByteBuffer(testFile("hello.txt").toFile());
+        ByteBuffer bb = 
NIOConverter.toByteBuffer(testFile(TEST_FILE_NAME).toFile());
         assertNotNull(bb);
 
         assertEquals("Hello World", NIOConverter.toString(bb, null));
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/issues/FilePollingConsumerIssueTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/issues/FilePollingConsumerIssueTest.java
index 1b28ae3eb2c..52b48b7a65e 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/issues/FilePollingConsumerIssueTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/issues/FilePollingConsumerIssueTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.issues;
 
+import java.util.UUID;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
@@ -26,18 +28,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 public class FilePollingConsumerIssueTest extends ContextTestSupport {
+    private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + 
".txt";
 
     @Test
     public void testFilePollingConsumer() throws Exception {
-        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
-        Endpoint endpoint = 
context.getEndpoint(fileUri("?initialDelay=0&delay=10&fileName=hello.txt"));
+        Endpoint endpoint = 
context.getEndpoint(fileUri("?initialDelay=0&delay=10&fileName=" + 
TEST_FILE_NAME));
         PollingConsumer consumer = endpoint.createPollingConsumer();
         consumer.start();
         Exchange exchange = consumer.receive(5000);
         assertNotNull(exchange);
 
-        assertEquals("hello.txt", 
exchange.getIn().getHeader(Exchange.FILE_NAME, String.class));
+        assertEquals(TEST_FILE_NAME, 
exchange.getIn().getHeader(Exchange.FILE_NAME, String.class));
         assertEquals("Hello World", exchange.getIn().getBody(String.class));
 
         consumer.stop();
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java 
b/core/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
index 21fa6942536..f8765cbee59 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.UUID;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.ExpressionIllegalSyntaxException;
@@ -39,6 +40,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  * Unit test for File Language.
  */
 public class FileLanguageTest extends LanguageTestSupport {
+    private static final String TEST_FILE_NAME_NOEXT_1 = "hello" + 
UUID.randomUUID();
+    private static final String TEST_FILE_NAME_1 = TEST_FILE_NAME_NOEXT_1 + 
".txt";
+    private static final String TEST_FILE_NAME_NOEXT_2 = "MyBigFile" + 
UUID.randomUUID();
+    private static final String TEST_FILE_NAME_2 = TEST_FILE_NAME_NOEXT_2 + 
".txt";
 
     private File file;
 
@@ -56,7 +61,7 @@ public class FileLanguageTest extends LanguageTestSupport {
 
     @Test
     public void testConstantExpression() {
-        assertExpression("MyBigFile.txt", "MyBigFile.txt");
+        assertExpression(TEST_FILE_NAME_2, TEST_FILE_NAME_2);
     }
 
     @Test
@@ -81,11 +86,11 @@ public class FileLanguageTest extends LanguageTestSupport {
         assertExpression("${file:name.ext}", "txt");
         assertExpression("${file:name.ext.single}", "txt");
         assertExpression("${file:name}", "test" + File.separator + 
file.getName());
-        assertExpression("${file:name.noext}", "test" + File.separator + 
"hello");
-        assertExpression("${file:name.noext.single}", "test" + File.separator 
+ "hello");
+        assertExpression("${file:name.noext}", "test" + File.separator + 
TEST_FILE_NAME_NOEXT_1);
+        assertExpression("${file:name.noext.single}", "test" + File.separator 
+ TEST_FILE_NAME_NOEXT_1);
         assertExpression("${file:onlyname}", file.getName());
-        assertExpression("${file:onlyname.noext}", "hello");
-        assertExpression("${file:onlyname.noext.single}", "hello");
+        assertExpression("${file:onlyname.noext}", TEST_FILE_NAME_NOEXT_1);
+        assertExpression("${file:onlyname.noext.single}", 
TEST_FILE_NAME_NOEXT_1);
         assertExpression("${file:parent}", file.getParent());
         assertExpression("${file:path}", file.getPath());
         assertExpression("${file:absolute}", FileUtil.isAbsolute(file));
@@ -103,9 +108,9 @@ public class FileLanguageTest extends LanguageTestSupport {
         assertExpression("$simple{file:ext}", "txt");
         assertExpression("$simple{file:name.ext}", "txt");
         assertExpression("$simple{file:name}", "test" + File.separator + 
file.getName());
-        assertExpression("$simple{file:name.noext}", "test" + File.separator + 
"hello");
+        assertExpression("$simple{file:name.noext}", "test" + File.separator + 
TEST_FILE_NAME_NOEXT_1);
         assertExpression("$simple{file:onlyname}", file.getName());
-        assertExpression("$simple{file:onlyname.noext}", "hello");
+        assertExpression("$simple{file:onlyname.noext}", 
TEST_FILE_NAME_NOEXT_1);
         assertExpression("$simple{file:parent}", file.getParent());
         assertExpression("$simple{file:path}", file.getPath());
         assertExpression("$simple{file:absolute}", FileUtil.isAbsolute(file));
@@ -127,7 +132,7 @@ public class FileLanguageTest extends LanguageTestSupport {
         assertExpression("backup-${date:file:yyyyMMdd}", "backup-" + expected);
 
         assertExpression("backup-${date:header.birthday:yyyyMMdd}", 
"backup-19740420");
-        assertExpression("hello-${date:header.special:yyyyMMdd}", 
"hello-20080808");
+        assertExpression(TEST_FILE_NAME_NOEXT_1 + 
"-${date:header.special:yyyyMMdd}", TEST_FILE_NAME_NOEXT_1 + "-20080808");
 
         assertThrows(IllegalArgumentException.class,
                 () -> 
this.assertExpression("nodate-${date:header.xxx:yyyyMMdd}", null),
@@ -143,7 +148,8 @@ public class FileLanguageTest extends LanguageTestSupport {
         assertExpression("backup-$simple{date:file:yyyyMMdd}", "backup-" + 
expected);
 
         assertExpression("backup-$simple{date:header.birthday:yyyyMMdd}", 
"backup-19740420");
-        assertExpression("hello-$simple{date:header.special:yyyyMMdd}", 
"hello-20080808");
+        assertExpression(TEST_FILE_NAME_NOEXT_1 + 
"-$simple{date:header.special:yyyyMMdd}",
+                TEST_FILE_NAME_NOEXT_1 + "-20080808");
 
         assertThrows(IllegalArgumentException.class,
                 () -> 
this.assertExpression("nodate-$simple{date:header.xxx:yyyyMMdd}", null),
@@ -152,16 +158,17 @@ public class FileLanguageTest extends LanguageTestSupport 
{
 
     @Test
     public void testSimpleAndFile() {
-        assertExpression("backup-${in.header.foo}-${file:name.noext}.bak", 
"backup-abc-test" + File.separator + "hello.bak");
-        assertExpression("backup-${in.header.foo}-${file:onlyname.noext}.bak", 
"backup-abc-hello.bak");
+        assertExpression("backup-${in.header.foo}-${file:name.noext}.bak",
+                "backup-abc-test" + File.separator + TEST_FILE_NAME_NOEXT_1 + 
".bak");
+        assertExpression("backup-${in.header.foo}-${file:onlyname.noext}.bak", 
"backup-abc-" + TEST_FILE_NAME_NOEXT_1 + ".bak");
     }
 
     @Test
     public void testSimpleAndFileAndBean() {
         
assertExpression("backup-${in.header.foo}-${bean:generator}-${file:name.noext}.bak",
-                "backup-abc-generatorbybean-test" + File.separator + 
"hello.bak");
+                "backup-abc-generatorbybean-test" + File.separator + 
TEST_FILE_NAME_NOEXT_1 + ".bak");
         
assertExpression("backup-${in.header.foo}-${bean:generator}-${file:onlyname.noext}.bak",
-                "backup-abc-generatorbybean-hello.bak");
+                "backup-abc-generatorbybean-" + TEST_FILE_NAME_NOEXT_1 + 
".bak");
     }
 
     @Test
@@ -172,13 +179,13 @@ public class FileLanguageTest extends LanguageTestSupport 
{
 
     @Test
     public void testNoEscapeAllowed() {
-        exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
-        assertExpression("target\\newdir\\onwindows\\${file:name}", 
"target\\newdir\\onwindows\\hello.txt");
+        exchange.getIn().setHeader(Exchange.FILE_NAME, TEST_FILE_NAME_1);
+        assertExpression("target\\newdir\\onwindows\\${file:name}", 
"target\\newdir\\onwindows\\" + TEST_FILE_NAME_1);
     }
 
     @Test
     public void testFileNameDoubleExtension() {
-        file = testFile("test/bigfile.tar.gz").toFile();
+        file = testFile("test/" + TEST_FILE_NAME_NOEXT_2 + ".tar.gz").toFile();
 
         String uri = fileUri("?fileExist=Override");
         GenericFile<File> gf = 
FileConsumer.asGenericFile(testDirectory().toString(), file, null, false);
@@ -188,8 +195,8 @@ public class FileLanguageTest extends LanguageTestSupport {
         Exchange answer = endpoint.createExchange(gf);
         endpoint.configureMessage(gf, answer.getIn());
 
-        assertEquals("bigfile.tar.gz", file.getName());
-        assertExpression(answer, "${file:onlyname}", "bigfile.tar.gz");
+        assertEquals(TEST_FILE_NAME_NOEXT_2 + ".tar.gz", file.getName());
+        assertExpression(answer, "${file:onlyname}", TEST_FILE_NAME_NOEXT_2 + 
".tar.gz");
         assertExpression(answer, "${file:ext}", "tar.gz");
     }
 
@@ -197,10 +204,10 @@ public class FileLanguageTest extends LanguageTestSupport 
{
     public Exchange createExchange() {
         // create the file
         String uri = "file://" + testDirectory().toString() + 
"?fileExist=Override";
-        template.sendBodyAndHeader(uri, "Hello World", Exchange.FILE_NAME, 
"test/hello.txt");
+        template.sendBodyAndHeader(uri, "Hello World", Exchange.FILE_NAME, 
"test/" + TEST_FILE_NAME_1);
 
         // get the file handle
-        file = testDirectory().resolve("test/hello.txt").toFile();
+        file = testDirectory().resolve("test/" + TEST_FILE_NAME_1).toFile();
         GenericFile<File> gf = 
FileConsumer.asGenericFile(testDirectory().toString(), file, null, false);
 
         FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
@@ -240,7 +247,7 @@ public class FileLanguageTest extends LanguageTestSupport {
 
     @Test
     public void testConstantFilename() {
-        assertExpression("hello.txt", "hello.txt");
+        assertExpression(TEST_FILE_NAME_1, TEST_FILE_NAME_1);
     }
 
     public static class MyFileNameGenerator {

Reply via email to