KickJava   Java API By Example, From Geeks To Geeks.

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


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 RecipientIsRegexTest extends AbstractRecipientIsTest {
32
33     private String JavaDoc regex = ".*";
34
35     public RecipientIsRegexTest(String JavaDoc arg0)
36             throws UnsupportedEncodingException JavaDoc {
37         super(arg0);
38     }
39
40     private void setRegex(String JavaDoc regex) {
41         this.regex = regex;
42     }
43
44     // test if the recipients get returned as matched
45
public void testRegexIsMatchedAllRecipients() throws MessagingException JavaDoc {
46         setRecipients(new MailAddress[] { new MailAddress(
47                 "test@james.apache.org") });
48         setRegex(".*@.*");
49
50         setupAll();
51
52         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
53
54         assertNotNull(matchedRecipients);
55         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
56                 .size());
57     }
58
59     // test if one recipients get returned as matched
60
public void testRegexIsMatchedOneRecipient() throws MessagingException JavaDoc {
61         setRecipients(new MailAddress[] {
62                 new MailAddress("test@james.apache.org"),
63                 new MailAddress("test2@james.apache.org") });
64         setRegex("^test@.*");
65
66         setupAll();
67
68         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
69
70         assertNotNull(matchedRecipients);
71         assertEquals(matchedRecipients.size(), 1);
72     }
73
74     // test if no recipient get returned cause it not match
75
public void testRegexIsNotMatch() throws MessagingException JavaDoc {
76         setRecipients(new MailAddress[] {
77                 new MailAddress("test@james2.apache.org"),
78                 new MailAddress("test2@james2.apache.org") });
79         setRegex(".*\\+");
80
81         setupAll();
82
83         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
84
85         assertEquals(matchedRecipients.size(), 0);
86     }
87
88     // test if an exception was thrown cause the regex was invalid
89
public void testRegexIsNotMatchedCauseError() throws MessagingException JavaDoc {
90         Collection JavaDoc matchedRecipients = null;
91         String JavaDoc invalidRegex = "(!(";
92         String JavaDoc regexException = null;
93         String JavaDoc exception = "Malformed pattern: " + invalidRegex;
94
95         setRecipients(new MailAddress[] {
96                 new MailAddress("test@james2.apache.org"),
97                 new MailAddress("test2@james2.apache.org") });
98         setRegex(invalidRegex);
99
100         try {
101             setupAll();
102             matchedRecipients = matcher.match(mockedMail);
103         } catch (MessagingException JavaDoc m) {
104             regexException = m.getMessage();
105         }
106
107         assertNull(matchedRecipients);
108         assertEquals(regexException, exception);
109
110     }
111
112     // test if an exception was thrown cause the regex was invalid
113
public void testThrowExceptionWithEmptyPattern() throws MessagingException JavaDoc {
114         boolean catchException = false;
115
116         setRecipients(new MailAddress[] {
117                 new MailAddress("test@james2.apache.org"),
118                 new MailAddress("test2@james2.apache.org") });
119         setRegex("");
120
121         try {
122             setupAll();
123         } catch (MessagingException JavaDoc m) {
124             catchException = true;
125         }
126         assertTrue(catchException);
127
128     }
129
130     protected String JavaDoc getRecipientName() {
131         return regex;
132     }
133
134     protected Matcher createMatcher() {
135         return new RecipientIsRegex();
136     }
137 }
138
Popular Tags