KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > AbstractMailTest


1 package info.magnolia.cms.mail;
2
3 import info.magnolia.cms.mail.handlers.SimpleMailHandler;
4
5 import java.io.File JavaDoc;
6 import java.util.regex.Matcher JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.subethamail.wiser.Wiser;
14
15
16 /**
17  * @author fgiust
18  * @version $Revision: 6341 $ ($Author: philipp $)
19  */

20 public abstract class AbstractMailTest extends TestCase {
21
22     /**
23      * Logger
24      */

25     protected Logger log = LoggerFactory.getLogger(getClass());
26
27     public final static String JavaDoc TEST_RECIPIENT = "niko@macnica.com";
28
29     public final static String JavaDoc TEST_SENDER = "niko@magnolia.com";
30
31     public static final String JavaDoc TEST_FILE_JPG = "magnolia.jpg";
32
33     public static final String JavaDoc TEST_FILE_PDF = "magnolia.pdf";
34
35     public static int SMTP_PORT = 25025;
36
37     protected MgnlMailFactory factory;
38
39     protected SimpleMailHandler handler;
40
41     protected Wiser wiser = new Wiser();
42
43     public File JavaDoc getResourceFile(String JavaDoc filename) {
44         return new File JavaDoc(getClass().getResource("/" + filename).getFile());
45     }
46
47     public String JavaDoc getResourcePath(String JavaDoc filename) {
48         return getResourceFile(filename).getAbsolutePath();
49     }
50
51     /**
52      * @see junit.framework.TestCase#setUp()
53      */

54     public void setUp() throws Exception JavaDoc {
55         super.setUp();
56
57         handler = new SimpleMailHandler();
58         factory = MgnlMailFactory.getInstance();
59         factory.initParam(MgnlMailFactory.SMTP_SERVER, "localhost");
60         factory.initParam(MgnlMailFactory.SMTP_PORT, Integer.toString(SMTP_PORT));
61
62         wiser.setPort(SMTP_PORT);
63         wiser.start();
64     }
65
66     /**
67      * @see junit.framework.TestCase#tearDown()
68      */

69     public void tearDown() throws Exception JavaDoc {
70         wiser.stop();
71         super.tearDown();
72     }
73
74     /**
75      * this test will fail when subject is not US-ASCII TODO: replace with mail parser or handle encoding and improve
76      * pattern
77      * @param message
78      * @param subject
79      * @return true is <code>message</code>'s subject equals <code>subject</code>
80      */

81     protected boolean hasMatchingSubject(String JavaDoc message, String JavaDoc subject) {
82         Pattern JavaDoc pattern = Pattern.compile("Subject: " + subject);
83         Matcher JavaDoc matcher = pattern.matcher(message);
84         return matcher.find();
85     }
86
87 }
88
Popular Tags