You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2022/03/31 09:47:38 UTC

[camel-quarkus] 06/43: file: Rewrite the charset related test #3627

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

ppalaga pushed a commit to branch 2.7.x
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit a39123c86f341a8dce912500ef18b8ae1ca532de
Author: aldettinger <al...@gmail.com>
AuthorDate: Mon Mar 21 16:26:40 2022 +0100

    file: Rewrite the charset related test #3627
---
 integration-tests/file/pom.xml                     | 21 +++++-----
 .../quarkus/component/file/it/FileResource.java    | 46 ++++++++++++++++++++++
 .../quarkus/component/file/it/FileRoutes.java      | 10 ++---
 .../camel/quarkus/component/file/it/FileTest.java  | 33 +++++-----------
 4 files changed, 70 insertions(+), 40 deletions(-)

diff --git a/integration-tests/file/pom.xml b/integration-tests/file/pom.xml
index e789499..731ceb4 100644
--- a/integration-tests/file/pom.xml
+++ b/integration-tests/file/pom.xml
@@ -59,6 +59,16 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-resteasy-jackson</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.hamcrest</groupId>
+                    <artifactId>hamcrest-core</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
 
         <!-- test dependencies -->
         <dependency>
@@ -71,17 +81,6 @@
             <artifactId>rest-assured</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.awaitility</groupId>
-            <artifactId>awaitility</artifactId>
-            <scope>test</scope>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.hamcrest</groupId>
-                    <artifactId>hamcrest-core</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
     </dependencies>
 
 
diff --git a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java
index e5b8f79..3f2ee87 100644
--- a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java
+++ b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java
@@ -16,9 +16,15 @@
  */
 package org.apache.camel.quarkus.component.file.it;
 
+import java.io.File;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 import javax.enterprise.context.ApplicationScoped;
@@ -33,11 +39,13 @@ import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import com.mchange.io.FileUtils;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
 import org.apache.camel.Exchange;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.awaitility.Awaitility;
 
 @Path("/file")
 @ApplicationScoped
@@ -138,4 +146,42 @@ public class FileResource {
         return producerTemplate.requestBody("direct:" + route, body, String.class);
     }
 
+    @Path("/writeThenReadFileWithCharsetShouldSucceed")
+    @GET
+    public void writeThenReadFileWithCharsetShouldSucceed() throws Exception {
+
+        // Delete any charset encoded files that would reside from a previous run
+        Files.deleteIfExists(Paths.get("target/charsetIsoRead/charsetEncodedFile.txt"));
+        Files.deleteIfExists(Paths.get("target/charsetIsoWrite/charsetEncodedFile.txt"));
+
+        // Using a charset that has few chance to be the default one on the build platform
+        String charsetName = "ISO-8859-1";
+        String unencodedContent = "A string with ð char";
+        byte[] encodedContent = unencodedContent.getBytes(charsetName);
+
+        // Produce in the folder named 'charsetIsoWrite' and check the content is encoded as expected
+        producerTemplate.request("file:target/charsetIsoWrite/?charset=" + charsetName, ex -> {
+            ex.getMessage().setHeader(Exchange.FILE_NAME, "charsetEncodedFile.txt");
+            ex.getMessage().setBody(unencodedContent);
+        });
+        Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
+            File file = new File("target/charsetIsoWrite/charsetEncodedFile.txt");
+            return Arrays.equals(encodedContent, FileUtils.getBytes(file));
+        });
+
+        // Move the encoded file to the read folder
+        java.nio.file.Path source = Paths.get("target/charsetIsoWrite/charsetEncodedFile.txt");
+        java.nio.file.Path destination = Paths.get("target/charsetIsoRead/charsetEncodedFile.txt");
+        Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE);
+
+        // Start the route to consume the encoded file
+        context.getRouteController().startRoute("charsetIsoRead");
+
+        // Check that the consumed file content has been decoded as expected
+        Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
+            String decodedContent = getFromMock("charsetIsoRead");
+            return unencodedContent.equals(decodedContent);
+        });
+    }
+
 }
diff --git a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java
index 891339c..0986c97 100644
--- a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java
+++ b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java
@@ -55,13 +55,11 @@ public class FileRoutes extends RouteBuilder {
                         .convertBodyTo(String.class)
                         .to("mock:" + CONSUME_BATCH);
 
-        from("file://target/charsetUTF8?initialDelay=0&delay=10&delete=true&charset=UTF-8")
+        from("file://target/charsetIsoRead?initialDelay=0&delay=10&delete=true&charset=ISO-8859-1")
+                .routeId("charsetIsoRead")
+                .autoStartup(false)
                 .convertBodyTo(String.class)
-                .to("mock:charsetUTF8");
-
-        from("file://target/charsetISO?initialDelay=0&delay=10&delete=true&charset=ISO-8859-1")
-                .convertBodyTo(String.class)
-                .to("mock:charsetISO");
+                .to("mock:charsetIsoRead");
 
         from("file://target/idempotent?idempotent=true&move=done/${file:name}&initialDelay=0&delay=10")
                 .convertBodyTo(String.class).to("mock:idempotent");
diff --git a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java
index 2922d7d..587c204 100644
--- a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java
+++ b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java
@@ -53,7 +53,6 @@ class FileTest {
     private static final String FILE_CONTENT_01 = "Hello1";
     private static final String FILE_CONTENT_02 = "Hello2";
     private static final String FILE_CONTENT_03 = "Hello3";
-    private static final String FILE_BODY_UTF8 = "Hello World \u4f60\u597d";
 
     private List<Path> pathsToDelete = new LinkedList<>();
 
@@ -85,26 +84,11 @@ class FileTest {
 
     @DisabledOnOs(value = OS.WINDOWS, disabledReason = "https://github.com/apache/camel-quarkus/issues/3530")
     @Test
-    public void charset() throws UnsupportedEncodingException {
-        // Create a new file
-        createFile(FILE_BODY_UTF8, "/file/create/charsetUTF8", "UTF-8", null);
-        createFile(FILE_BODY_UTF8, "/file/create/charsetISO", "UTF-8", null);
-
-        await().atMost(10, TimeUnit.SECONDS).until(
-                () -> RestAssured
-                        .get("/file/getFromMock/charsetUTF8")
-                        .then()
-                        .extract().asString(),
-                equalTo(FILE_BODY_UTF8));
-
-        // File content read as ISO-8859-1 has to have different content (because file contains some unknown
-        // characters)
-        await().atMost(10, TimeUnit.SECONDS).until(
-                () -> RestAssured
-                        .get("/file/getFromMock/charsetISO")
-                        .then()
-                        .extract().asString(),
-                equalTo(new String(FILE_BODY_UTF8.getBytes(), "ISO-8859-1")));
+    public void writeThenReadFileWithCharsetShouldSucceed() {
+        RestAssured
+                .get("/file/writeThenReadFileWithCharsetShouldSucceed")
+                .then()
+                .statusCode(204);
     }
 
     @Test
@@ -273,12 +257,15 @@ class FileTest {
     }
 
     private static String createFile(String content, String path) throws UnsupportedEncodingException {
-        return createFile(content.getBytes("UTF-8"), path, null, null);
+        return createFile(content, path, "UTF-8", null);
     }
 
     static String createFile(String content, String path, String charset, String prefix)
             throws UnsupportedEncodingException {
-        return createFile(content.getBytes(), path, charset, prefix);
+        if (charset == null) {
+            charset = "UTF-8";
+        }
+        return createFile(content.getBytes(charset), path, charset, prefix);
     }
 
     static String createFile(byte[] content, String path, String charset, String fileName) {