KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.mailet.GenericMatcher;
24
25 import javax.mail.MessagingException JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 public class HasMailAttributeWithValueRegexTest extends
29         AbstractHasMailAttributeTest {
30
31     private String JavaDoc regex = ".*";
32
33     public HasMailAttributeWithValueRegexTest() {
34         super();
35     }
36
37     private void setRegex(String JavaDoc regex) {
38         this.regex = regex;
39     }
40
41     protected String JavaDoc getHasMailAttribute() {
42         return MAIL_ATTRIBUTE_NAME + ", " + regex;
43     }
44
45     protected GenericMatcher createMatcher() {
46         return new HasMailAttributeWithValueRegex();
47     }
48
49     // test if the mail attribute was matched
50
public void testAttributeIsMatched() throws MessagingException JavaDoc {
51         init();
52         setRegex(".*");
53         setupAll();
54
55         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
56
57         assertNotNull(matchedRecipients);
58         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
59                 .size());
60     }
61
62     // test if the mail attribute was not matched
63
public void testHeaderIsNotMatched() throws MessagingException JavaDoc {
64         setRegex("\\d");
65         setupAll();
66
67         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
68
69         assertNull(matchedRecipients);
70     }
71
72     // test if an exception was thrown cause the regex was invalid
73
public void testHeaderIsNotMatchedCauseValue() throws MessagingException JavaDoc {
74
75         String JavaDoc invalidRegex = "(!(";
76         String JavaDoc regexException = null;
77         String JavaDoc exception = "Malformed pattern: " + invalidRegex;
78
79         setRegex(invalidRegex);
80         setupMockedMail(mockedMimeMessage);
81
82         try {
83             setupMatcher();
84         } catch (MessagingException JavaDoc m) {
85             regexException = m.getMessage();
86         }
87
88         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
89
90         assertNull(matchedRecipients);
91         assertEquals(regexException, exception);
92
93     }
94
95     protected String JavaDoc getConfigOption() {
96         return "HasMailAttributeWithValueRegex=";
97     }
98 }
99
Popular Tags