KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 public class HasHeaderTest extends TestCase {
36
37     private MimeMessage JavaDoc mockedMimeMessage;
38
39     private MockMail mockedMail;
40
41     private Matcher matcher;
42
43     private final String JavaDoc HEADER_NAME = "JUNIT";
44
45     private final String JavaDoc HEADER_VALUE = "test-value";
46
47     private String JavaDoc headerName = "defaultHeaderName";
48
49     private String JavaDoc headerValue = "defaultHeaderValue";
50
51     public HasHeaderTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
52         super(arg0);
53     }
54
55     private void setHeaderName(String JavaDoc headerName) {
56         this.headerName = headerName;
57     }
58
59     private void setHeaderValue(String JavaDoc headerValue) {
60         this.headerValue = headerValue;
61     }
62
63     private void setupMockedMimeMessage() throws MessagingException JavaDoc {
64         mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
65     }
66
67     private void setupMatcher() throws MessagingException JavaDoc {
68         setupMockedMimeMessage();
69         matcher = new HasHeader();
70         MockMatcherConfig mci = new MockMatcherConfig("HasHeader="
71                 + HEADER_NAME, new MockMailContext());
72         matcher.init(mci);
73     }
74
75     // test if the Header was matched
76
public void testHeaderIsMatched() throws MessagingException JavaDoc {
77         setHeaderName(HEADER_NAME);
78         setHeaderValue(HEADER_VALUE);
79
80         setupMockedMimeMessage();
81         mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
82         setupMatcher();
83
84         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
85
86         assertNotNull(matchedRecipients);
87         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
88                 .size());
89     }
90
91     // test if the Header was not matched
92
public void testHeaderIsNotMatched() throws MessagingException JavaDoc {
93         setupMockedMimeMessage();
94         mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
95         setupMatcher();
96
97         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
98
99         assertNull(matchedRecipients);
100     }
101 }
102
Popular Tags