1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    * 
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program 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
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace.mina;
20  
21  import java.io.ByteArrayInputStream;
22  
23  import javax.mail.Folder;
24  import javax.mail.Message;
25  import javax.mail.MessagingException;
26  import javax.mail.Session;
27  import javax.mail.Store;
28  import javax.mail.internet.MimeMessage;
29  
30  import junit.framework.Assert;
31  import junit.framework.TestCase;
32  import net.sf.wallace.WallaceServerConfig;
33  
34  import org.springframework.context.ConfigurableApplicationContext;
35  import org.springframework.context.support.ClassPathXmlApplicationContext;
36  
37  /***
38   * @author rnewson
39   * 
40   * Wire everything up and try it out.
41   */
42  public class IntegrationTest extends TestCase {
43  
44      private ConfigurableApplicationContext context;
45  
46      private Session mailSession;
47  
48      protected void setUp() {
49          context = new ClassPathXmlApplicationContext("applicationContext.xml");
50          final WallaceServerConfig config = (WallaceServerConfig) context.getBean("wallaceServerConfig");
51          mailSession = config.getSession();
52      }
53  
54      protected void tearDown() {
55          context.close();
56      }
57  
58      public void testConnectSuccess() throws Exception {
59          final Store store = mailSession.getStore("imap");
60          try {
61              store.connect("localhost", 10143, "testuser", "password");
62          } finally {
63              store.close();
64          }
65      }
66  
67      public void testConnectFailure() throws Exception {
68          final Store store = mailSession.getStore("imap");
69          try {
70              store.connect("localhost", 10143, "testuser", "wrong_password");
71              fail("Expected MessagingException.");
72          } catch (MessagingException e) {
73              // Empty.
74          } finally {
75              store.close();
76          }
77      }
78  
79      public void testCreateMailbox() throws Exception {
80          final String username = "testuser";
81          final String mailboxName = "testCreateMailbox";
82  
83          final Store store = mailSession.getStore("imap");
84          store.connect("localhost", 10143, username, "password");
85          try {
86              final Folder root = store.getDefaultFolder();
87              final Folder folder = root.getFolder(mailboxName);
88              if (folder.exists()) {
89                  folder.delete(false);
90              }
91  
92              Assert.assertTrue("Unable to create folder", folder.create(Folder.HOLDS_MESSAGES));
93          } finally {
94              store.close();
95          }
96      }
97  
98      public void testAppendAMessage() throws Exception {
99          final Message message = new MimeMessage(mailSession, new ByteArrayInputStream("Subject: test\r\nBody text\r\n".getBytes()));
100         final Message[] messages = new Message[] { message };
101 
102         final Store store = mailSession.getStore("imap");
103         store.connect("localhost", 10143, "testuser", "password");
104         try {
105             final Folder root = store.getDefaultFolder();
106             final Folder folder = root.getFolder("testAppendAMessage");
107             try {
108                 if (folder.exists()) {
109                     folder.delete(false);
110                 }
111 
112                 folder.create(Folder.HOLDS_MESSAGES);
113                 folder.open(Folder.READ_WRITE);
114                 Assert.assertEquals(0, folder.getMessageCount());
115                 folder.appendMessages(messages);
116                 folder.open(Folder.READ_WRITE);
117                 Assert.assertEquals(1, folder.getNewMessageCount());
118             } finally {
119                 folder.close(false);
120             }
121         } finally {
122             store.close();
123         }
124     }
125 }