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.javamail;
20  
21  import java.io.ByteArrayInputStream;
22  import java.util.Properties;
23  import java.util.concurrent.CountDownLatch;
24  import java.util.concurrent.TimeUnit;
25  
26  import javax.mail.Folder;
27  import javax.mail.Message;
28  import javax.mail.Session;
29  import javax.mail.Store;
30  import javax.mail.URLName;
31  import javax.mail.event.MessageCountEvent;
32  import javax.mail.event.MessageCountListener;
33  import javax.mail.internet.MimeMessage;
34  
35  import junit.framework.Assert;
36  import junit.framework.TestCase;
37  
38  /***
39   * Verify basic features of the javamaildir provider.
40   * 
41   * See http://javamaildir.sourceforge.net/
42   * 
43   * @author rnewson
44   */
45  public final class MaildirProviderTest extends TestCase {
46  
47      private static final String MAILDIR_PROTOCOL = "maildir";
48  
49      private static final String MAILDIR_PREFIX = MAILDIR_PROTOCOL + ":///";
50  
51      private Session session;
52  
53      private String tempDir;
54  
55      protected void setUp() throws Exception {
56          final Properties properties = new Properties();
57          properties.setProperty("mail.debug", "true");
58          session = Session.getInstance(properties);
59          tempDir = System.getProperty("java.io.tmpdir");
60      }
61  
62      public void testMaildirProvider() throws Exception {
63          Assert.assertNotNull(session.getProvider(MAILDIR_PROTOCOL));
64      }
65  
66      public void testMaildirStore() throws Exception {
67          Assert.assertNotNull(session.getStore(new URLName(MAILDIR_PREFIX + tempDir + "/maildirtest")));
68      }
69  
70      public void testMaildirFolder() throws Exception {
71          final Store store = session.getStore(new URLName(MAILDIR_PREFIX + tempDir + "/maildirtest"));
72          Assert.assertNotNull(store.getFolder("INBOX"));
73      }
74  
75      public void testMaildirCreateFolder() throws Exception {
76          final URLName url = new URLName(MAILDIR_PREFIX + tempDir + "/maildirtest");
77          final Store store = session.getStore(url);
78          final Folder root = store.getDefaultFolder();
79          final Folder folder = root.getFolder("test");
80  
81          if (folder.exists()) {
82              folder.delete(false);
83          }
84  
85          Assert.assertFalse("Folder should not already exist.", folder.exists());
86          Assert.assertTrue("Folder should be successfully created.", folder.create(Folder.HOLDS_MESSAGES));
87          Assert.assertTrue("Folder should now exist.", folder.exists());
88      }
89  
90      public void testMaildirAppendMessage() throws Exception {
91          final URLName url = new URLName(MAILDIR_PREFIX + tempDir + "/maildirtest");
92          final Store store = session.getStore(url);
93          store.connect();
94          final Folder root = store.getDefaultFolder();
95          final Folder folder = root.getFolder("secondtest");
96  
97          if (!folder.exists()) {
98              folder.create(Folder.HOLDS_MESSAGES);
99          }
100 
101         final Message message = new MimeMessage(session, new ByteArrayInputStream("Subject: Test\r\n\r\n".getBytes("US-ASCII")));
102         final Message[] messages = new Message[] { message };
103 
104         final CountDownLatch latch = new CountDownLatch(1);
105 
106         final MessageCountListener listener = new MessageCountListener() {
107 
108             public void messagesAdded(final MessageCountEvent e) {
109                 Assert.assertEquals(MessageCountEvent.ADDED, e.getType());
110                 latch.countDown();
111             }
112 
113             public void messagesRemoved(final MessageCountEvent e) {
114                 throw new UnsupportedOperationException();
115             }
116         };
117 
118         folder.addMessageCountListener(listener);
119         folder.open(Folder.READ_WRITE);
120         try {
121             folder.appendMessages(messages);
122         } finally {
123             folder.close(false);
124         }
125 
126         if (!latch.await(1, TimeUnit.SECONDS)) {
127             fail("Event did not fire.");
128         }
129     }
130 
131 }