KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > redundant > AbstractMailRepositoryTestCase


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 package org.apache.james.mailboxmanager.redundant;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Random JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.mail.Message JavaDoc;
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.Session JavaDoc;
33 import javax.mail.internet.InternetAddress JavaDoc;
34 import javax.mail.internet.MimeMessage JavaDoc;
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 JavaDoc random;
50
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53         destroyRepository();
54         configureRepository();
55         assertNotNull(mailRepository);
56     }
57
58     protected void tearDown() throws Exception JavaDoc {
59         super.tearDown();
60         destroyRepository();
61         mailRepository = null;
62     }
63
64     protected abstract void configureRepository() throws Exception JavaDoc;
65
66     protected abstract void destroyRepository() throws IOException JavaDoc, MessagingException JavaDoc;
67
68     public static Mail generateMail() throws MessagingException JavaDoc {
69         Mail m = new MailImpl();
70         MimeMessage JavaDoc mm = new MimeMessage JavaDoc((Session JavaDoc) null);
71         int r = getRandom().nextInt() % 100000;
72         int r2 = getRandom().nextInt() % 100000;
73         mm.setSubject("good news" + r);
74         mm.setFrom(new InternetAddress JavaDoc("user" + r + "@localhost"));
75         mm.setRecipients(Message.RecipientType.TO,
76                 new InternetAddress JavaDoc[] { new InternetAddress JavaDoc("user" + r2
77                         + "@localhost") });
78         String JavaDoc 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 JavaDoc key = "james-test-" + System.currentTimeMillis() + "-"
84                 + getRandom().nextLong();
85         m.setName(key);
86         return m;
87     }
88
89     protected static boolean contentEquals(MimeMessage JavaDoc m1, MimeMessage JavaDoc m2)
90             throws IOException JavaDoc, MessagingException JavaDoc {
91         ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
92         m1.writeTo(new CRLFOutputStream(baos1));
93         ByteArrayOutputStream JavaDoc baos2 = new ByteArrayOutputStream JavaDoc();
94         m2.writeTo(new CRLFOutputStream(baos2));
95         return Arrays.equals(baos1.toByteArray(), baos2.toByteArray());
96     }
97
98     protected static int messageHashSum(MimeMessage JavaDoc mm)
99             throws IOException JavaDoc, MessagingException JavaDoc {
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 JavaDoc ma1, Collection JavaDoc ma2)
106             throws IOException JavaDoc, MessagingException JavaDoc {
107         if (ma1.size() != ma2.size())
108             return false;
109         Set JavaDoc s1 = new HashSet JavaDoc();
110         Set JavaDoc s2 = new HashSet JavaDoc();
111         for (Iterator JavaDoc it = ma1.iterator(); it.hasNext();) {
112             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) it.next();
113             s1.add(new Integer JavaDoc(messageHashSum(mm)));
114         }
115         for (Iterator JavaDoc it = ma2.iterator(); it.hasNext();) {
116             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) it.next();
117             s2.add(new Integer JavaDoc(messageHashSum(mm)));
118         }
119         return s1.equals(s2);
120     }
121
122     protected static synchronized Random JavaDoc getRandom() {
123         if (random == null) {
124             random = new Random JavaDoc();
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 JavaDoc added)
139             throws IOException JavaDoc, MessagingException JavaDoc {
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 JavaDoc getNativeMessages()
155             throws NativeMethodNotSupportetException {
156         throw new NativeMethodNotSupportetException(
157                 "AbstractMailRepositoryTestCase.getNativeMessages() not supported");
158     }
159
160     protected void nativeStoreMessage(MimeMessage JavaDoc mm)
161             throws NativeMethodNotSupportetException {
162         throw new NativeMethodNotSupportetException(
163                 "AbstractMailRepositoryTestCase.nativeStoreMessage() not supported");
164     }
165
166     class NativeMethodNotSupportetException extends Exception JavaDoc {
167
168         public NativeMethodNotSupportetException(String JavaDoc string) {
169             super(string);
170         }
171
172         private static final long serialVersionUID = 1477298541686913960L;
173
174     }
175
176 }
177
Popular Tags