1 5 package org.exoplatform.services.communication.message.test; 6 7 import java.util.List ; 8 import java.util.Iterator ; 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 20 public class TestMessageService extends BasicTestCase { 21 static private String EMAIL = "exo.platform@laposte.net" ; 22 static protected MessageService service_ ; 23 24 public TestMessageService(String name) { 25 super(name); 26 } 27 28 public void setUp() throws Exception { 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 { 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 { 44 Account account = createAccount("accountName", "userName"); 46 service_.createAccount(account); 47 List accounts = service_.getAccounts("userName"); 49 assertEquals(1, accounts.size()); 50 for (Iterator 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 service_.removeAccount((Account) accounts.get(0)); 57 accounts = service_.getAccounts("userName"); 58 assertTrue(accounts.isEmpty()); 59 List folders = service_.getFolders(account.getId()); 60 assertTrue(folders.isEmpty()); 61 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 { 78 Account account = createAccount("accountName", "userName"); 79 service_.createAccount(account); 80 Folder folder = createFolder("folderName"); 82 service_.createFolder(account, folder); 83 assertSame(folder, service_.getFolder(account, "folderName")); 84 service_.removeFolder(service_.getFolder(account, "folderName")); 86 List folders = service_.getFolders(account.getId()) ; 87 assertEquals("Expect 0 folder in the account", 4, folders.size()) ; 88 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 { 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 129 service_.removeAccount(senderAccount) ; 130 service_.removeAccount(receiverAccount) ; 131 } 132 133 149 165 166 private void runMessageOperations(Account senderAccount, 167 Account receiverAccount, 168 Message m, long delay) throws Exception { 169 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 attachments = service_.getAttachments((Message) messages.currentPage().get(0)) ; 186 188 Folder senderInbox = service_.getFolder(senderAccount, MessageService.SENT_FOLDER); 189 messages = service_.getMessages(senderInbox) ; 190 assertEquals("Expect 1 message", 1, messages.getAvailable()) ; 191 192 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 service_.removeMessage((Message) messages.currentPage().get(0)) ; 204 messages = service_.getMessages(senderTrash) ; 205 assertEquals("Expect 0 message", 0, messages.getAvailable()) ; 206 Thread.sleep(1000) ; 208 } 209 210 private Account createAccount(String accountName, String 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 name) { 222 Folder folder = service_.createFolderInstance(); 223 folder.setName(name); 224 return folder; 225 } 226 227 private Message createMessage(String 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 |