KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > TestUtil


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.mailboxmanager;
21
22 import org.apache.commons.lang.builder.HashCodeBuilder;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Random JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.mail.Message JavaDoc;
34 import javax.mail.MessagingException JavaDoc;
35 import javax.mail.Session JavaDoc;
36 import javax.mail.internet.InternetAddress JavaDoc;
37 import javax.mail.internet.MimeMessage JavaDoc;
38
39 import com.sun.mail.util.CRLFOutputStream;
40
41 public class TestUtil {
42     
43     private static Random JavaDoc random;
44     
45
46     public static boolean contentEquals(MimeMessage JavaDoc m1, MimeMessage JavaDoc m2, boolean verbose)
47             throws IOException JavaDoc, MessagingException JavaDoc {
48         ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
49         m1.writeTo(new CRLFOutputStream(baos1));
50         ByteArrayOutputStream JavaDoc baos2 = new ByteArrayOutputStream JavaDoc();
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 JavaDoc(b1,0,i+1)+"\u00C2\u00B0");
61                     System.out.println("B2:"+new String JavaDoc(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 JavaDoc mm)
72             throws IOException JavaDoc, MessagingException JavaDoc {
73         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74         mm.writeTo(new CRLFOutputStream(baos));
75         return baos.toByteArray();
76     }
77
78     public static boolean messageSetsEqual(Collection JavaDoc ma1, Collection JavaDoc ma2)
79             throws IOException JavaDoc, MessagingException JavaDoc {
80         if (ma1.size() != ma2.size())
81             return false;
82         Set JavaDoc s1 = new HashSet JavaDoc();
83         Set JavaDoc s2 = new HashSet JavaDoc();
84         for (Iterator JavaDoc it = ma1.iterator(); it.hasNext();) {
85             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) it.next();
86             HashCodeBuilder builder = new HashCodeBuilder();
87             builder.append(messageToByteArray(mm));
88             s1.add(new Integer JavaDoc(builder.toHashCode()));
89         }
90         for (Iterator JavaDoc it = ma2.iterator(); it.hasNext();) {
91             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) it.next();
92             HashCodeBuilder builder = new HashCodeBuilder();
93             builder.append(messageToByteArray(mm));
94             s2.add(new Integer JavaDoc(builder.toHashCode()));
95         }
96         return s1.equals(s2);
97     }
98     
99     
100     public static MimeMessage JavaDoc createMessage() throws MessagingException JavaDoc {
101         MimeMessage JavaDoc mm = new MimeMessage JavaDoc((Session JavaDoc) null);
102         int r = getRandom().nextInt() % 100000;
103         int r2 = getRandom().nextInt() % 100000;
104         mm.setSubject("good news" + r);
105         mm.setFrom(new InternetAddress JavaDoc("user" + r + "@localhost"));
106         mm.setRecipients(Message.RecipientType.TO,
107                 new InternetAddress JavaDoc[] { new InternetAddress JavaDoc("user" + r2
108                         + "@localhost") });
109         String JavaDoc 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 JavaDoc getRandom() {
118         if (random == null) {
119             random = new Random JavaDoc();
120         }
121         return random;
122
123     }
124
125
126 }
127
Popular Tags