1 19 package org.apache.james.mailboxmanager.redundant; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Random ; 28 import java.util.Set ; 29 30 import javax.mail.Message ; 31 import javax.mail.MessagingException ; 32 import javax.mail.Session ; 33 import javax.mail.internet.InternetAddress ; 34 import javax.mail.internet.MimeMessage ; 35 36 import junit.framework.TestCase; 37 38 import org.apache.james.core.MailImpl; 39 import org.apache.james.mailrepository.javamail.HashJavamailStoreMailRepository; 40 import org.apache.james.services.MailRepository; 41 import org.apache.mailet.Mail; 42 43 import com.sun.mail.util.CRLFOutputStream; 44 45 public abstract class AbstractMailRepositoryTestCase extends TestCase { 46 47 protected MailRepository mailRepository; 48 49 private static Random random; 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 destroyRepository(); 54 configureRepository(); 55 assertNotNull(mailRepository); 56 } 57 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 destroyRepository(); 61 mailRepository = null; 62 } 63 64 protected abstract void configureRepository() throws Exception ; 65 66 protected abstract void destroyRepository() throws IOException , MessagingException ; 67 68 public static Mail generateMail() throws MessagingException { 69 Mail m = new MailImpl(); 70 MimeMessage mm = new MimeMessage ((Session ) null); 71 int r = getRandom().nextInt() % 100000; 72 int r2 = getRandom().nextInt() % 100000; 73 mm.setSubject("good news" + r); 74 mm.setFrom(new InternetAddress ("user" + r + "@localhost")); 75 mm.setRecipients(Message.RecipientType.TO, 76 new InternetAddress [] { new InternetAddress ("user" + r2 77 + "@localhost") }); 78 String text = "Hello User" + r2 79 + "!\n\nhave a nice holiday.\r\n\r\ngreetings,\nUser" + r 80 + "\n"; 81 mm.setText(text); 82 m.setMessage(mm); 83 String key = "james-test-" + System.currentTimeMillis() + "-" 84 + getRandom().nextLong(); 85 m.setName(key); 86 return m; 87 } 88 89 protected static boolean contentEquals(MimeMessage m1, MimeMessage m2) 90 throws IOException , MessagingException { 91 ByteArrayOutputStream baos1 = new ByteArrayOutputStream (); 92 m1.writeTo(new CRLFOutputStream(baos1)); 93 ByteArrayOutputStream baos2 = new ByteArrayOutputStream (); 94 m2.writeTo(new CRLFOutputStream(baos2)); 95 return Arrays.equals(baos1.toByteArray(), baos2.toByteArray()); 96 } 97 98 protected static int messageHashSum(MimeMessage mm) 99 throws IOException , MessagingException { 100 HashJavamailStoreMailRepository.HasherOutputStream hos=new HashJavamailStoreMailRepository.HasherOutputStream(); 101 mm.writeTo(new CRLFOutputStream(hos)); 102 return hos.getHash(); 103 } 104 105 protected static boolean messageSetsEqual(Collection ma1, Collection ma2) 106 throws IOException , MessagingException { 107 if (ma1.size() != ma2.size()) 108 return false; 109 Set s1 = new HashSet (); 110 Set s2 = new HashSet (); 111 for (Iterator it = ma1.iterator(); it.hasNext();) { 112 MimeMessage mm = (MimeMessage ) it.next(); 113 s1.add(new Integer (messageHashSum(mm))); 114 } 115 for (Iterator it = ma2.iterator(); it.hasNext();) { 116 MimeMessage mm = (MimeMessage ) it.next(); 117 s2.add(new Integer (messageHashSum(mm))); 118 } 119 return s1.equals(s2); 120 } 121 122 protected static synchronized Random getRandom() { 123 if (random == null) { 124 random = new Random (); 125 } 126 return random; 127 128 } 129 130 protected void assertNativeMessageCountEquals(int count) { 131 try { 132 assertEquals("message count differs", count, 133 getNativeMessageCount()); 134 } catch (NativeMethodNotSupportetException e) { 135 } 136 } 137 138 protected void assertNativeMessagesEqual(Collection added) 139 throws IOException , MessagingException { 140 try { 141 boolean equ = messageSetsEqual(getNativeMessages(), added); 142 assertTrue("messages differ", equ); 143 } catch (NativeMethodNotSupportetException e) { 144 } 145 146 } 147 148 protected int getNativeMessageCount() 149 throws NativeMethodNotSupportetException { 150 throw new NativeMethodNotSupportetException( 151 "AbstractMailRepositoryTestCase.getNativeMessageCount() not supported"); 152 } 153 154 protected Collection getNativeMessages() 155 throws NativeMethodNotSupportetException { 156 throw new NativeMethodNotSupportetException( 157 "AbstractMailRepositoryTestCase.getNativeMessages() not supported"); 158 } 159 160 protected void nativeStoreMessage(MimeMessage mm) 161 throws NativeMethodNotSupportetException { 162 throw new NativeMethodNotSupportetException( 163 "AbstractMailRepositoryTestCase.nativeStoreMessage() not supported"); 164 } 165 166 class NativeMethodNotSupportetException extends Exception { 167 168 public NativeMethodNotSupportetException(String string) { 169 super(string); 170 } 171 172 private static final long serialVersionUID = 1477298541686913960L; 173 174 } 175 176 } 177 | Popular Tags |