1 19 20 21 22 23 package org.apache.james.smtpserver; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 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 response; 41 42 private SMTPSession setupMockedSession(final int rcptCount) { 43 SMTPSession session = new AbstractSMTPSession() { 44 HashMap state = new HashMap (); 45 boolean processing = false; 46 47 public Map getState() { 48 return state; 49 } 50 51 public boolean isRelayingAllowed() { 52 return false; 53 } 54 55 public void writeResponse(String 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 |