1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.message

File MessageMimeMessageFactoryTest.java

 

Code metrics

0
28
4
1
108
66
5
0.18
7
4
1.25

Classes

Class Line # Actions
MessageMimeMessageFactoryTest 45 28 0% 5 1
0.9687596.9%
 

Contributing tests

This file is covered by 3 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 org.xwiki.mail.internal.factory.message;
21   
22    import java.util.Properties;
23   
24    import javax.mail.Message;
25    import javax.mail.MessagingException;
26    import javax.mail.Session;
27    import javax.mail.internet.InternetAddress;
28    import javax.mail.internet.MimeMessage;
29   
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import static org.hamcrest.CoreMatchers.equalTo;
35    import static org.hamcrest.CoreMatchers.notNullValue;
36    import static org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertThat;
38    import static org.junit.Assert.fail;
39   
40    /**
41    * Unit tests for {@link org.xwiki.mail.internal.factory.message.MessageMimeMessageFactory}.
42    *
43    * @version $Id: bdf5543e335c50ef05110166983cfa7267bd8fb8 $
44    */
 
45    public class MessageMimeMessageFactoryTest
46    {
47    @Rule
48    public MockitoComponentMockingRule<MessageMimeMessageFactory> mocker =
49    new MockitoComponentMockingRule<>(MessageMimeMessageFactory.class);
50   
 
51  1 toggle @Test
52    public void createMessageWithBadSource() throws Exception
53    {
54  1 try {
55  1 this.mocker.getComponentUnderTest().createMessage("source", null);
56  0 fail("Should have thrown an exception");
57    } catch (MessagingException expected) {
58  1 assertEquals("Failed to create mime message from source [class java.lang.String]", expected.getMessage());
59    }
60    }
61   
 
62  1 toggle @Test
63    public void createMultipleMessages() throws Exception
64    {
65  1 MimeMessage source = new MimeMessage(Session.getInstance(new Properties()));
66  1 source.setFrom(new InternetAddress("localhost@xwiki.org"));
67  1 source.addRecipient(Message.RecipientType.TO, new InternetAddress("john@doe.com"));
68  1 source.setSubject("Subject");
69  1 source.setText("Content");
70   
71  1 MimeMessage first = mocker.getComponentUnderTest().createMessage(source, null);
72   
73  1 assertEqualMimeMessage(first, source);
74   
75  1 first.setFrom(new InternetAddress("jane@doe.com"));
76  1 first.addRecipient(Message.RecipientType.TO, new InternetAddress("jack@doe.com"));
77  1 first.setSubject("First subject");
78  1 first.setText("First content");
79   
80  1 MimeMessage second = mocker.getComponentUnderTest().createMessage(source, null);
81   
82    // Ensure second message is similar to source, and not to modified first
83  1 assertEqualMimeMessage(second, source);
84    }
85   
 
86  1 toggle @Test
87    public void ensureMessageReceiveSameMessageID() throws Exception
88    {
89  1 MimeMessage source = new MimeMessage(Session.getInstance(new Properties()));
90  1 source.setText("Content");
91   
92  1 MimeMessage first = mocker.getComponentUnderTest().createMessage(source, null);
93  1 MimeMessage second = mocker.getComponentUnderTest().createMessage(source, null);
94   
95    // Ensure second message is similar to source, and not to modified first
96  1 assertThat(first.getMessageID(), notNullValue());
97  1 assertThat(second.getMessageID(), notNullValue());
98  1 assertThat(first.getMessageID(), equalTo(second.getMessageID()));
99    }
100   
 
101  2 toggle private void assertEqualMimeMessage(MimeMessage message1, MimeMessage message2) throws Exception
102    {
103  2 assertThat(message1.getFrom(), equalTo(message2.getFrom()));
104  2 assertThat(message1.getAllRecipients(), equalTo(message2.getAllRecipients()));
105  2 assertThat(message1.getSubject(), equalTo(message2.getSubject()));
106  2 assertThat(message1.getContent(), equalTo(message2.getContent()));
107    }
108    }