KickJava   Java API By Example, From Geeks To Geeks.

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


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.james.Constants;
24 import org.apache.james.services.AbstractDNSServer;
25 import org.apache.james.services.DNSServer;
26 import org.apache.james.test.mock.avalon.MockServiceManager;
27 import org.apache.james.test.mock.mailet.MockMail;
28 import org.apache.james.test.mock.mailet.MockMailContext;
29 import org.apache.james.test.mock.mailet.MockMatcherConfig;
30
31 import org.apache.mailet.MailAddress;
32 import org.apache.mailet.Matcher;
33
34 import javax.mail.MessagingException JavaDoc;
35 import javax.mail.internet.ParseException JavaDoc;
36
37 import java.io.UnsupportedEncodingException JavaDoc;
38 import java.net.InetAddress JavaDoc;
39 import java.net.UnknownHostException JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.Collection JavaDoc;
42
43 import junit.framework.TestCase;
44
45 public class InSpammerBlacklistTest extends TestCase {
46
47     private MockMail mockedMail;
48
49     private Matcher matcher;
50     
51     private final static String JavaDoc BLACKLIST = "my.black.list.";
52     private final static StringBuffer JavaDoc LISTED_HOST = new StringBuffer JavaDoc("111.222.111.222");
53
54     public InSpammerBlacklistTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
55         super(arg0);
56     }
57
58     private MockServiceManager setUpServiceManager() {
59         MockServiceManager sMan = new MockServiceManager();
60         sMan.put(DNSServer.ROLE, setUpDNSServer());
61         return sMan;
62     }
63     
64     private DNSServer setUpDNSServer() {
65         DNSServer dns = new AbstractDNSServer() {
66             public InetAddress JavaDoc getByName(String JavaDoc name) throws UnknownHostException JavaDoc {
67                 if (name.equals(LISTED_HOST.reverse() + "." + BLACKLIST)) {
68                     return null;
69                 } else {
70                     throw new UnknownHostException JavaDoc("Not listed");
71                 }
72             }
73         };
74         return dns;
75     }
76     private void setupMockedMail(String JavaDoc remoteAddr) throws ParseException JavaDoc {
77         mockedMail = new MockMail();
78         mockedMail.setRemoteAddr(remoteAddr);
79         mockedMail.setRecipients(Arrays.asList(new MailAddress[] {new MailAddress("test@email")}));
80
81     }
82
83     private void setupMatcher(String JavaDoc blacklist) throws MessagingException JavaDoc {
84         matcher = new InSpammerBlacklist();
85         MockMailContext context = new MockMailContext();
86         context.setAttribute(Constants.AVALON_COMPONENT_MANAGER, setUpServiceManager());
87         MockMatcherConfig mci = new MockMatcherConfig("InSpammerBlacklist=" + blacklist,context);
88         matcher.init(mci);
89     }
90
91     
92     public void testInBlackList() throws MessagingException JavaDoc {
93         setupMockedMail(LISTED_HOST.toString());
94         setupMatcher(BLACKLIST);
95
96         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
97
98         assertNotNull(matchedRecipients);
99         assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
100     }
101     
102     public void testNotInBlackList() throws MessagingException JavaDoc {
103         setupMockedMail("212.12.14.1");
104         setupMatcher(BLACKLIST);
105
106         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
107
108         assertNull(matchedRecipients);
109     }
110 }
111
Popular Tags