KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 import javax.mail.MessagingException JavaDoc;
27
28 import org.apache.mailet.MailAddress;
29 import org.apache.mailet.Matcher;
30
31 public class SenderIsRegexTest extends AbstractSenderIsTest {
32
33     private final String JavaDoc SENDER_NAME = "test@james.apache.org";
34
35     private String JavaDoc regex = ".*";
36
37     public SenderIsRegexTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
38         super(arg0);
39     }
40
41     private void setRegex(String JavaDoc regex) {
42         this.regex = regex;
43     }
44
45     // test if matched
46
public void testSenderIsRegexMatchedAllRecipients()
47             throws MessagingException JavaDoc {
48         setSender(new MailAddress(SENDER_NAME));
49         setRegex(".*@.*");
50         setupMockedMail();
51         setupMatcher();
52
53         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
54
55         assertNotNull(matchedRecipients);
56         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
57                 .size());
58     }
59
60     // test if not matched
61
public void testSenderIsRegexNotMatchedAllRecipients()
62             throws MessagingException JavaDoc {
63         setSender(new MailAddress("t@james.apache.org"));
64         setRegex("^\\.");
65
66         setupMockedMail();
67         setupMatcher();
68
69         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
70
71         assertNull(matchedRecipients);
72     }
73
74     // test if throw exception if no pattern is used
75
public void testThrowExceptionWithoutPattern() throws MessagingException JavaDoc {
76         boolean exceptionCatched = false;
77         setSender(new MailAddress("t@james.apache.org"));
78         setRegex("");
79
80         setupMockedMail();
81
82         try {
83             setupMatcher();
84         } catch (MessagingException JavaDoc m) {
85             exceptionCatched = true;
86         }
87         assertTrue(exceptionCatched);
88     }
89
90     // test if throw exception if invalid pattern is used
91
public void testThrowExceptionWithInvalidPattern()
92             throws MessagingException JavaDoc {
93         boolean exceptionCatched = false;
94         setSender(new MailAddress("t@james.apache.org"));
95         setRegex("(.");
96
97         setupMockedMail();
98
99         try {
100             setupMatcher();
101         } catch (MessagingException JavaDoc m) {
102             exceptionCatched = true;
103         }
104         assertTrue(exceptionCatched);
105     }
106
107     protected Matcher createMatcher() {
108         return new SenderIsRegex();
109     }
110
111     protected String JavaDoc getConfigOption() {
112         return "SenderIsRegex=";
113     }
114
115     protected String JavaDoc getConfigValue() {
116         return regex;
117     }
118 }
119
Popular Tags