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.handlers;
9   
10  import junit.framework.TestCase;
11  import java.net.Socket;
12  
13  /***
14   *
15   * @author Peter Donald
16   * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
17   */
18  public class RequestHandlerTestCase
19      extends TestCase
20  {
21      public void testEndConnectionWithError()
22          throws Exception
23      {
24          final MockRequestHandler handler = new MockRequestHandler();
25          final ExceptionOnCloseSocket socket = new ExceptionOnCloseSocket();
26          handler.endConnection( socket );
27          assertEquals( "getErrorClosingConnectionSocket",
28                        socket,
29                        handler.getErrorClosingConnectionSocket() );
30          assertEquals( "getErrorClosingConnectionThrowable",
31                        ExceptionOnCloseSocket.IO_EXCEPTION,
32                        handler.getErrorClosingConnectionThrowable() );
33      }
34  
35      public void testCreateRunnable()
36          throws Exception
37      {
38          final MockRequestHandler handler = new MockRequestHandler();
39          final Socket socket = new Socket();
40          final Runnable runnable = handler.createRunnable( socket );
41          runnable.run();
42          assertEquals( "getPerformRequestSocket",
43                        socket,
44                        handler.getPerformRequestSocket() );
45      }
46  
47      public void testPerformRequestWithError()
48          throws Exception
49      {
50          final ExceptingRequestHandler handler = new ExceptingRequestHandler();
51          final Socket socket = new Socket();
52          handler.performRequest( socket );
53          assertEquals( "getPerformRequestSocket",
54                        socket,
55                        handler.getPerformRequestSocket() );
56          assertEquals( "getErrorHandlingConnectionSocket",
57                        socket,
58                        handler.getErrorHandlingConnectionSocket() );
59          assertEquals( "getErrorClosingConnectionThrowable",
60                        ExceptingRequestHandler.EXCEPTION,
61                        handler.getErrorHandlingConnectionThrowable() );
62      }
63  
64      public void testShutdownWhileThreadStillGoingButInteruptible()
65          throws Exception
66      {
67          final DelayingRequestHandler handler = new DelayingRequestHandler( 2000, true );
68          final Socket socket = new Socket();
69          final Runnable runnable = new Runnable()
70          {
71              public void run()
72              {
73                  handler.handleConnection( socket );
74              }
75          };
76          final Thread thread = new Thread( runnable );
77          thread.start();
78          Thread.sleep( 50 );
79  
80          handler.shutdown( 50 );
81          assertEquals( "isShutdown", true, handler.isShutdown() );
82          assertEquals( "isExited", true, handler.isExited() );
83          assertEquals( "isExitDueToInterrupt", true, handler.isExitDueToInterrupt() );
84      }
85  
86      public void testShutdownWhileThreadStillGoingAndNotInteruptible()
87          throws Exception
88      {
89          final DelayingRequestHandler handler =
90              new DelayingRequestHandler( 2000, false );
91          final Socket socket = new Socket();
92          final Runnable runnable = new Runnable()
93          {
94              public void run()
95              {
96                  handler.handleConnection( socket );
97              }
98          };
99          final Thread thread = new Thread( runnable );
100         thread.start();
101         Thread.sleep( 50 );
102 
103         handler.shutdown( 50 );
104         assertEquals( "isShutdown", true, handler.isShutdown() );
105         assertEquals( "isExited", false, handler.isExited() );
106         assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
107     }
108 
109     public void testShutdownWhileThreadStillGoingAndWaitIndefinetly()
110         throws Exception
111     {
112         final DelayingRequestHandler handler =
113             new DelayingRequestHandler( 200, false );
114         final Socket socket = new Socket();
115         final Runnable runnable = new Runnable()
116         {
117             public void run()
118             {
119                 handler.handleConnection( socket );
120             }
121         };
122         final Thread thread = new Thread( runnable );
123         thread.start();
124         Thread.sleep( 50 );
125 
126         handler.shutdown( 0 );
127         assertEquals( "isShutdown", true, handler.isShutdown() );
128         assertEquals( "isExited", true, handler.isExited() );
129         assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
130     }
131 }