KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.mail.MessagingException JavaDoc;
27 import javax.mail.internet.MimeMessage JavaDoc;
28
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.james.smtpserver.core.filter.fastfail.JunkScoreHandler;
32 import org.apache.james.test.mock.avalon.MockLogger;
33 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
34 import org.apache.james.test.mock.mailet.MockMail;
35 import org.apache.james.util.junkscore.JunkScore;
36 import org.apache.mailet.Mail;
37
38 import junit.framework.TestCase;
39
40 public class JunkScoreHandlerTest extends TestCase {
41     private String JavaDoc response = null;
42     private boolean stopped = false;
43     private boolean messageAborted = false;
44     private final static String JavaDoc KEY1 = "KEY1";
45     private final static String JavaDoc KEY2 = "KEY2";
46     private final static double SCORE1 = 10.0;
47     private final static double SCORE2 = 7.1;
48
49     public void setUp() {
50         response = null;
51         stopped = false;
52         messageAborted = false;
53     }
54     
55     private SMTPSession setupMockedSMTPSession() {
56         SMTPSession session = new AbstractSMTPSession() {
57             HashMap JavaDoc state = new HashMap JavaDoc();
58             HashMap JavaDoc cState = new HashMap JavaDoc();
59             Mail m = null;
60         
61             public Map JavaDoc getState() {
62             state.put(SMTPSession.SENDER, "sender@localhost");
63                 return state;
64             }
65
66             public Map JavaDoc getConnectionState() {
67                 return cState;
68             }
69
70             public void writeResponse(String JavaDoc resp) {
71                 response = resp;
72             }
73
74             public void setStopHandlerProcessing(boolean b) {
75                 stopped = b;
76             }
77
78             public void abortMessage() {
79                 messageAborted = true;
80             }
81             
82             public Mail getMail(){
83                 if (m == null) m = getMockMail();
84                 return m;
85             }
86             
87             public String JavaDoc getRemoteHost() {
88                 return "anyHost";
89             }
90             
91             public String JavaDoc getRemoteIPAddress() {
92                 return "000.000.000.001";
93             }
94         };
95         return session;
96     }
97     
98     private Mail getMockMail(){
99         Mail m = new MockMail();
100         try {
101             m.setMessage(new MockMimeMessage());
102         } catch (MessagingException JavaDoc e) {
103             e.printStackTrace();
104         }
105         return m;
106     }
107     
108     public void testIllegalActionThrowException() {
109         boolean exception = false;
110         JunkScoreHandler handler = new JunkScoreHandler();
111         ContainerUtil.enableLogging(handler,new MockLogger());
112     
113         try {
114             handler.setAction("invalid");
115         } catch (ConfigurationException e) {
116             exception = true;
117         }
118     
119         assertTrue("Exception thrown",exception);
120     }
121     
122     public void testRejectAction() throws ConfigurationException {
123
124         SMTPSession session = setupMockedSMTPSession();
125         JunkScoreHandler handler = new JunkScoreHandler();
126         ContainerUtil.enableLogging(handler,new MockLogger());
127     
128         handler.setAction("reject");
129         handler.setMaxScore(15.0);
130         handler.onConnect(session);
131         handler.onMessage(session);
132
133         assertNull("Not rejected",response);
134         ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).setStoredScore(KEY1, SCORE1);
135         ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).setStoredScore(KEY2, SCORE2);
136         handler.onMessage(session);
137     
138         assertNotNull("Rejected",response);
139         assertTrue("Rejected",stopped);
140         assertTrue("Rejected",messageAborted);
141     }
142     
143     public void testHeaderAction() throws ConfigurationException, MessagingException JavaDoc {
144         SMTPSession session = setupMockedSMTPSession();
145         JunkScoreHandler handler = new JunkScoreHandler();
146         ContainerUtil.enableLogging(handler,new MockLogger());
147     
148         handler.setAction("header");
149
150         handler.onConnect(session);
151         ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).setStoredScore(KEY1, SCORE1);
152         ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).setStoredScore(KEY2, SCORE2);
153         handler.onMessage(session);
154     
155         MimeMessage JavaDoc message = session.getMail().getMessage();
156         assertNotNull("Header added",message.getHeader("X-JUNKSCORE")[0]);
157         assertNotNull("Header added",message.getHeader("X-JUNKSCORE-COMPOSED")[0]);
158     }
159 }
160
Popular Tags