1 19 20 package org.apache.james.mailboxmanager; 21 22 import org.apache.commons.lang.builder.HashCodeBuilder; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Random ; 31 import java.util.Set ; 32 33 import javax.mail.Message ; 34 import javax.mail.MessagingException ; 35 import javax.mail.Session ; 36 import javax.mail.internet.InternetAddress ; 37 import javax.mail.internet.MimeMessage ; 38 39 import com.sun.mail.util.CRLFOutputStream; 40 41 public class TestUtil { 42 43 private static Random random; 44 45 46 public static boolean contentEquals(MimeMessage m1, MimeMessage m2, boolean verbose) 47 throws IOException , MessagingException { 48 ByteArrayOutputStream baos1 = new ByteArrayOutputStream (); 49 m1.writeTo(new CRLFOutputStream(baos1)); 50 ByteArrayOutputStream baos2 = new ByteArrayOutputStream (); 51 m2.writeTo(new CRLFOutputStream(baos2)); 52 if (verbose) { 53 byte[] b1=baos1.toByteArray(); 54 byte[] b2=baos2.toByteArray(); 55 int size=b1.length; 56 if (b2.length<b1.length) size=b2.length; 57 for (int i=0; i< size; i++) { 58 if (b1[i]!=b2[i]) { 59 System.out.println("I: "+i+" B1: "+b1[i]+" B2 "+b2[i]); 60 System.out.println("B1:"+new String (b1,0,i+1)+"\u00C2\u00B0"); 61 System.out.println("B2:"+new String (b2,0,i+1)+"\u00C2\u00B0"); 62 break; 63 } 64 } 65 66 } 67 68 return Arrays.equals(baos1.toByteArray(), baos2.toByteArray()); 69 } 70 71 public static byte[] messageToByteArray(MimeMessage mm) 72 throws IOException , MessagingException { 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 74 mm.writeTo(new CRLFOutputStream(baos)); 75 return baos.toByteArray(); 76 } 77 78 public static boolean messageSetsEqual(Collection ma1, Collection ma2) 79 throws IOException , MessagingException { 80 if (ma1.size() != ma2.size()) 81 return false; 82 Set s1 = new HashSet (); 83 Set s2 = new HashSet (); 84 for (Iterator it = ma1.iterator(); it.hasNext();) { 85 MimeMessage mm = (MimeMessage ) it.next(); 86 HashCodeBuilder builder = new HashCodeBuilder(); 87 builder.append(messageToByteArray(mm)); 88 s1.add(new Integer (builder.toHashCode())); 89 } 90 for (Iterator it = ma2.iterator(); it.hasNext();) { 91 MimeMessage mm = (MimeMessage ) it.next(); 92 HashCodeBuilder builder = new HashCodeBuilder(); 93 builder.append(messageToByteArray(mm)); 94 s2.add(new Integer (builder.toHashCode())); 95 } 96 return s1.equals(s2); 97 } 98 99 100 public static MimeMessage createMessage() throws MessagingException { 101 MimeMessage mm = new MimeMessage ((Session ) null); 102 int r = getRandom().nextInt() % 100000; 103 int r2 = getRandom().nextInt() % 100000; 104 mm.setSubject("good news" + r); 105 mm.setFrom(new InternetAddress ("user" + r + "@localhost")); 106 mm.setRecipients(Message.RecipientType.TO, 107 new InternetAddress [] { new InternetAddress ("user" + r2 108 + "@localhost") }); 109 String text = "Hello User" + r2 110 + "!\n\nhave a nice holiday.\r\n\r\ngreetings,\nUser" + r 111 + "\n"; 112 mm.setText(text); 113 mm.saveChanges(); 114 return mm; 115 } 116 117 public static synchronized Random getRandom() { 118 if (random == null) { 119 random = new Random (); 120 } 121 return random; 122 123 } 124 125 126 } 127 | Popular Tags |