KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SpamAssassinHandlerTest


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.smtpserver;
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.mail.MessagingException JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.apache.avalon.framework.container.ContainerUtil;
33 import org.apache.james.smtpserver.core.filter.fastfail.SpamAssassinHandler;
34 import org.apache.james.test.mock.avalon.MockLogger;
35 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
36 import org.apache.james.test.mock.mailet.MockMail;
37 import org.apache.james.test.mock.util.MockSpamd;
38 import org.apache.james.test.util.Util;
39 import org.apache.james.util.SpamAssassinInvoker;
40 import org.apache.mailet.Mail;
41
42 public class SpamAssassinHandlerTest extends TestCase {
43     private SMTPSession mockedSMTPSession;
44
45     private String JavaDoc response = null;
46     
47     public final static String JavaDoc SPAMD_HOST = "localhost";
48
49     public void setUp() {
50         // reset reponse
51
response = null;
52     }
53
54     private SMTPSession setupMockedSMTPSession(final Mail mail) {
55         mockedSMTPSession = new AbstractSMTPSession() {
56
57             private HashMap JavaDoc state = new HashMap JavaDoc();
58
59             private String JavaDoc ipAddress = "192.168.0.1";
60
61             private String JavaDoc host = "localhost";
62
63             private boolean relayingAllowed;
64
65             public void abortMessage() {
66             }
67
68             public Mail getMail() {
69                 return mail;
70             }
71
72             public String JavaDoc getRemoteHost() {
73                 return host;
74             }
75
76             public String JavaDoc getRemoteIPAddress() {
77                 return ipAddress;
78             }
79
80             public Map JavaDoc getState() {
81                 state.put(SMTPSession.SENDER, "sender@james.apache.org");
82                 return state;
83             }
84
85             public boolean isRelayingAllowed() {
86                 return relayingAllowed;
87             }
88
89             public void setRelayingAllowed(boolean relayingAllowed) {
90                 this.relayingAllowed = relayingAllowed;
91             }
92
93             public void writeResponse(String JavaDoc respString) {
94                 response = respString;
95             }
96         };
97
98         return mockedSMTPSession;
99
100     }
101
102     private String JavaDoc getResponse() {
103         return response;
104     }
105
106     private Mail setupMockedMail(MimeMessage JavaDoc message) {
107         MockMail mail = new MockMail();
108         mail.setMessage(message);
109         return mail;
110     }
111
112     public MimeMessage JavaDoc setupMockedMimeMessage(String JavaDoc text)
113             throws MessagingException JavaDoc {
114         MimeMessage JavaDoc message = new MimeMessage JavaDoc(new MockMimeMessage());
115         message.setText(text);
116         message.saveChanges();
117
118         return message;
119     }
120
121     public void testNonSpam() throws IOException JavaDoc, MessagingException JavaDoc {
122
123         int port = Util.getNonPrivilegedPort();
124         MockSpamd spamd = new MockSpamd(port);
125         new Thread JavaDoc(spamd).start();
126
127         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("test")));
128
129         SpamAssassinHandler handler = new SpamAssassinHandler();
130
131         ContainerUtil.enableLogging(handler, new MockLogger());
132         handler.setSpamdHost(SPAMD_HOST);
133         handler.setSpamdPort(port);
134         handler.setSpamdRejectionHits(200.0);
135         handler.onMessage(session);
136
137         assertNull("Email was not rejected", getResponse());
138         assertEquals("email was not spam", session.getMail().getAttribute(
139                 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "NO");
140         assertNotNull("spam hits", session.getMail().getAttribute(
141                 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME));
142
143     }
144
145     public void testSpam() throws IOException JavaDoc, MessagingException JavaDoc {
146
147         int port = Util.getNonPrivilegedPort();
148         new Thread JavaDoc(new MockSpamd(port)).start();
149
150         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE)));
151
152         SpamAssassinHandler handler = new SpamAssassinHandler();
153
154         ContainerUtil.enableLogging(handler, new MockLogger());
155         handler.setSpamdHost(SPAMD_HOST);
156         handler.setSpamdPort(port);
157         handler.setSpamdRejectionHits(2000.0);
158         handler.onMessage(session);
159
160         assertNull("Email was not rejected", getResponse());
161         assertEquals("email was spam", session.getMail().getAttribute(
162                 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "YES");
163         assertNotNull("spam hits", session.getMail().getAttribute(
164                 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME));
165     }
166
167     public void testSpamReject() throws IOException JavaDoc, MessagingException JavaDoc {
168
169         int port = Util.getNonPrivilegedPort();
170         new Thread JavaDoc(new MockSpamd(port)).start();
171
172         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE)));
173
174         SpamAssassinHandler handler = new SpamAssassinHandler();
175
176         ContainerUtil.enableLogging(handler, new MockLogger());
177         handler.setSpamdHost(SPAMD_HOST);
178         handler.setSpamdPort(port);
179         handler.setSpamdRejectionHits(200.0);
180         handler.onMessage(session);
181
182         assertNotNull("Email was rejected", getResponse());
183         assertEquals("email was spam", session.getMail().getAttribute(
184                 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "YES");
185         assertNotNull("spam hits", session.getMail().getAttribute(
186                 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME));
187     }
188
189 }
190
Popular Tags