You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/04/22 05:03:34 UTC

[james-project] branch master updated: JAMES-3753 Fix FlowedMessageUtils.deflow() (#972)

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new d38a35996d JAMES-3753 Fix FlowedMessageUtils.deflow() (#972)
d38a35996d is described below

commit d38a35996d693ac927331cfa1238ce5768c0719f
Author: cketti <ck...@cketti.de>
AuthorDate: Fri Apr 22 07:03:30 2022 +0200

    JAMES-3753 Fix FlowedMessageUtils.deflow() (#972)
---
 .../org/apache/mailet/base/FlowedMessageUtils.java |   8 +-
 .../apache/mailet/base/FlowedMessageUtilsTest.java | 205 +++++++++++++++++++++
 2 files changed, 209 insertions(+), 4 deletions(-)

diff --git a/mailet/base/src/main/java/org/apache/mailet/base/FlowedMessageUtils.java b/mailet/base/src/main/java/org/apache/mailet/base/FlowedMessageUtils.java
index 45689081c3..36ee5c49c1 100644
--- a/mailet/base/src/main/java/org/apache/mailet/base/FlowedMessageUtils.java
+++ b/mailet/base/src/main/java/org/apache/mailet/base/FlowedMessageUtils.java
@@ -78,11 +78,11 @@ public final class FlowedMessageUtils {
             String line = i < lines.length ? lines[i] : null;
             int actualQuoteDepth = 0;
             
-            if (line != null && line.length() > 0) {
+            if (line != null) {
                 if (line.equals(RFC2646_SIGNATURE)) {
                     // signature handling (the previous line is not flowed)
                     resultLineFlowed = false;
-                } else if (line.charAt(0) == RFC2646_QUOTE) {
+                } else if (line.length() > 0 && line.charAt(0) == RFC2646_QUOTE) {
                     // Quote
                     actualQuoteDepth = 1;
                     while (actualQuoteDepth < line.length() && line.charAt(actualQuoteDepth) == RFC2646_QUOTE) {
@@ -95,7 +95,7 @@ public final class FlowedMessageUtils {
                     line = line.substring(actualQuoteDepth);
                     
                 } else {
-                    // id quote-depth changes wrt the first line then this is not flowed
+                    // if quote-depth changes wrt the first line then this is not flowed
                     if (resultLineQuoteDepth > 0) {
                         resultLineFlowed = false;
                     }
@@ -107,7 +107,7 @@ public final class FlowedMessageUtils {
                 }
                 
             // if the previous was the last then it was not flowed
-            } else if (line == null) {
+            } else {
                 resultLineFlowed = false;
             }
 
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/FlowedMessageUtilsTest.java b/mailet/base/src/test/java/org/apache/mailet/base/FlowedMessageUtilsTest.java
new file mode 100644
index 0000000000..61987bd030
--- /dev/null
+++ b/mailet/base/src/test/java/org/apache/mailet/base/FlowedMessageUtilsTest.java
@@ -0,0 +1,205 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.mailet.base;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class FlowedMessageUtilsTest {
+    private static final boolean DEL_SP_NO = false;
+    private static final boolean DEL_SP_YES = true;
+
+    @Test
+    void deflowWithSimpleText() {
+        String input = "Text that should be \r\n" +
+            "displayed on one line";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo("Text that should be displayed on one line");
+    }
+
+    @Test
+    void deflowWithOnlySomeLinesEndingInSpace() {
+        String input = "Text that \r\n" +
+            "should be \r\n" +
+            "displayed on \r\n" +
+            "one line.\r\n" +
+            "Text that should retain\r\n" +
+            "its line break.";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(
+            "Text that should be displayed on one line.\r\n" +
+                "Text that should retain\r\n" +
+                "its line break.");
+    }
+
+    @Test
+    void deflowWithNothingToDo() {
+        String input = "Line one\r\nLine two\r\n";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(input);
+    }
+
+    @Test
+    void deflowWithQuotedText() {
+        String input = "On [date], [user] wrote:\r\n" +
+            "> Text that should be displayed \r\n" +
+            "> on one line\r\n" +
+            "\r\n" +
+            "Some more text that should be \r\n" +
+            "displayed on one line.\r\n";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(
+            "On [date], [user] wrote:\r\n" +
+                "> Text that should be displayed on one line\r\n" +
+                "\r\n" +
+                "Some more text that should be displayed on one line.\r\n");
+    }
+
+    @Test
+    void deflowWithQuotedTextEndingInSpace() {
+        String input = "> Quoted text \r\n" +
+            "Some other text";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo("> Quoted text \r\nSome other text");
+    }
+
+    @Test
+    void deflowWithQuotedTextEndingInSpaceBeforeQuotedTextOfDifferentQuoteDepth() {
+        String input = ">> Depth 2 \r\n" +
+            "> Depth 1 \r\n" +
+            "> is here\r\n" +
+            "Some other text";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(
+            ">> Depth 2 \r\n" +
+                "> Depth 1 is here\r\n" +
+                "Some other text");
+    }
+
+    @Test
+    void deflowWithQuotedTextEndingInSpaceFollowedByEmptyLine() {
+        String input = "> Quoted \r\n" +
+            "\r\n" +
+            "Text";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(input);
+    }
+
+    @Test
+    void deflowWithDelSp() {
+        String input = "Text that is wrapped mid wo \r\nrd";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_YES);
+
+        assertThat(result).isEqualTo("Text that is wrapped mid word");
+    }
+
+    @Test
+    void deflowWithQuotedTextAndSpaceStuffingAndDelSp() {
+        String input = "> Quoted te \r\n" +
+            "> xt";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_YES);
+
+        assertThat(result).isEqualTo("> Quoted text");
+    }
+
+    @Test
+    void deflowWithSpaceStuffedSecondLine() {
+        String input = "Text that should be \r\n" +
+            " displayed on one line";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo("Text that should be displayed on one line");
+    }
+
+    @Test
+    void deflowWithOnlySpaceStuffing() {
+        String input = "Line 1\r\n" +
+            " Line 2\r\n" +
+            " Line 3\r\n";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo("Line 1\r\nLine 2\r\nLine 3\r\n");
+    }
+
+    @Test
+    void deflowWithQuotedSpaceStuffedSecondLine() {
+        String input = "> Text that should be \r\n" +
+            "> displayed on one line";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo("> Text that should be displayed on one line");
+    }
+
+    @Test
+    void deflowWithTextContainingSignature() {
+        String input = "Text that should be \r\n" +
+            "displayed on one line.\r\n" +
+            "\r\n" +
+            "-- \r\n" +
+            "Signature \r\n" +
+            "text";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(
+            "Text that should be displayed on one line.\r\n" +
+                "\r\n" +
+                "-- \r\n" +
+                "Signature text");
+    }
+
+    @Test
+    void deflowWithQuotedTextContainingSignature() {
+        String input = "> Text that should be \r\n" +
+            "> displayed on one line.\r\n" +
+            "> \r\n" +
+            "> -- \r\n" +
+            "> Signature \r\n" +
+            "> text";
+
+        String result = FlowedMessageUtils.deflow(input, DEL_SP_NO);
+
+        assertThat(result).isEqualTo(
+            "> Text that should be displayed on one line.\r\n" +
+                "> \r\n" +
+                "> -- \r\n" +
+                "> Signature text");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org