1   /*
2    * Copyright (C) The Spice Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.spice.netserve.connection.impl;
9   
10  import java.net.ServerSocket;
11  
12  import junit.framework.TestCase;
13  
14  /***
15   *
16   * @author Peter Donald
17   * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
18   */
19  public class ConnectionAcceptorTestCase
20     extends TestCase
21  {
22     public void testNullConfigInCtor()
23        throws Exception
24     {
25        try
26        {
27           new ConnectionAcceptor( null,
28                                   new NullAcceptorMonitor() );
29        }
30        catch ( final NullPointerException npe )
31        {
32           assertEquals( "npe.message", "config", npe.getMessage() );
33           return;
34        }
35        fail( "Expected to fail due to NPE for config" );
36     }
37  
38     public void testNullMonitorInCtor()
39        throws Exception
40     {
41        try
42        {
43           new ConnectionAcceptor( new AcceptorConfig( "name",
44                                                       new ServerSocket(),
45                                                       new MockSocketConnectionHandler() ),
46                                   null );
47        }
48        catch ( NullPointerException npe )
49        {
50           assertEquals( "npe.message", "monitor", npe.getMessage() );
51           return;
52        }
53        fail( "Expected to fail due to NPE for monitor" );
54     }
55  
56     public void testShutdownServerSocketCausesError()
57        throws Exception
58     {
59        final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
60        final ConnectionAcceptor acceptor =
61           new ConnectionAcceptor( new AcceptorConfig( "name",
62                                                       new ExceptOnCloseServerSocket(),
63                                                       new MockSocketConnectionHandler() ),
64                                   monitor );
65        assertEquals( "errorClosingServerSocket pre-shutdownServerSocket()",
66                      null,
67                      monitor.getErrorClosingServerSocket() );
68        acceptor.shutdownServerSocket();
69        assertEquals( "errorClosingServerSocket post-shutdownServerSocket()",
70                      ExceptOnCloseServerSocket.EXCEPTION,
71                      monitor.getErrorClosingServerSocket() );
72     }
73  
74     public void testShutdownServerSocket()
75        throws Exception
76     {
77        final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
78        final ConnectionAcceptor acceptor =
79           new ConnectionAcceptor( new AcceptorConfig( "name",
80                                                       new ServerSocket(),
81                                                       new MockSocketConnectionHandler() ),
82                                   monitor );
83        assertEquals( "errorClosingServerSocket pre-shutdownServerSocket()",
84                      null,
85                      monitor.getErrorClosingServerSocket() );
86        acceptor.shutdownServerSocket();
87        assertEquals( "errorClosingServerSocket post-shutdownServerSocket()",
88                      null,
89                      monitor.getErrorClosingServerSocket() );
90     }
91  
92     public void testExceptionOnAccept()
93        throws Exception
94     {
95        final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
96        final ConnectionAcceptor acceptor =
97           new ConnectionAcceptor( new AcceptorConfig( "name",
98                                                       new ExceptOnAcceptServerSocket( false ),
99                                                       new MockSocketConnectionHandler() ),
100                                  monitor );
101       assertEquals( "getErrorAcceptingConnection pre-shutdownServerSocket()",
102                     null,
103                     monitor.getErrorAcceptingConnection() );
104       final Thread thread = startAcceptor( acceptor );
105       waitUntilStarted( acceptor );
106       waitUntilListening( monitor );
107 
108       assertEquals( "getErrorAcceptingConnection post-shutdownServerSocket()",
109                     ExceptOnAcceptServerSocket.ERROR_EXCEPTION,
110                     monitor.getErrorAcceptingConnection() );
111 
112       acceptor.close( 0 );
113       thread.join();
114    }
115 
116    public void testInteruptOnAccept()
117       throws Exception
118    {
119       final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
120       final ConnectionAcceptor acceptor =
121          new ConnectionAcceptor( new AcceptorConfig( "name",
122                                                      new ExceptOnAcceptServerSocket( true ),
123                                                      new MockSocketConnectionHandler() ),
124                                  monitor );
125       final Thread thread = startAcceptor( acceptor );
126       waitUntilStarted( acceptor );
127       waitUntilListening( monitor );
128 
129       try
130       {
131          Thread.sleep( 30 );
132       }
133       catch ( final InterruptedException e )
134       {
135       }
136       assertTrue( "1 < monitor.getListenCount", 1 < monitor.getListenCount() );
137 
138       acceptor.close( 0 );
139       thread.join();
140    }
141 
142    public void _testNormalHandlerAccept()
143       throws Exception
144    {
145       final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
146       final BlockingServerSocket serverSocket = new BlockingServerSocket();
147       final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
148       final ConnectionAcceptor acceptor =
149          new ConnectionAcceptor( new AcceptorConfig( "name",
150                                                      serverSocket,
151                                                      handler ),
152                                  monitor );
153       final Thread thread = startAcceptor( acceptor );
154       waitUntilStarted( acceptor );
155       waitUntilListening( monitor );
156       serverSocket.unlock();
157       try
158       {
159          Thread.sleep( 30 );
160       }
161       catch ( final InterruptedException e )
162       {
163       }
164       assertEquals( "handler.getSocket()",
165                     BlockingServerSocket.SOCKET,
166                     handler.getSocket() );
167       acceptor.close( 50 );
168       serverSocket.unlock();
169       thread.join();
170    }
171 
172    public void testAcceptAfterClose()
173       throws Exception
174    {
175       final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
176       final BlockingServerSocket serverSocket = new BlockingServerSocket();
177       final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
178       final ConnectionAcceptor acceptor =
179          new ConnectionAcceptor( new AcceptorConfig( "name",
180                                                      serverSocket,
181                                                      handler ),
182                                  monitor );
183       final Thread thread = startAcceptor( acceptor );
184       waitUntilStarted( acceptor );
185       waitUntilListening( monitor );
186       acceptor.close( 50 );
187       serverSocket.unlock();
188       waitUntilListening( monitor );
189       assertEquals( "handler.getSocket()",
190                     null,
191                     handler.getSocket() );
192       serverSocket.unlock();
193       thread.join();
194    }
195 
196    private void waitUntilListening( final RecordingAcceptorMonitor monitor )
197    {
198       while ( 0 == monitor.getListenCount() )
199       {
200          try
201          {
202             Thread.sleep( 30 );
203          }
204          catch ( final InterruptedException e )
205          {
206          }
207       }
208    }
209 
210    private Thread startAcceptor( final ConnectionAcceptor acceptor )
211    {
212       final Thread thread = new Thread( acceptor );
213       thread.start();
214       return thread;
215    }
216 
217    private void waitUntilStarted( final ConnectionAcceptor acceptor )
218    {
219       while ( !acceptor.isRunning() )
220       {
221          try
222          {
223             Thread.sleep( 30 );
224          }
225          catch ( final InterruptedException e )
226          {
227          }
228       }
229    }
230 }