1 /************************************************************************
2 * Copyright (c) 2006 The Apache Software Foundation. *
3 * All rights reserved. *
4 * ------------------------------------------------------------------- *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you *
6 * may not use this file except in compliance with the License. You *
7 * may obtain a copy of the License at: *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14 * implied. See the License for the specific language governing *
15 * permissions and limitations under the License. *
16 ***********************************************************************/
17
18 package org.apache.james.transport.matchers;
19
20 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
21 import org.apache.james.test.mock.mailet.MockMail;
22 import org.apache.james.test.mock.mailet.MockMailContext;
23 import org.apache.james.test.mock.mailet.MockMatcherConfig;
24
25 import org.apache.mailet.MailAddress;
26 import org.apache.mailet.Matcher;
27
28 import javax.mail.MessagingException;
29 import javax.mail.internet.InternetAddress;
30 import javax.mail.internet.MimeMessage;
31 import javax.mail.internet.ParseException;
32 import javax.mail.internet.MimeMessage.RecipientType;
33
34 import java.io.Serializable;
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 HasMailAttributeWithValueRegexTest extends TestCase {
42
43 private MimeMessage mockedMimeMessage;
44
45 private MockMail mockedMail;
46
47 private Matcher matcher;
48
49 private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit";
50
51 private final String MAIL_ATTRIBUTE_VALUE = "true";
52
53 private String mailAttributeName = "org.apache.james";
54
55 private String mailAttributeValue = "false";
56
57 private String regex = ".*";
58
59 public HasMailAttributeWithValueRegexTest(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 setRegex(String regex) {
73 this.regex = regex;
74 }
75
76 private void setupMockedMimeMessage() throws MessagingException {
77 String sender = "test@james.apache.org";
78 String rcpt = "test2@james.apache.org";
79
80 mockedMimeMessage = new MockMimeMessage();
81 mockedMimeMessage.setFrom(new InternetAddress(sender));
82 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
83 mockedMimeMessage.setSubject("testmail");
84 mockedMimeMessage.setText("testtext");
85 mockedMimeMessage.saveChanges();
86
87 }
88
89 private void setupMockedMail(MimeMessage m) throws ParseException {
90 mockedMail = new MockMail();
91 mockedMail.setMessage(m);
92 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
93 new MailAddress("test@james.apache.org"),
94 new MailAddress("test2@james.apache.org") }));
95 mockedMail.setAttribute(mailAttributeName,
96 (Serializable) mailAttributeValue);
97
98 }
99
100 private void setupMatcher() throws MessagingException {
101 setupMockedMimeMessage();
102 matcher = new HasMailAttributeWithValueRegex();
103 MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute="
104 + MAIL_ATTRIBUTE_NAME + ", " + regex, new MockMailContext());
105 matcher.init(mci);
106 }
107
108
109 public void testAttributeIsMatched() throws MessagingException {
110 setMailAttributeName(MAIL_ATTRIBUTE_NAME);
111 setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
112 setRegex(".*");
113
114 setupMockedMimeMessage();
115 setupMockedMail(mockedMimeMessage);
116 setupMatcher();
117
118 Collection matchedRecipients = matcher.match(mockedMail);
119
120 assertNotNull(matchedRecipients);
121 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
122 .size());
123 }
124
125
126 public void testHeaderIsNotMatched() throws MessagingException {
127 setRegex("//d");
128 setupMockedMimeMessage();
129 setupMockedMail(mockedMimeMessage);
130 setupMatcher();
131
132 Collection matchedRecipients = matcher.match(mockedMail);
133
134 assertNull(matchedRecipients);
135 }
136
137
138 public void testHeaderIsNotMatchedCauseValue() throws MessagingException {
139
140 String invalidRegex = "(!(";
141 String regexException = null;
142 String exception = "Malformed pattern: " + invalidRegex;
143
144 setRegex(invalidRegex);
145 setupMockedMimeMessage();
146 setupMockedMail(mockedMimeMessage);
147
148 try {
149 setupMatcher();
150 } catch (MessagingException m) {
151 regexException = m.getMessage();
152 }
153
154 Collection matchedRecipients = matcher.match(mockedMail);
155
156 assertNull(matchedRecipients);
157 assertEquals(regexException, exception);
158
159 }
160 }