1   /*
2    *   @(#) $Id: AbstractTest.java 210062 2005-07-11 03:52:38Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.examples.echoserver;
20  
21  import java.io.IOException;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.mina.common.ByteBuffer;
26  import org.apache.mina.common.TransportType;
27  import org.apache.mina.registry.Service;
28  import org.apache.mina.registry.ServiceRegistry;
29  import org.apache.mina.registry.SimpleServiceRegistry;
30  
31  /***
32   * Tests echo server example.
33   * 
34   * @author Trustin Lee (trustin@apache.org)
35   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
36   */
37  public class AbstractTest extends TestCase
38  {
39      protected int port;
40  
41      protected ServiceRegistry registry;
42      
43      protected AbstractTest()
44      {
45      }
46  
47      protected static void assertEquals( byte[] expected, byte[] actual )
48      {
49          assertEquals( toString( expected ), toString( actual ) );
50      }
51  
52      protected static void assertEquals( ByteBuffer expected, ByteBuffer actual )
53      {
54          assertEquals( toString( expected ), toString( actual ) );
55      }
56  
57      protected static String toString( byte[] buf )
58      {
59          StringBuffer str = new StringBuffer( buf.length * 4 );
60          for( int i = 0; i < buf.length; i ++ )
61          {
62              str.append( buf[ i ] );
63              str.append( ' ' );
64          }
65          return str.toString();
66      }
67      
68      protected static String toString( ByteBuffer buf )
69      {
70          return buf.getHexDump();
71      }
72  
73      protected void setUp() throws Exception
74      {
75          registry = new SimpleServiceRegistry();
76          
77          // Find an availble test port and bind to it.
78          boolean socketBound = false;
79          boolean datagramBound = false;
80  
81          // Let's start from port #1 to detect possible resource leak
82          // because test will fail in port 1-1023 if user run this test
83          // as a normal user.
84          for( port = 1; port <= 65535; port ++ )
85          {
86              socketBound = false;
87              datagramBound = false;
88              
89              Service socketService = new Service( "echo", TransportType.SOCKET, port );
90              Service datagramService = new Service( "echo", TransportType.DATAGRAM, port );
91              
92              try
93              {
94                  registry.bind( socketService, new EchoProtocolHandler() );
95                  socketBound = true;
96  
97                  registry.bind( datagramService, new EchoProtocolHandler() );
98                  datagramBound = true;
99  
100                 break;
101             }
102             catch( IOException e )
103             {
104             }
105             finally
106             {
107                 if( !socketBound || !datagramBound )
108                 {
109                     registry.unbindAll();
110                 }
111             }
112         }
113 
114         // If there is no port available, test fails.
115         if( !socketBound || !datagramBound )
116         {
117             throw new IOException( "Cannot bind any test port." );
118         }
119 
120         System.out.println( "Using port " + port + " for testing." );
121     }
122 
123     protected void tearDown() throws Exception
124     {
125         registry.unbindAll();
126     }
127 }