commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1801385 - in /commons/proper/email/trunk/src: main/java/org/apache/commons/mail/ test/java/org/apache/commons/mail/
Date Sun, 09 Jul 2017 19:41:59 GMT
Author: ggregory
Date: Sun Jul  9 19:41:59 2017
New Revision: 1801385

URL: http://svn.apache.org/viewvc?rev=1801385&view=rev
Log:
Replaces end-of-line characters with spaces in email subjects.

Modified:
    commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java
    commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailUtils.java
    commons/proper/email/trunk/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
    commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailTest.java
    commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailUtilsTest.java

Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java?rev=1801385&r1=1801384&r2=1801385&view=diff
==============================================================================
--- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java (original)
+++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java Sun Jul  9
19:41:59 2017
@@ -1231,15 +1231,14 @@ public abstract class Email
     }
 
     /**
-     * Set the email subject.
+     * Sets the email subject. Replaces end-of-line characters with spaces.
      *
      * @param aSubject A String.
      * @return An Email.
      * @since 1.0
      */
-    public Email setSubject(final String aSubject)
-    {
-        this.subject = aSubject;
+    public Email setSubject(final String aSubject) {
+        this.subject = EmailUtils.clearEndOfLineCharacters(aSubject);
         return this;
     }
 

Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailUtils.java?rev=1801385&r1=1801384&r2=1801385&view=diff
==============================================================================
--- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailUtils.java (original)
+++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailUtils.java Sun Jul
 9 19:41:59 2017
@@ -270,6 +270,16 @@ final class EmailUtils
     }
 
     /**
+     * Replaces end-of-line characters with spaces.
+     * 
+     * @param input the input string to be URL scanned.
+     * @return a clean string
+     */
+    static String clearEndOfLineCharacters(final String input) {
+        return input.replace('\n', ' ').replace('\r', ' ');
+    }
+    
+    /**
      * Encodes an input string according to RFC 2392. Unsafe characters are escaped.
      *
      * @param input the input string to be URL encoded

Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/AbstractEmailTest.java?rev=1801385&r1=1801384&r2=1801385&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
(original)
+++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
Sun Jul  9 19:41:59 2017
@@ -92,8 +92,16 @@ public abstract class AbstractEmailTest
             "A",
             "\uc5ec",
             "0123456789",
-            "012345678901234567890",
-            "\n"
+            "012345678901234567890"
+    };
+
+    /** Test characters not acceptable to email */
+    protected String[] testCharsInvalid =
+    {
+            "\n",
+            "\r",
+            "\r\n",
+            "\n\r",
     };
 
     /** Array of test strings */

Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailTest.java?rev=1801385&r1=1801384&r2=1801385&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailTest.java (original)
+++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailTest.java Sun Jul
 9 19:41:59 2017
@@ -971,14 +971,26 @@ public class EmailTest extends AbstractE
     }
 
     @Test
-    public void testSetSubject()
-    {
+    public void testSetSubjectValid() {
         for (final String validChar : testCharsValid) {
             email.setSubject(validChar);
             assertEquals(validChar, email.getSubject());
         }
     }
 
+    @Test
+    public void testSetSubjectInvalid() {
+        for (final String invalidChar : testCharsInvalid) {
+            email.setSubject(invalidChar);
+            assertNotEquals(invalidChar, email.getSubject());
+        }
+        assertEquals("abcdefg", email.setSubject("abcdefg").getSubject());
+        assertEquals("abc defg", email.setSubject("abc\rdefg").getSubject());
+        assertEquals("abc defg", email.setSubject("abc\ndefg").getSubject());
+        assertEquals("abc  defg", email.setSubject("abc\r\ndefg").getSubject());
+        assertEquals("abc  defg", email.setSubject("abc\n\rdefg").getSubject());
+    }
+
     @Test(expected = EmailException.class)
     public void testSendNoHostName() throws Exception
     {

Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailUtilsTest.java?rev=1801385&r1=1801384&r2=1801385&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailUtilsTest.java (original)
+++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailUtilsTest.java Sun
Jul  9 19:41:59 2017
@@ -28,11 +28,19 @@ import org.junit.Test;
  *
  * @since 1.3
  */
-public class EmailUtilsTest
-{
+public class EmailUtilsTest {
+
     @Test
-    public void testUrlEncoding() throws UnsupportedEncodingException
-    {
+    public void testClearEndOfLineCharacters() {
+        assertEquals("abcdefg", EmailUtils.clearEndOfLineCharacters("abcdefg"));
+        assertEquals("abc defg", EmailUtils.clearEndOfLineCharacters("abc\rdefg"));
+        assertEquals("abc defg", EmailUtils.clearEndOfLineCharacters("abc\ndefg"));
+        assertEquals("abc  defg", EmailUtils.clearEndOfLineCharacters("abc\r\ndefg"));
+        assertEquals("abc  defg", EmailUtils.clearEndOfLineCharacters("abc\n\rdefg"));
+    }
+
+    @Test
+    public void testUrlEncoding() throws UnsupportedEncodingException {
         assertEquals("abcdefg", EmailUtils.encodeUrl("abcdefg"));
         assertEquals("0123456789", EmailUtils.encodeUrl("0123456789"));
         assertEquals("Test%20CID", EmailUtils.encodeUrl("Test CID"));



Mime
View raw message