KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > FetchedFromTest


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
21 package org.apache.james.transport.matchers;
22
23 import junit.framework.TestCase;
24 import org.apache.james.test.mock.mailet.MockMail;
25 import org.apache.james.test.mock.mailet.MockMailContext;
26 import org.apache.james.test.mock.mailet.MockMatcherConfig;
27 import org.apache.james.test.util.Util;
28 import org.apache.mailet.Matcher;
29
30 import javax.mail.MessagingException JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32 import javax.mail.internet.ParseException JavaDoc;
33 import java.io.UnsupportedEncodingException JavaDoc;
34 import java.util.Collection JavaDoc;
35
36 public class FetchedFromTest extends TestCase {
37
38     private MimeMessage JavaDoc mockedMimeMessage;
39
40     private MockMail mockedMail;
41
42     private Matcher matcher;
43
44     private final String JavaDoc HEADER_NAME = "X-fetched-from";
45
46     private final String JavaDoc HEADER_VALUE = "james-user";
47
48     private String JavaDoc headerName = "defaultHeaderName";
49
50     private String JavaDoc headerValue = "defaultHeaderValue";
51
52     public FetchedFromTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
53         super(arg0);
54     }
55
56     private void setHeaderName(String JavaDoc headerName) {
57         this.headerName = headerName;
58     }
59
60     private void setHeaderValue(String JavaDoc headerValue) {
61         this.headerValue = headerValue;
62     }
63
64     private void setupMockedMimeMessage() throws MessagingException JavaDoc {
65         mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
66     }
67
68     private void setupMockedMail(MimeMessage JavaDoc m) throws ParseException JavaDoc {
69         mockedMail = Util.createMockMail2Recipients(m);
70     }
71
72     private void setupMatcher() throws MessagingException JavaDoc {
73         setupMockedMimeMessage();
74         matcher = new FetchedFrom();
75         MockMatcherConfig mci = new MockMatcherConfig("FetchedFrom="
76                 + HEADER_VALUE, new MockMailContext());
77         matcher.init(mci);
78     }
79
80     // test if the Header was matched
81
public void testHeaderIsMatched() throws MessagingException JavaDoc {
82         setHeaderName(HEADER_NAME);
83         setHeaderValue(HEADER_VALUE);
84
85         setupMockedMimeMessage();
86         setupMockedMail(mockedMimeMessage);
87         setupMatcher();
88
89         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
90         assertNotNull(matchedRecipients);
91         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
92                 .size());
93     }
94
95     // test if the Header was not matched
96
public void testHeaderIsNotMatched() throws MessagingException JavaDoc {
97         setHeaderName(HEADER_NAME);
98         setHeaderValue(headerValue);
99
100         setupMockedMimeMessage();
101         setupMockedMail(mockedMimeMessage);
102         setupMatcher();
103
104         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
105         assertNull(matchedRecipients);
106     }
107
108     // test if the Header was removed after matched
109
public void testHeaderWasRemovedAfterMatched() throws MessagingException JavaDoc {
110         setHeaderName(HEADER_NAME);
111         setHeaderValue(HEADER_VALUE);
112
113         setupMockedMimeMessage();
114         setupMockedMail(mockedMimeMessage);
115         setupMatcher();
116
117         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
118         Collection JavaDoc matchedRecipients2 = matcher.match(mockedMail);
119
120         assertNotNull(matchedRecipients);
121         assertNull(matchedRecipients2);
122     }
123 }
124
Popular Tags