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;
24  import java.util.Collection;
25  
26  import javax.mail.MessagingException;
27  import javax.mail.internet.ParseException;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.mailet.base.test.MailUtil;
32  import org.apache.mailet.base.test.FakeMail;
33  import org.apache.mailet.base.test.FakeMailContext;
34  import org.apache.mailet.base.test.FakeMatcherConfig;
35  import org.apache.mailet.Matcher;
36  
37  public class SMTPIsAuthNetworkTest extends TestCase {
38  
39      private FakeMail mockedMail;
40  
41      private Matcher matcher;
42  
43      private boolean isAuthorized = false;
44  
45      private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork";
46  
47      public SMTPIsAuthNetworkTest(String arg0)
48              throws UnsupportedEncodingException {
49          super(arg0);
50      }
51  
52      private void setIsAuthorized(boolean isAuthorized) {
53          this.isAuthorized = isAuthorized;
54      }
55  
56      private void setupMockedMail() throws ParseException {
57          mockedMail = MailUtil.createMockMail2Recipients(null);
58          if (isAuthorized) {
59              mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true");
60          }
61      }
62  
63      private void setupMatcher() throws MessagingException {
64          matcher = new SMTPIsAuthNetwork();
65          FakeMatcherConfig mci = new FakeMatcherConfig("SMTPIsAuthNetwork",
66                  new FakeMailContext());
67          matcher.init(mci);
68      }
69  
70      public void testIsAuthNetwork() throws MessagingException {
71          setIsAuthorized(true);
72          setupMockedMail();
73          setupMatcher();
74  
75          Collection matchedRecipients = matcher.match(mockedMail);
76  
77          assertNotNull(matchedRecipients);
78          assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
79                  .size());
80      }
81  
82      public void testIsNotAuthNetwork() throws MessagingException {
83          setIsAuthorized(false);
84          setupMockedMail();
85          setupMatcher();
86  
87          Collection matchedRecipients = matcher.match(mockedMail);
88  
89          assertNull(matchedRecipients);
90      }
91  }