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.ParseException;
34 import javax.mail.internet.MimeMessage.RecipientType;
35
36 import java.io.Serializable;
37 import java.io.UnsupportedEncodingException;
38 import java.util.Arrays;
39 import java.util.Collection;
40
41 import junit.framework.TestCase;
42
43 public class HasMailAttributeWithValueTest extends TestCase {
44
45 private MimeMessage mockedMimeMessage;
46
47 private MockMail mockedMail;
48
49 private Matcher matcher;
50
51 private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit";
52
53 private final String MAIL_ATTRIBUTE_VALUE = "true";
54
55 private String mailAttributeName = "org.apache.james";
56
57 private String mailAttributeValue = "false";
58
59 public HasMailAttributeWithValueTest(String arg0)
60 throws UnsupportedEncodingException {
61 super(arg0);
62 }
63
64 private void setMailAttributeName(String mailAttributeName) {
65 this.mailAttributeName = mailAttributeName;
66 }
67
68 private void setMailAttributeValue(String mailAttributeValue) {
69 this.mailAttributeValue = mailAttributeValue;
70 }
71
72 private void setupMockedMimeMessage() throws MessagingException {
73 String sender = "test@james.apache.org";
74 String rcpt = "test2@james.apache.org";
75
76 mockedMimeMessage = new MockMimeMessage();
77 mockedMimeMessage.setFrom(new InternetAddress(sender));
78 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
79 mockedMimeMessage.setSubject("testmail");
80 mockedMimeMessage.setText("testtext");
81 mockedMimeMessage.saveChanges();
82
83 }
84
85 private void setupMockedMail(MimeMessage m) throws ParseException {
86 mockedMail = new MockMail();
87 mockedMail.setMessage(m);
88 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
89 new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org") }));
90 mockedMail.setAttribute(mailAttributeName,
91 (Serializable) mailAttributeValue);
92
93 }
94
95 private void setupMatcher() throws MessagingException {
96 setupMockedMimeMessage();
97 matcher = new HasMailAttributeWithValue();
98 MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute="
99 + MAIL_ATTRIBUTE_NAME + ", " + MAIL_ATTRIBUTE_VALUE,
100 new MockMailContext());
101 matcher.init(mci);
102 }
103
104
105 public void testHeaderIsMatched() throws MessagingException {
106 setMailAttributeName(MAIL_ATTRIBUTE_NAME);
107 setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
108
109 setupMockedMimeMessage();
110 setupMockedMail(mockedMimeMessage);
111 setupMatcher();
112
113 Collection matchedRecipients = matcher.match(mockedMail);
114
115 assertNotNull(matchedRecipients);
116 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
117 .size());
118 }
119
120
121 public void testHeaderIsNotMatched() throws MessagingException {
122 setupMockedMimeMessage();
123 setupMockedMail(mockedMimeMessage);
124 setupMatcher();
125
126 Collection matchedRecipients = matcher.match(mockedMail);
127
128 assertNull(matchedRecipients);
129 }
130
131
132 public void testHeaderIsNotMatchedCauseValue() throws MessagingException {
133 setMailAttributeName(MAIL_ATTRIBUTE_NAME);
134 setupMockedMimeMessage();
135 setupMockedMail(mockedMimeMessage);
136 setupMatcher();
137
138 Collection matchedRecipients = matcher.match(mockedMail);
139
140 assertNull(matchedRecipients);
141 }
142 }