KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > util > MessageGenerator


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.imapserver.util;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Random JavaDoc;
26
27 import javax.mail.Message JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Session JavaDoc;
30 import javax.mail.internet.InternetAddress JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32
33 import com.sun.mail.util.CRLFOutputStream;
34
35 public class MessageGenerator
36 {
37     private static Random JavaDoc random;
38
39     protected static synchronized Random JavaDoc getRandom() {
40         if (random == null) {
41             random = new Random JavaDoc();
42         }
43         return random;
44
45     }
46     
47     public static int calculateSize(MimeMessage JavaDoc m) throws IOException JavaDoc, MessagingException JavaDoc {
48         ByteArrayOutputStream JavaDoc os =new ByteArrayOutputStream JavaDoc();
49         m.writeTo(os);
50         return os.size();
51     }
52
53     public static MimeMessage JavaDoc generateSimpleMessage() throws MessagingException JavaDoc {
54         
55         MimeMessage JavaDoc mm = new MimeMessage JavaDoc((Session JavaDoc) null);
56         int r = getRandom().nextInt() % 100000;
57         int r2 = getRandom().nextInt() % 100000;
58         mm.setSubject("good news" + r);
59         mm.setFrom(new InternetAddress JavaDoc("user" + r + "@localhost"));
60         mm.setSentDate(new Date JavaDoc());
61         mm.setRecipients(Message.RecipientType.TO,
62                 new InternetAddress JavaDoc[] { new InternetAddress JavaDoc("user" + r2
63                         + "@localhost") });
64         String JavaDoc text = "Hello User" + r2
65                 + "!\r\n\r\nhave a nice holiday.\r\n\r\ngreetings,\nUser" + r
66                 + "\r\n";
67         mm.setText(text);
68         return mm;
69     }
70     public static String JavaDoc messageContentToString(Message JavaDoc mm) throws IOException JavaDoc, MessagingException JavaDoc {
71         ByteArrayOutputStream JavaDoc os=new ByteArrayOutputStream JavaDoc();
72         mm.writeTo(new CRLFOutputStream(os));
73         return os.toString();
74     }
75
76     public static MimeMessage JavaDoc[] generateSimpleMessages(int c)
77             throws MessagingException JavaDoc {
78         MimeMessage JavaDoc[] msgs=new MimeMessage JavaDoc[c];
79         for (int i=0; i<c; i++) {
80             msgs[i]=generateSimpleMessage();
81         }
82         return msgs;
83     }
84     
85     public static MimeMessage JavaDoc generateMessage(int size) throws MessagingException JavaDoc {
86         MimeMessage JavaDoc mm = new MimeMessage JavaDoc((Session JavaDoc) null);
87         int r = getRandom().nextInt() % 100000;
88         int r2 = getRandom().nextInt() % 100000;
89         mm.setSubject("good news" + r);
90         mm.setFrom(new InternetAddress JavaDoc("user" + r + "@localhost"));
91         mm.setSentDate(new Date JavaDoc());
92         mm.setRecipients(Message.RecipientType.TO,
93                 new InternetAddress JavaDoc[] { new InternetAddress JavaDoc("user" + r2
94                         + "@localhost") });
95         char[] textChars=new char[size];
96         for (int i = 0; i < textChars.length; i++) {
97             if (i%80 == 0) {
98                 textChars[i]='\n';
99             } else {
100                 textChars[i]=(char)(65+getRandom().nextInt(26));
101             }
102         }
103         mm.setText(new String JavaDoc(textChars));
104         return mm;
105     }
106 }
107
Popular Tags