KickJava   Java API By Example, From Geeks To Geeks.

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


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
22
23 package org.apache.james.smtpserver;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.james.smtpserver.core.filter.fastfail.MaxRcptHandler;
32 import org.apache.james.test.mock.avalon.MockLogger;
33 import org.apache.james.util.junkscore.JunkScore;
34 import org.apache.james.util.junkscore.JunkScoreImpl;
35
36
37
38 public class MaxRcptHandlerTest extends TestCase{
39     
40     private String JavaDoc response;
41     
42     private SMTPSession setupMockedSession(final int rcptCount) {
43         SMTPSession session = new AbstractSMTPSession() {
44             HashMap JavaDoc state = new HashMap JavaDoc();
45             boolean processing = false;
46             
47             public Map JavaDoc getState() {
48                 return state;
49             }
50             
51             public boolean isRelayingAllowed() {
52                 return false;
53             }
54             
55             public void writeResponse(String JavaDoc resp) {
56                 response = resp;
57             }
58             
59             public void setStopHandlerProcessing(boolean processing) {
60                 this.processing = processing;
61             }
62             
63             public boolean getStopHandlerProcessing() {
64                 return processing;
65             }
66             
67             public int getRcptCount() {
68                 return rcptCount;
69             }
70             
71         };
72         return session;
73     }
74     
75     public void testRejectMaxRcpt() {
76         SMTPSession session = setupMockedSession(3);
77         MaxRcptHandler handler = new MaxRcptHandler();
78     
79         ContainerUtil.enableLogging(handler,new MockLogger());
80     
81         handler.setAction("reject");
82         handler.setMaxRcpt(2);
83         handler.onCommand(session);
84     
85         assertNotNull("Rejected.. To many recipients", response);
86         assertTrue("Reject.. Stop processing",session.getStopHandlerProcessing());
87     }
88     
89     public void testAddScoreMaxRcpt() {
90         SMTPSession session = setupMockedSession(3);
91         session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
92     
93         MaxRcptHandler handler = new MaxRcptHandler();
94     
95         ContainerUtil.enableLogging(handler,new MockLogger());
96     
97         handler.setAction("junkScore");
98         handler.setScore(20);
99         handler.setMaxRcpt(2);
100         handler.onCommand(session);
101     
102         assertNull("Not Rejected.. we use junkScore action", response);
103         assertFalse("Not Rejected.. we use junkScore action",session.getStopHandlerProcessing());
104         assertEquals("Get Score", ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("MaxRcptCheck"),20.0,0d);
105     }
106     
107     public void testNotRejectMaxRcpt() {
108         SMTPSession session = setupMockedSession(3);
109         MaxRcptHandler handler = new MaxRcptHandler();
110     
111         ContainerUtil.enableLogging(handler,new MockLogger());
112     
113         handler.setAction("reject");
114         handler.setMaxRcpt(4);
115         handler.onCommand(session);
116     
117         assertNull("Not Rejected..", response);
118         assertFalse("Not stop processing",session.getStopHandlerProcessing());
119     }
120
121 }
122
Popular Tags