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.mailet.base.test;
21  
22  import org.apache.mailet.MailAddress;
23  
24  import javax.mail.MessagingException;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeMessage;
27  import javax.mail.internet.ParseException;
28  
29  import java.util.Arrays;
30  
31  /**
32   * some utilities for James unit testing
33   */
34  public class MailUtil {
35  
36      private static int m_counter = 0;
37  
38      public static String newId() {
39          m_counter++;
40          return "MockMailUtil-ID-" + m_counter;
41      }
42      
43      public static FakeMail createMockMail2Recipients(MimeMessage m) throws ParseException {
44          FakeMail mockedMail = new FakeMail();
45          mockedMail.setName(newId());
46          mockedMail.setMessage(m);
47          mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
48                  new MailAddress("test@james.apache.org"),
49                  new MailAddress("test2@james.apache.org") }));
50          return mockedMail;
51      }
52  
53      public static FakeMimeMessage createMimeMessage() throws MessagingException {
54          return createMimeMessage(null, null);
55      }
56      
57      public static FakeMimeMessage createMimeMessageWithSubject(String subject) throws MessagingException {
58          return createMimeMessage(null, null, subject, 0);
59      }
60      
61      public static FakeMimeMessage createMimeMessage(String subject, int number) throws MessagingException {
62          return createMimeMessage(null, null, subject, number);
63      }
64      
65      public static FakeMimeMessage createMimeMessage(String headerName, String headerValue) throws MessagingException {
66          return createMimeMessage(headerName, headerValue, "testmail", 0);
67      }
68      
69      public static FakeMimeMessage createMimeMessage(String headerName, String headerValue, String subject, int number) throws MessagingException {
70          String sender = "test@james.apache.org";
71          String rcpt = "test2@james.apache.org";
72  
73          FakeMimeMessage mockedMimeMessage = new FakeMimeMessage(number);
74          mockedMimeMessage.setFrom(new InternetAddress(sender));
75          mockedMimeMessage.setRecipients(MimeMessage.RecipientType.TO, rcpt);
76          if (headerName != null) mockedMimeMessage.setHeader(headerName, headerValue);
77          if (subject != null) mockedMimeMessage.setSubject(subject);
78          mockedMimeMessage.setText("testtext");
79          mockedMimeMessage.saveChanges();
80          return mockedMimeMessage;
81      }
82  
83  }