KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.container.ContainerUtil;
24 import org.apache.james.smtpserver.core.POP3BeforeSMTPHandler;
25 import org.apache.james.test.mock.avalon.MockLogger;
26 import org.apache.james.util.POP3BeforeSMTPHelper;
27
28 import junit.framework.TestCase;
29
30 public class POP3BeforeSMTPHandlerTest extends TestCase {
31
32     private SMTPSession mockedSession;
33
34     private void setupMockedSMTPSession() {
35         mockedSession = new AbstractSMTPSession() {
36             private boolean relayingAllowed = false;
37
38             public String JavaDoc getRemoteIPAddress() {
39                 return "192.168.200.1";
40             }
41
42             public boolean isRelayingAllowed() {
43                 return relayingAllowed;
44             }
45
46             public void setRelayingAllowed(boolean relayingAllowed) {
47                 this.relayingAllowed = relayingAllowed;
48             }
49
50         };
51     }
52
53     public void testAuthWorks() {
54
55         POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler();
56
57         ContainerUtil.enableLogging(handler, new MockLogger());
58         setupMockedSMTPSession();
59         POP3BeforeSMTPHelper.addIPAddress("192.168.200.1");
60
61         assertFalse(mockedSession.isRelayingAllowed());
62         handler.onConnect(mockedSession);
63         assertTrue(mockedSession.isRelayingAllowed());
64     }
65
66     public void testIPGetRemoved() {
67         long sleepTime = 100;
68         POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler();
69
70         ContainerUtil.enableLogging(handler, new MockLogger());
71         setupMockedSMTPSession();
72         POP3BeforeSMTPHelper.addIPAddress("192.168.200.1");
73         assertFalse(mockedSession.isRelayingAllowed());
74
75         try {
76             Thread.sleep(sleepTime);
77             POP3BeforeSMTPHelper.removeExpiredIP(10);
78             handler.onConnect(mockedSession);
79             assertFalse(mockedSession.isRelayingAllowed());
80
81         } catch (InterruptedException JavaDoc e) {
82             //ignore
83
}
84     }
85     
86     public void testThrowExceptionOnIllegalExpireTime() {
87         boolean exception = false;
88         POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler();
89
90         ContainerUtil.enableLogging(handler, new MockLogger());
91         setupMockedSMTPSession();
92
93         try {
94             handler.setExpireTime("1 unit");
95         } catch (NumberFormatException JavaDoc e) {
96             exception = true;
97         }
98         assertTrue(exception);
99     }
100     
101     public void testValidExpireTime() {
102         boolean exception = false;
103         POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler();
104
105         ContainerUtil.enableLogging(handler, new MockLogger());
106         setupMockedSMTPSession();
107
108         try {
109             handler.setExpireTime("1 hour");
110         } catch (NumberFormatException JavaDoc e) {
111             exception = true;
112         }
113         assertFalse(exception);
114     }
115     
116 }
117
Popular Tags