OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / test / com / ozacc / mail / mailet / MailetRunnerTest.java
1 package com.ozacc.mail.mailet;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 import javax.mail.internet.InternetAddress;\r
7 \r
8 import org.apache.log4j.BasicConfigurator;\r
9 \r
10 import junit.framework.TestCase;\r
11 \r
12 import com.ozacc.mail.fetch.ReceivedMail;\r
13 import com.ozacc.mail.mock.MockFetchMailPro;\r
14 \r
15 /**\r
16  * MailetRunnerのテストケース。\r
17  * \r
18  * @author Tomohiro Otsuka\r
19  * @version $Id: MailetRunnerTest.java,v 1.1.2.1 2005/02/06 13:53:54 otsuka Exp $\r
20  */\r
21 public class MailetRunnerTest extends TestCase {\r
22 \r
23         private MailetRunner mailetRunner;\r
24 \r
25         /*\r
26          * @see TestCase#setUp()\r
27          */\r
28         protected void setUp() throws Exception {\r
29                 super.setUp();\r
30                 BasicConfigurator.configure();\r
31 \r
32                 MockFetchMailPro fetchMailPro = new MockFetchMailPro();\r
33                 fetchMailPro.setupGetMails(getSampleMails());\r
34 \r
35                 Mailet mailet = getSubjectOutputMailet();\r
36                 Matcher matcher = getExampleComAddressMatcher();\r
37                 List matcherList = new ArrayList();\r
38                 matcherList.add(matcher);\r
39                 MailetWrapper mw = new MailetWrapper(mailet, matcherList);\r
40                 List mwList = new ArrayList();\r
41                 mwList.add(mw);\r
42                 mwList.add(mw);\r
43 \r
44                 mailetRunner = new MailetRunner();\r
45                 mailetRunner.setFetchMailPro(fetchMailPro);\r
46                 mailetRunner.setMailetWrapperList(mwList);\r
47         }\r
48 \r
49         /**\r
50          * @return \r
51          */\r
52         private ReceivedMail[] getSampleMails() {\r
53                 List list = new ArrayList();\r
54                 ReceivedMail m1 = new ReceivedMail();\r
55                 m1.setSubject("m1 - 出力されます。");\r
56                 m1.addTo("to@example.com");\r
57                 list.add(m1);\r
58                 ReceivedMail m2 = new ReceivedMail();\r
59                 m2.setSubject("m2 - 出力されません。");\r
60                 m2.addTo("to@example.net");\r
61                 list.add(m2);\r
62                 ReceivedMail m3 = new ReceivedMail();\r
63                 m3.setSubject("m3 - 出力されます。");\r
64                 m3.addTo("to@example.net");\r
65                 m3.addTo("to@example.com");\r
66                 list.add(m3);\r
67                 return (ReceivedMail[])list.toArray(new ReceivedMail[list.size()]);\r
68         }\r
69 \r
70         /**\r
71          * @return \r
72          */\r
73         private Matcher getExampleComAddressMatcher() {\r
74                 Matcher m = new Matcher() {\r
75 \r
76                         public boolean match(ReceivedMail mail) {\r
77                                 InternetAddress[] toAddresses = mail.getTo();\r
78                                 for (int i = 0; i < toAddresses.length; i++) {\r
79                                         InternetAddress address = toAddresses[i];\r
80                                         if (address.getAddress().equalsIgnoreCase("to@example.com")) {\r
81                                                 return true;\r
82                                         }\r
83                                 }\r
84                                 return false;\r
85                         }\r
86                 };\r
87                 return m;\r
88         }\r
89 \r
90         /**\r
91          * @return \r
92          */\r
93         private Mailet getSubjectOutputMailet() {\r
94                 return new Mailet() {\r
95 \r
96                         public void service(ReceivedMail mail) {\r
97                                 System.out.println(mail.getSubject());\r
98                         }\r
99                 };\r
100         }\r
101 \r
102         /*\r
103          * @see TestCase#tearDown()\r
104          */\r
105         protected void tearDown() throws Exception {\r
106                 super.tearDown();\r
107                 BasicConfigurator.resetConfiguration();\r
108         }\r
109 \r
110         public void testRun() {\r
111                 mailetRunner.run();\r
112         }\r
113 \r
114 }