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 HasMailAttributeWithValueRegexTest 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 private String regex = ".*";
60
61 public HasMailAttributeWithValueRegexTest(String arg0)
62 throws UnsupportedEncodingException {
63 super(arg0);
64 }
65
66 private void setMailAttributeName(String mailAttributeName) {
67 this.mailAttributeName = mailAttributeName;
68 }
69
70 private void setMailAttributeValue(String mailAttributeValue) {
71 this.mailAttributeValue = mailAttributeValue;
72 }
73
74 private void setRegex(String regex) {
75 this.regex = regex;
76 }
77
78 private void setupMockedMimeMessage() throws MessagingException {
79 String sender = "test@james.apache.org";
80 String rcpt = "test2@james.apache.org";
81
82 mockedMimeMessage = new MockMimeMessage();
83 mockedMimeMessage.setFrom(new InternetAddress(sender));
84 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
85 mockedMimeMessage.setSubject("testmail");
86 mockedMimeMessage.setText("testtext");
87 mockedMimeMessage.saveChanges();
88
89 }
90
91 private void setupMockedMail(MimeMessage m) throws ParseException {
92 mockedMail = new MockMail();
93 mockedMail.setMessage(m);
94 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
95 new MailAddress("test@james.apache.org"),
96 new MailAddress("test2@james.apache.org") }));
97 mockedMail.setAttribute(mailAttributeName,
98 (Serializable) mailAttributeValue);
99
100 }
101
102 private void setupMatcher() throws MessagingException {
103 setupMockedMimeMessage();
104 matcher = new HasMailAttributeWithValueRegex();
105 MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute="
106 + MAIL_ATTRIBUTE_NAME + ", " + regex, new MockMailContext());
107 matcher.init(mci);
108 }
109
110
111 public void testAttributeIsMatched() throws MessagingException {
112 setMailAttributeName(MAIL_ATTRIBUTE_NAME);
113 setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
114 setRegex(".*");
115
116 setupMockedMimeMessage();
117 setupMockedMail(mockedMimeMessage);
118 setupMatcher();
119
120 Collection matchedRecipients = matcher.match(mockedMail);
121
122 assertNotNull(matchedRecipients);
123 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
124 .size());
125 }
126
127
128 public void testHeaderIsNotMatched() throws MessagingException {
129 setRegex("//d");
130 setupMockedMimeMessage();
131 setupMockedMail(mockedMimeMessage);
132 setupMatcher();
133
134 Collection matchedRecipients = matcher.match(mockedMail);
135
136 assertNull(matchedRecipients);
137 }
138
139
140 public void testHeaderIsNotMatchedCauseValue() throws MessagingException {
141
142 String invalidRegex = "(!(";
143 String regexException = null;
144 String exception = "Malformed pattern: " + invalidRegex;
145
146 setRegex(invalidRegex);
147 setupMockedMimeMessage();
148 setupMockedMail(mockedMimeMessage);
149
150 try {
151 setupMatcher();
152 } catch (MessagingException m) {
153 regexException = m.getMessage();
154 }
155
156 Collection matchedRecipients = matcher.match(mockedMail);
157
158 assertNull(matchedRecipients);
159 assertEquals(regexException, exception);
160
161 }
162 }