1 19 20 package org.apache.james.mailboxmanager.torque.repository; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Date ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import javax.mail.MessagingException ; 32 import javax.mail.internet.MimeMessage ; 33 34 import org.apache.avalon.framework.configuration.Configuration; 35 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 36 import org.apache.james.mailboxmanager.MailboxManagerException; 37 import org.apache.james.mailboxmanager.MessageResult; 38 import org.apache.james.mailboxmanager.impl.GeneralMessageSetImpl; 39 import org.apache.james.mailboxmanager.mailbox.GeneralMailboxSession; 40 import org.apache.james.mailboxmanager.manager.MailboxManager; 41 import org.apache.james.mailboxmanager.mock.MockUser; 42 import org.apache.james.mailboxmanager.mock.TorqueMailboxManagerProviderSingleton; 43 import org.apache.james.mailboxmanager.redundant.AbstractMailRepositoryNativeTestCase; 44 import org.apache.james.mailboxmanager.repository.MailboxManagerMailRepository; 45 46 public class TorqueMailboxManagerMailRepositoryNativeTestCase extends 47 AbstractMailRepositoryNativeTestCase { 48 49 private static final String TUSER_INBOX = "#mail.tuser.INBOX"; 50 GeneralMailboxSession shadowMailbox = null; 51 52 protected void configureRepository() throws Exception { 53 TorqueMailboxManagerProviderSingleton 54 .getTorqueMailboxManagerProviderInstance().deleteEverything(); 55 MailboxManagerMailRepository mailboxManagerMailRepository = new MailboxManagerMailRepository(); 56 57 DefaultConfigurationBuilder db = new DefaultConfigurationBuilder(); 58 59 Configuration conf = db 60 .build( 61 new ByteArrayInputStream ( 62 ("<repository destinationURL=\"mailboxmanager://#mail/tuser\" postfix=\".INBOX\" translateDelimiters=\"true\" type=\"MAIL\"></repository>") 63 .getBytes()), "repository"); 64 mailboxManagerMailRepository.configure(conf); 65 mailboxManagerMailRepository.initialize(); 66 mailboxManagerMailRepository.setMailboxManagerProvider(TorqueMailboxManagerProviderSingleton 67 .getTorqueMailboxManagerProviderInstance()); 68 mailRepository = mailboxManagerMailRepository; 69 70 } 71 72 protected void destroyRepository() throws IOException , MessagingException { 73 } 74 75 protected void assertNativeMessageCountEquals(int count) { 76 assertEquals(count, getNativeMessageCount()); 77 } 78 79 80 protected void assertNativeMessagesEqual(Collection expectedMessages) 81 throws IOException , MessagingException { 82 Collection existing = getNativeMessages(); 83 Set existingSet = new HashSet (); 84 for (Iterator iter = existing.iterator(); iter.hasNext();) { 85 MimeMessage mm = (MimeMessage ) iter.next(); 86 existingSet.add(new Integer (messageHashSum(mm))); 87 } 88 Set expectedSet = new HashSet (); 89 for (Iterator iter = expectedMessages.iterator(); iter.hasNext();) { 90 MimeMessage mm = (MimeMessage ) iter.next(); 91 expectedSet.add(new Integer (messageHashSum(mm))); 92 } 93 assertEquals(expectedSet.size(), existingSet.size()); 94 assertTrue(expectedSet.equals(existingSet)); 95 96 } 97 98 protected int getNativeMessageCount() { 99 try { 100 return getShadowMailbox().getMessageCount(); 101 } catch (MailboxManagerException e) { 102 throw new RuntimeException (e); 103 } 104 } 105 106 public void testLock() throws MessagingException { 107 super.testLock(); 108 } 109 110 111 protected Collection getNativeMessages() { 112 final MessageResult[] mr; 113 try { 114 mr = getShadowMailbox().getMessages(GeneralMessageSetImpl.all(), 115 MessageResult.MIME_MESSAGE); 116 117 } catch (MailboxManagerException e) { 118 throw new RuntimeException (e); 119 } 120 Collection existing = new ArrayList (); 121 for (int i = 0; i < mr.length; i++) { 122 existing.add(mr[i].getMimeMessage()); 123 } 124 return existing; 125 } 126 127 protected void nativeStoreMessage(MimeMessage mm) { 128 try { 129 getShadowMailbox().appendMessage(mm, new Date (), 130 MessageResult.NOTHING); 131 } catch (MailboxManagerException e) { 132 throw new RuntimeException (e); 133 } 134 135 } 136 137 protected GeneralMailboxSession getShadowMailbox() { 138 if (shadowMailbox == null) { 139 try { 140 MailboxManager mailboxManager= TorqueMailboxManagerProviderSingleton 141 .getTorqueMailboxManagerProviderInstance() 142 .getMailboxManagerInstance(new MockUser()); 143 if (!mailboxManager.existsMailbox(TUSER_INBOX)) { 144 mailboxManager.createMailbox(TUSER_INBOX); 145 } 146 shadowMailbox = mailboxManager.getGeneralMailboxSession(TUSER_INBOX); 147 } catch (Exception e) { 148 throw new RuntimeException (e); 149 } 150 } 151 152 return shadowMailbox; 153 } 154 155 } 156 | Popular Tags |