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.transport.matchers;
21
22 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
23 import org.apache.james.test.mock.mailet.MockMail;
24 import org.apache.james.test.mock.mailet.MockMailContext;
25 import org.apache.james.test.mock.mailet.MockMatcherConfig;
26
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.Matcher;
29
30 import javax.mail.MessagingException;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
33 import javax.mail.internet.MimeMessage.RecipientType;
34
35 import java.io.UnsupportedEncodingException;
36 import java.util.Arrays;
37 import java.util.Collection;
38
39 import junit.framework.TestCase;
40
41 public class IsSingleRecipientTest extends TestCase {
42
43 private MimeMessage mockedMimeMessage;
44
45 private MockMail mockedMail;
46
47 private Matcher matcher;
48
49 private MailAddress[] recipients;
50
51 public IsSingleRecipientTest(String arg0)
52 throws UnsupportedEncodingException {
53 super(arg0);
54 }
55
56 private void setRecipients(MailAddress[] recipients) {
57 this.recipients = recipients;
58 }
59
60 private void setupMockedMimeMessage() throws MessagingException {
61 String sender = "test@james.apache.org";
62 String rcpt = "test2@james.apache.org";
63
64 mockedMimeMessage = new MockMimeMessage();
65 mockedMimeMessage.setFrom(new InternetAddress(sender));
66 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
67 mockedMimeMessage.setSubject("testmail");
68 mockedMimeMessage.setText("testtext");
69 mockedMimeMessage.saveChanges();
70
71 }
72
73 private void setupMockedMail(MimeMessage m) {
74 mockedMail = new MockMail();
75 mockedMail.setMessage(m);
76 mockedMail.setRecipients(Arrays.asList(recipients));
77
78 }
79
80 private void setupMatcher() throws MessagingException {
81
82 matcher = new IsSingleRecipient();
83 MockMatcherConfig mci = new MockMatcherConfig("IsSingleRecipient",
84 new MockMailContext());
85 matcher.init(mci);
86 }
87
88
89 public void testHostIsMatchedAllRecipients() throws MessagingException {
90 setRecipients(new MailAddress[] { new MailAddress(
91 "test@james.apache.org") });
92
93 setupMockedMimeMessage();
94 setupMockedMail(mockedMimeMessage);
95 setupMatcher();
96
97 Collection matchedRecipients = matcher.match(mockedMail);
98
99 assertNotNull(matchedRecipients);
100 }
101
102
103 public void testHostIsMatchedOneRecipient() throws MessagingException {
104 setRecipients(new MailAddress[] {
105 new MailAddress("test@james2.apache.org"),
106 new MailAddress("test2@james.apache.org") });
107
108 setupMockedMimeMessage();
109 setupMockedMail(mockedMimeMessage);
110 setupMatcher();
111
112 Collection matchedRecipients = matcher.match(mockedMail);
113
114 assertNull(matchedRecipients);
115 }
116
117 }