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