1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
78 boolean socketBound = false;
79 boolean datagramBound = false;
80
81
82
83
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
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 }