1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2005  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace.mina;
20  
21  import org.apache.mina.common.IdleStatus;
22  import org.apache.mina.protocol.ProtocolSession;
23  import org.easymock.MockControl;
24  
25  import junit.framework.TestCase;
26  
27  /***
28   * 
29   * 
30   * @author rnewson
31   */
32  public final class IMAP4ProtocolHandlerTest extends TestCase {
33      
34      private IMAP4ProtocolHandler imap4ProtocolHandler;
35  
36      protected void setUp() {
37          imap4ProtocolHandler = new IMAP4ProtocolHandler(null);
38      }
39  
40      public void testMessageReceivedRequiresClientMessage() {
41          try {
42              imap4ProtocolHandler.messageReceived(null, new Object());
43          } catch (AssertionError e) {
44              return;
45          }
46          fail("Expected assertion failure.");
47      }
48      
49      public void testIdleStatusCannotBeNull() {
50          try {
51              imap4ProtocolHandler.sessionIdle(null, null);
52          } catch (AssertionError e) {
53              return;
54          }
55          fail("Expected assertion failure.");
56      }
57      
58      public void testIdleSessionCausesSessionClose() {
59          final MockControl control = MockControl.createStrictControl(ProtocolSession.class);
60          final ProtocolSession protocolSession = (ProtocolSession) control.getMock();
61          control.expectAndReturn(protocolSession.getRemoteAddress(), null);
62          protocolSession.close();
63          control.replay();
64          
65          imap4ProtocolHandler.sessionIdle(protocolSession, IdleStatus.BOTH_IDLE);
66          control.verify();
67      }
68      
69      public void testUncaughtExceptionCausesSessionClose() {
70          final MockControl control = MockControl.createStrictControl(ProtocolSession.class);
71          final ProtocolSession protocolSession = (ProtocolSession) control.getMock();
72          control.expectAndReturn(protocolSession.getRemoteAddress(), null);
73          protocolSession.close();
74          control.replay();
75          
76          imap4ProtocolHandler.exceptionCaught(protocolSession, new Throwable("Unexpected exception."));
77          control.verify();
78      }
79  
80  
81  }