1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: NetworkAcceptorTest.java,v 1.1 2005/09/03 23:13:29 mat007 Exp $
29   */
30  
31  package jtge.engine.net;
32  
33  import java.io.IOException;
34  import java.net.ServerSocket;
35  import jtge.engine.io.AsynchronousAcceptor;
36  import jtge.engine.io.IAcceptor;
37  import jtge.engine.io.IConnectionObserver;
38  import jtge.engine.io.IInputOutputFactory;
39  import jtge.util.EasyMockTestCase;
40  
41  /***
42   * Network acceptor test case.
43   * <p>
44   * Those tests are a nightmare because of the synchronous io behaviour... Have to refactor all this and go nio !
45   *
46   * @see IAcceptor
47   * @author Mathieu Champlon
48   * @version $Revision: 1.1 $ $Date: 2005/09/03 23:13:29 $
49   */
50  public class NetworkAcceptorTest extends EasyMockTestCase
51  {
52      /***
53       * Tested object.
54       */
55      private IAcceptor acceptor;
56      /***
57       * Mock objects.
58       */
59      private IInputOutputFactory mockFactory;
60      private IConnectionObserver mockObserver;
61      /***
62       * Dummy objects.
63       */
64      private static final int ASYNCHRONOUS_DELAY = 10; // ms
65      private static final int PORT_NUMBER = 7777;
66  
67      protected void setUp()
68      {
69          mockFactory = (IInputOutputFactory)createMock( IInputOutputFactory.class );
70          mockObserver = (IConnectionObserver)createMock( IConnectionObserver.class );
71          acceptor = new AsynchronousAcceptor( new NetworkAcceptor( mockFactory, mockObserver, PORT_NUMBER ) );
72          assertFalse( isOpen() );
73      }
74  
75      protected void tearDown() throws InterruptedException
76      {
77          acceptor.close();
78          Thread.sleep( ASYNCHRONOUS_DELAY );
79      }
80  
81      private boolean isOpen()
82      {
83          try
84          {
85              final ServerSocket server = new ServerSocket( PORT_NUMBER );
86              server.close();
87              Thread.sleep( ASYNCHRONOUS_DELAY );
88          }
89          catch( Exception e )
90          {
91              return true;
92          }
93          return false;
94      }
95  
96      public void testCreateWithInvalidPort()
97      {
98          try
99          {
100             new NetworkAcceptor( mockFactory, mockObserver, -1 );
101         }
102         catch( Exception e )
103         {
104             return;
105         }
106         fail();
107     }
108 
109     public void testOpenPortAlreadyInUse() throws IOException
110     {
111         final ServerSocket server = new ServerSocket( PORT_NUMBER );
112         replay();
113         assertTrue( isOpen() );
114         try
115         {
116             new NetworkAcceptor( mockFactory, mockObserver, PORT_NUMBER ).open();
117             Thread.sleep( ASYNCHRONOUS_DELAY );
118         }
119         catch( Exception e )
120         {
121             verify();
122             return;
123         }
124         finally
125         {
126             server.close();
127         }
128         fail( "should throw an exception" );
129     }
130 
131     public void testOpen() throws InterruptedException
132     {
133         acceptor.open();
134         Thread.sleep( ASYNCHRONOUS_DELAY );
135         assertTrue( isOpen() );
136     }
137 
138     public void testOpenAndCloseRightAway() throws InterruptedException
139     {
140         acceptor.open();
141         acceptor.close();
142         Thread.sleep( ASYNCHRONOUS_DELAY );
143         assertFalse( isOpen() );
144     }
145 
146     public void testClose() throws InterruptedException
147     {
148         acceptor.open();
149         Thread.sleep( ASYNCHRONOUS_DELAY );
150         assertTrue( isOpen() );
151         acceptor.close();
152         Thread.yield();
153         assertFalse( isOpen() );
154     }
155 
156     public void testCloseWhileNotOpenIsNoOp()
157     {
158         acceptor.close();
159     }
160 
161     public void testCloseTwiceWhileNotOpenIsNoOp()
162     {
163         acceptor.close();
164         acceptor.close();
165     }
166 }