KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > message > test > TestMessageService


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.message.test;
6
7 import java.util.List JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import org.exoplatform.commons.utils.PageList;
10 import org.exoplatform.container.PortalContainer;
11 import org.exoplatform.services.communication.message.*;
12 import org.exoplatform.services.database.HibernateService;
13 import org.exoplatform.test.BasicTestCase;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @author Benjamin Mestrallet (benjamin.mestrallet@exoplatform.com)
17  * @since Aug 28, 2004
18  * @version $Id: TestMessageService.java,v 1.16 2004/11/03 04:24:57 tuan08 Exp $
19  */

20 public class TestMessageService extends BasicTestCase {
21   static private String JavaDoc EMAIL = "exo.platform@laposte.net" ;
22   static protected MessageService service_ ;
23
24   public TestMessageService(String JavaDoc name) {
25     super(name);
26   }
27
28   public void setUp() throws Exception JavaDoc {
29     if (service_ == null) {
30       PortalContainer pcontainer = PortalContainer.getInstance();
31       service_ =
32         (MessageService) pcontainer.getComponentInstanceOfType(MessageService.class) ;
33     }
34   }
35
36   public void tearDown() throws Exception JavaDoc {
37     PortalContainer manager = PortalContainer.getInstance();
38     HibernateService hservice =
39         (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ;
40     hservice.closeSession();
41   }
42
43   public void testAccountOperations() throws Exception JavaDoc {
44     //test creates accounts
45
Account account = createAccount("accountName", "userName");
46     service_.createAccount(account);
47     //test getAccounts by user name
48
List JavaDoc accounts = service_.getAccounts("userName");
49     assertEquals(1, accounts.size());
50     for (Iterator JavaDoc iterator = accounts.iterator(); iterator.hasNext();) {
51       Account account1 = (Account) iterator.next();
52       assertTrue(account1.getProperty(Account.SERVER_SETTING_HOSTNAME) != null) ;
53       assertSame(account, account1);
54     }
55     //test remove account
56
service_.removeAccount((Account) accounts.get(0));
57     accounts = service_.getAccounts("userName");
58     assertTrue(accounts.isEmpty());
59     List JavaDoc folders = service_.getFolders(account.getId());
60     assertTrue(folders.isEmpty());
61     //test multiple accounts per user
62
account = createAccount("accountName", "userName");
63     service_.createAccount(account);
64     account = createAccount("accountName2", "userName");
65     service_.createAccount(account);
66     account = createAccount("accountName3", "userName");
67     service_.createAccount(account);
68     accounts = service_.getAccounts("userName");
69     assertEquals(3, accounts.size());
70     service_.removeAccount((Account) accounts.get(0));
71     service_.removeAccount((Account) accounts.get(1));
72     service_.removeAccount((Account) accounts.get(2));
73     accounts = service_.getAccounts("userName");
74     assertTrue(accounts.isEmpty());
75   }
76
77   public void testFolderOperations() throws Exception JavaDoc {
78     Account account = createAccount("accountName", "userName");
79     service_.createAccount(account);
80     // test create new folder
81
Folder folder = createFolder("folderName");
82     service_.createFolder(account, folder);
83     assertSame(folder, service_.getFolder(account, "folderName"));
84     //test remove folder
85
service_.removeFolder(service_.getFolder(account, "folderName"));
86     List JavaDoc folders = service_.getFolders(account.getId()) ;
87     assertEquals("Expect 0 folder in the account", 4, folders.size()) ;
88     //Test multiple folders
89
Folder folder1 = createFolder("folderName");
90     service_.createFolder(account, folder1);
91     Folder folder2 = createFolder("folderName2");
92     service_.createFolder(account, folder2);
93     Folder folder3 = createFolder("folderName3");
94     service_.createFolder(account, folder3);
95
96     folders = service_.getFolders(account.getId());
97     assertEquals("Expect 7 folder in the account", 7, folders.size());
98     service_.removeFolder(folder1);
99     service_.removeFolder(folder2);
100     service_.removeFolder(folder3);
101     folders = service_.getFolders(account.getId());
102     assertTrue(4 == folders.size());
103     service_.removeAccount(account) ;
104     folders = service_.getFolders(account.getId());
105     assertTrue(0 == folders.size());
106   }
107
108   
109   public void testStandaloneMessageProtocolProtocolOperations() throws Exception JavaDoc {
110     Account senderAccount = createAccount("sender", "sender");
111     senderAccount.setProtocol(MessageService.STANDALONE_PROTOCOL) ;
112     service_.createAccount(senderAccount);
113     
114     Account receiverAccount = createAccount("receiver", "receiver");
115     receiverAccount.setProtocol(MessageService.STANDALONE_PROTOCOL) ;
116     service_.createAccount(receiverAccount);
117     
118     Message m = createMessage("receiver#receiver") ;
119     runMessageOperations(senderAccount, receiverAccount, m, 0) ;
120     //assertTrue(m.hasFlag(MessageHeader.ANSWERED_FLAG));
121
//assertTrue(m.hasFlag(MessageHeader.RECENT_FLAG));
122
//assertTrue(!m.hasFlag(MessageHeader.DELETED_FLAG));
123
//assertTrue(m.isNew());
124
//m.removeFlag(MessageHeader.RECENT_FLAG);
125
//m.addFlag(MessageHeader.SEEN_FLAG);
126
//assertTrue(!m.hasFlag(MessageHeader.RECENT_FLAG));
127
//assertTrue(!m.isNew());
128

129     service_.removeAccount(senderAccount) ;
130     service_.removeAccount(receiverAccount) ;
131   }
132   
133   /*
134   public void testPOP3MessageProtocolProtocolOperations() throws Exception {
135     Account account = createAccount("account", "account");
136     account.setProtocol(MessageService.POP3_PROTOCOL) ;
137     account.setReplyToAddress("exo.platform@laposte.net") ;
138     account.setProperty(Account.SERVER_SETTING_HOSTNAME, "pop.laposte.net") ;
139     account.setProperty(Account.SERVER_SETTING_USERNAME, "exo.platform") ;
140     account.setProperty(Account.SERVER_SETTING_PASSWORD, "exo2004") ;
141     service_.createAccount(account);
142     
143     Message m = createMessage("exo.platform@laposte.net") ;
144     runMessageOperations(account, account, m, 5000) ;
145
146     service_.removeAccount(account) ;
147   }
148   */

149   /*
150   public void testIMAPMessageProtocolProtocolOperations() throws Exception {
151     Account account = createAccount("account", "account");
152     account.setProtocol(MessageService.IMAP_PROTOCOL) ;
153     account.setReplyToAddress("exo.platform@laposte.net") ;
154     account.setProperty(Account.SERVER_SETTING_HOSTNAME, "imap.laposte.net") ;
155     account.setProperty(Account.SERVER_SETTING_USERNAME, "exo.platform") ;
156     account.setProperty(Account.SERVER_SETTING_PASSWORD, "exo2004") ;
157     service_.createAccount(account);
158     
159     Message m = createMessage("exo.platform@laposte.net") ;
160     runMessageOperations(account, account, m, 5000) ;
161
162     service_.removeAccount(account) ;
163   }
164   */

165   
166   private void runMessageOperations(Account senderAccount,
167                                     Account receiverAccount,
168                                     Message m, long delay) throws Exception JavaDoc {
169     //test send message
170
service_.sendMessage(senderAccount, m) ;
171     if(delay > 0) Thread.sleep(delay) ;
172     service_.synchronizeAccount(receiverAccount) ;
173     Folder receiverInbox = service_.getFolder(receiverAccount, MessageService.INBOX_FOLDER);
174     PageList messages = service_.getMessages(receiverInbox) ;
175     if(messages.getAvailable() != 1) {
176       fail("Expect one message. Maybe there is some delay on the smpt mail " +
177            "server so you do not get the message, or you have other message in or account " +
178            "Number of message in your account : " + messages.getAvailable());
179     }
180     if(service_.countNewMessages(receiverAccount) != 1) {
181       fail("Expect one message. Maybe there is some delay on the smpt mail " +
182            "server so you do not get the message, or you have other message in or account " +
183            "Number of message in your account : " + messages.getAvailable());
184     }
185     List JavaDoc attachments = service_.getAttachments((Message) messages.currentPage().get(0)) ;
186     //assertEquals("Expect 1 attachment", 1, attachments.size()) ;
187

188     Folder senderInbox = service_.getFolder(senderAccount, MessageService.SENT_FOLDER);
189     messages = service_.getMessages(senderInbox) ;
190     assertEquals("Expect 1 message", 1, messages.getAvailable()) ;
191     
192     //test move message
193
Folder senderTrash = service_.getFolder(senderAccount, MessageService.TRASH_FOLDER);
194     service_.moveMessage(senderAccount, senderTrash, (Message) messages.currentPage().get(0)) ;
195     messages = service_.getMessages(senderInbox) ;
196     assertEquals("Expect 0 message", 0, messages.getAvailable()) ;
197     messages = service_.getMessages(senderTrash) ;
198     assertEquals("Expect 1 message", 1, messages.getAvailable()) ;
199     attachments = service_.getAttachments((Message) messages.currentPage().get(0)) ;
200     assertEquals("Expect 1 attachment", 1, attachments.size()) ;
201     
202     //test remove message
203
service_.removeMessage((Message) messages.currentPage().get(0)) ;
204     messages = service_.getMessages(senderTrash) ;
205     assertEquals("Expect 0 message", 0, messages.getAvailable()) ;
206     // make sure that the indexing thread is finished
207
Thread.sleep(1000) ;
208   }
209
210   private Account createAccount(String JavaDoc accountName, String JavaDoc username){
211     Account account = service_.createAccountInstance() ;
212     account.setAccountName(accountName);
213     account.setOwner(username) ;
214     account.setProtocol("imap");
215     account.setProperty(Account.SERVER_SETTING_HOSTNAME, "localhost");
216     account.setProperty(Account.SERVER_SETTING_USERNAME, username);
217     account.setProperty(Account.SERVER_SETTING_PASSWORD, "password");
218     return account;
219   }
220
221   private Folder createFolder(String JavaDoc name) {
222     Folder folder = service_.createFolderInstance();
223     folder.setName(name);
224     return folder;
225   }
226   
227   private Message createMessage(String JavaDoc to) {
228     Message m = service_.createMessageInstance() ;
229     m.setFrom(EMAIL) ;
230     m.setTo(to) ;
231     m.setSubject("subject") ;
232     m.setBody("Body message: This is a test") ;
233     Attachment attachment = service_.createAttachment() ;
234     attachment.setName("attachment") ;
235     attachment.setContent("this is an attachment".getBytes()) ;
236     m.addAttachment(attachment);
237     m.addFlag(MessageHeader.ANSWERED_FLAG);
238     m.addFlag(MessageHeader.RECENT_FLAG);
239     return m ;
240   }
241 }
Popular Tags