1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.mailsender

File MailSenderPluginTest.java

 

Code metrics

0
47
12
1
165
112
14
0.3
3.92
12
1.17

Classes

Class Line # Actions
MailSenderPluginTest 32 47 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.plugin.mailsender;
21   
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.BeforeClass;
25    import org.junit.Test;
26   
27    /**
28    * Unit tests for the {@link MailSenderPlugin mailsender plugin}.
29    *
30    * @version $Id: 3913ee235a93fd4137a3c565b68d673efdc5f1fa $
31    */
 
32    public class MailSenderPluginTest
33    {
34    /** The empty {@link Mail} object used for testing. */
35    private Mail mail;
36   
37    /** The {@link MailSenderPlugin plugin} instance used for testing. */
38    private static MailSenderPlugin plugin;
39   
 
40  1 toggle @BeforeClass
41    public static void setUpPlugin()
42    {
43  1 plugin = new MailSenderPlugin("mail", MailSenderPlugin.class.getCanonicalName(), null);
44    }
45   
46    /** Setup: create a new {@code Mail} object and a plugin instance. */
 
47  10 toggle @Before
48    public void setUp()
49    {
50  10 this.mail = new Mail();
51    }
52   
53    /** Test that a {@code null} Mail throws an exception. */
 
54  1 toggle @Test
55    public void testParseRawMessageWithNullMail()
56    {
57  1 boolean thrown = false;
58  1 try {
59  1 plugin.parseRawMessage("Subject:Greetings!\n\nDear John,\nHello and Goodbye!", null);
60    } catch (IllegalArgumentException ex) {
61  1 thrown = true;
62    }
63  1 Assert.assertTrue(thrown);
64    }
65   
66    /** Test that a {@code null} message throws an exception. */
 
67  1 toggle @Test
68    public void testParseRawMessageWithNullMessage()
69    {
70  1 boolean thrown = false;
71  1 try {
72  1 plugin.parseRawMessage(null, this.mail);
73    } catch (IllegalArgumentException ex) {
74  1 thrown = true;
75    }
76  1 Assert.assertTrue(thrown);
77    }
78   
79    /** Test that a simple mail with no headers becomes the Mail's textPart. */
 
80  1 toggle @Test
81    public void testParseRawMessageWithSimpleMessage()
82    {
83  1 plugin.parseRawMessage("Dear John,\nHello and Goodbye!", this.mail);
84  1 Assert.assertEquals("Dear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
85  1 Assert.assertEquals(0, this.mail.getHeaders().size());
86   
87    }
88   
89    /** Test that the Subject header is treated as a special header and placed in the Mail correctly. */
 
90  1 toggle @Test
91    public void testParseRawMessageWithSubject()
92    {
93  1 plugin.parseRawMessage("Subject:Greetings!\n\nDear John,\nHello and Goodbye!", this.mail);
94  1 Assert.assertEquals("Dear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
95  1 Assert.assertEquals("Greetings!", this.mail.getSubject());
96  1 Assert.assertEquals(0, this.mail.getHeaders().size());
97    }
98   
99    /** Test that both Subject and From are detected as special headers. */
 
100  1 toggle @Test
101    public void testParseRawMessageWithSubjectAndFrom()
102    {
103  1 plugin
104    .parseRawMessage("Subject:Greetings!\nFrom:user@example.org\n\nDear John,\nHello and Goodbye!", this.mail);
105  1 Assert.assertEquals("Dear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
106  1 Assert.assertEquals("Greetings!", this.mail.getSubject());
107  1 Assert.assertEquals("user@example.org", this.mail.getFrom());
108  1 Assert.assertEquals(0, this.mail.getHeaders().size());
109    }
110   
111    /** Test that the first empty line marks the start of the body. */
 
112  1 toggle @Test
113    public void testParseRawMessageWithFakeFrom()
114    {
115  1 plugin.parseRawMessage("Subject:Greetings!\n\nFrom:user@example.org\n\nDear John,\nHello and Goodbye!",
116    this.mail);
117  1 Assert.assertEquals("From:user@example.org\r\n\r\nDear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
118  1 Assert.assertEquals("Greetings!", this.mail.getSubject());
119  1 Assert.assertNull(this.mail.getFrom());
120  1 Assert.assertEquals(0, this.mail.getHeaders().size());
121    }
122   
123    /** Test that the header name stops at the first colon. */
 
124  1 toggle @Test
125    public void testParseRawMessageWithColonInHeader()
126    {
127  1 plugin.parseRawMessage("Subject:Greetings:Human!\n\nDear John,\nHello and Goodbye!", this.mail);
128  1 Assert.assertEquals("Dear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
129  1 Assert.assertEquals("Greetings:Human!", this.mail.getSubject());
130  1 Assert.assertNull(this.mail.getFrom());
131  1 Assert.assertEquals(0, this.mail.getHeaders().size());
132    }
133   
134    /** Test that custom header are simply passed to the mail as-is. */
 
135  1 toggle @Test
136    public void testParseRawMessageWithExtraHeaders()
137    {
138  1 plugin.parseRawMessage("X-Header:Something extra!\n\nDear John,\nHello and Goodbye!", this.mail);
139  1 Assert.assertEquals("Dear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
140  1 Assert.assertNull(this.mail.getSubject());
141  1 Assert.assertEquals(1, this.mail.getHeaders().size());
142    }
143   
144    /** RFC 2822 allows headers to be split on multiple lines using "folding white spaces". */
 
145  1 toggle @Test
146    public void testParseRawMessageWithMultilineSubject()
147    {
148  1 plugin.parseRawMessage("Subject:Greetings\n from\n\thome\nFrom:user@example.org\n\nHello and Goodbye!",
149    this.mail);
150  1 Assert.assertEquals("Hello and Goodbye!\r\n", this.mail.getTextPart());
151  1 Assert.assertEquals("Greetings from\thome", this.mail.getSubject());
152  1 Assert.assertEquals("user@example.org", this.mail.getFrom());
153  1 Assert.assertEquals(0, this.mail.getHeaders().size());
154    }
155   
156    /** Headers can't contain spaces. Test that such lines are correctly used as the body. */
 
157  1 toggle @Test
158    public void testParseRawMessageWithFakeHeaders()
159    {
160  1 plugin.parseRawMessage("To Susan:Greetings!\n\nHello and Goodbye!", this.mail);
161  1 Assert.assertEquals("To Susan:Greetings!\r\n\r\nHello and Goodbye!\r\n", this.mail.getTextPart());
162  1 Assert.assertNull(this.mail.getSubject());
163  1 Assert.assertEquals(0, this.mail.getHeaders().size());
164    }
165    }