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 HasMailAttributeTest 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 public HasMailAttributeTest(String arg0)
58 throws UnsupportedEncodingException {
59 super(arg0);
60 }
61
62 private void setMailAttributeName(String mailAttributeName) {
63 this.mailAttributeName = mailAttributeName;
64 }
65
66 private void setMailAttributeValue(String mailAttributeValue) {
67 this.mailAttributeValue = mailAttributeValue;
68 }
69
70 private void setupMockedMimeMessage() throws MessagingException {
71 String sender = "test@james.apache.org";
72 String rcpt = "test2@james.apache.org";
73
74 mockedMimeMessage = new MockMimeMessage();
75 mockedMimeMessage.setFrom(new InternetAddress(sender));
76 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
77 mockedMimeMessage.setSubject("testmail");
78 mockedMimeMessage.setText("testtext");
79 mockedMimeMessage.saveChanges();
80
81 }
82
83 private void setupMockedMail(MimeMessage m) throws ParseException {
84 mockedMail = new MockMail();
85 mockedMail.setMessage(m);
86 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
87 new MailAddress("test@james.apache.org"),
88 new MailAddress("test2@james.apache.org") }));
89 mockedMail.setAttribute(mailAttributeName,
90 (Serializable) mailAttributeValue);
91
92 }
93
94 private void setupMatcher() throws MessagingException {
95 setupMockedMimeMessage();
96 matcher = new HasMailAttribute();
97 MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute="
98 + MAIL_ATTRIBUTE_NAME, new MockMailContext());
99 matcher.init(mci);
100 }
101
102
103 public void testAttributeIsMatched() throws MessagingException {
104 setMailAttributeName(MAIL_ATTRIBUTE_NAME);
105 setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
106
107 setupMockedMimeMessage();
108 setupMockedMail(mockedMimeMessage);
109 setupMatcher();
110
111 Collection matchedRecipients = matcher.match(mockedMail);
112
113 assertNotNull(matchedRecipients);
114 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
115 .size());
116 }
117
118
119 public void testAttributeIsNotMatched() throws MessagingException {
120 setupMockedMimeMessage();
121 setupMockedMail(mockedMimeMessage);
122 setupMatcher();
123
124 Collection matchedRecipients = matcher.match(mockedMail);
125
126 assertNull(matchedRecipients);
127 }
128 }