EMMA Coverage Report (generated Tue Dec 20 11:01:01 KST 2005)
[all classes][org.apache.mina.examples.echoserver]

COVERAGE SUMMARY FOR SOURCE FILE [AcceptorTest.java]

nameclass, %method, %block, %line, %
AcceptorTest.java100% (2/2)53%  (8/15)75%  (206/275)77%  (55/71)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AcceptorTest$1100% (1/1)25%  (2/8)31%  (18/58)40%  (4/10)
createServerSocket (int): ServerSocket 0%   (0/1)0%   (0/5)0%   (0/1)
createServerSocket (int, int): ServerSocket 0%   (0/1)0%   (0/6)0%   (0/1)
createServerSocket (int, int, InetAddress): ServerSocket 0%   (0/1)0%   (0/7)0%   (0/1)
createSocket (InetAddress, int, InetAddress, int): Socket 0%   (0/1)0%   (0/8)0%   (0/1)
createSocket (String, int): Socket 0%   (0/1)0%   (0/6)0%   (0/1)
createSocket (String, int, InetAddress, int): Socket 0%   (0/1)0%   (0/8)0%   (0/1)
AcceptorTest$1 (AcceptorTest): void 100% (1/1)100% (12/12)100% (3/3)
createSocket (InetAddress, int): Socket 100% (1/1)100% (6/6)100% (1/1)
     
class AcceptorTest100% (1/1)86%  (6/7)87%  (188/217)84%  (51/61)
main (String []): void 0%   (0/1)0%   (0/10)0%   (0/2)
testTCP0 (EchoTCPClient): void 100% (1/1)86%  (71/83)79%  (19/24)
testUDP (): void 100% (1/1)88%  (53/60)82%  (14/17)
AcceptorTest (): void 100% (1/1)100% (3/3)100% (2/2)
fillWriteBuffer (byte [], int): void 100% (1/1)100% (17/17)100% (3/3)
testTCP (): void 100% (1/1)100% (8/8)100% (3/3)
testTCPWithSSL (): void 100% (1/1)100% (36/36)100% (10/10)

1/*
2 *   @(#) $Id: AcceptorTest.java 357871 2005-12-20 01:56:40Z 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 */
19package org.apache.mina.examples.echoserver;
20 
21import java.io.IOException;
22import java.net.InetAddress;
23import java.net.ServerSocket;
24import java.net.Socket;
25import java.net.SocketTimeoutException;
26import java.net.UnknownHostException;
27 
28import javax.net.ServerSocketFactory;
29import javax.net.SocketFactory;
30 
31import org.apache.commons.net.EchoTCPClient;
32import org.apache.commons.net.EchoUDPClient;
33import org.apache.mina.common.TransportType;
34import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
35import org.apache.mina.examples.echoserver.ssl.SSLServerSocketFactory;
36import org.apache.mina.examples.echoserver.ssl.SSLSocketFactory;
37import org.apache.mina.io.IoAcceptor;
38import org.apache.mina.io.filter.SSLFilter;
39 
40/**
41 * Tests echo server example.
42 * 
43 * @author The Apache Directory Project (dev@directory.apache.org)
44 * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $
45 */
46public class AcceptorTest extends AbstractTest
47{
48    public AcceptorTest()
49    {
50    }
51 
52    public void testTCP() throws Exception
53    {
54        EchoTCPClient client = new EchoTCPClient();
55        testTCP0( client );
56    }
57 
58    public void testTCPWithSSL() throws Exception
59    {
60        // Add an SSL filter
61        SSLFilter sslFilter =
62            new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
63        IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
64        acceptor.getFilterChain().addLast( "SSL", sslFilter );
65        
66        // Create a commons-net socket factory
67        SSLSocketFactory.setSslEnabled(true);
68        SSLServerSocketFactory.setSslEnabled(true);
69        org.apache.commons.net.SocketFactory factory = new org.apache.commons.net.SocketFactory() {
70 
71            private SocketFactory f = SSLSocketFactory.getSocketFactory();
72            private ServerSocketFactory ssf = SSLServerSocketFactory.getServerSocketFactory();
73 
74            public Socket createSocket( String arg0, int arg1 ) throws UnknownHostException, IOException
75            {
76                return f.createSocket(arg0, arg1);
77            }
78 
79            public Socket createSocket( InetAddress arg0, int arg1 ) throws IOException
80            {
81                return f.createSocket(arg0, arg1);
82            }
83 
84            public Socket createSocket( String arg0, int arg1, InetAddress arg2, int arg3 ) throws UnknownHostException, IOException
85            {
86                return f.createSocket(arg0, arg1, arg2, arg3);
87            }
88 
89            public Socket createSocket( InetAddress arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException
90            {
91                return f.createSocket(arg0, arg1, arg2, arg3);
92            }
93 
94            public ServerSocket createServerSocket( int arg0 ) throws IOException
95            {
96                return ssf.createServerSocket(arg0);
97            }
98 
99            public ServerSocket createServerSocket( int arg0, int arg1 ) throws IOException
100            {
101                return ssf.createServerSocket(arg0, arg1);
102            }
103 
104            public ServerSocket createServerSocket( int arg0, int arg1, InetAddress arg2 ) throws IOException
105            {
106                return ssf.createServerSocket(arg0, arg1, arg2);
107            }
108            
109        };
110        
111        // Create a echo client with SSL factory and test it.
112        EchoTCPClient client = new EchoTCPClient();
113        client.setSocketFactory( factory );
114        testTCP0( client );
115    }
116    
117    private void testTCP0( EchoTCPClient client ) throws Exception
118    {
119        client.connect( InetAddress.getLocalHost(), port );
120        byte[] writeBuf = new byte[ 16 ];
121 
122        for( int i = 0; i < 10; i ++ )
123        {
124            fillWriteBuffer( writeBuf, i );
125            client.getOutputStream().write( writeBuf );
126        }
127 
128        client.setSoTimeout( 30000 );
129 
130        byte[] readBuf = new byte[ writeBuf.length ];
131 
132        for( int i = 0; i < 10; i ++ )
133        {
134            fillWriteBuffer( writeBuf, i );
135 
136            int readBytes = 0;
137            while( readBytes < readBuf.length )
138            {
139                int nBytes = client.getInputStream().read( readBuf,
140                        readBytes, readBuf.length - readBytes );
141 
142                if( nBytes < 0 )
143                    fail( "Unexpected disconnection." );
144 
145                readBytes += nBytes;
146            }
147 
148            assertEquals( writeBuf, readBuf );
149        }
150 
151        client.setSoTimeout( 500 );
152 
153        try
154        {
155            client.getInputStream().read();
156            fail( "Unexpected incoming data." );
157        }
158        catch( SocketTimeoutException e )
159        {
160        }
161 
162        client.disconnect();
163    }
164 
165    public void testUDP() throws Exception
166    {
167        EchoUDPClient client = new EchoUDPClient();
168        client.open();
169        client.setSoTimeout( 3000 );
170 
171        byte[] writeBuf = new byte[ 16 ];
172        byte[] readBuf = new byte[ writeBuf.length ];
173 
174        client.setSoTimeout( 500 );
175 
176        for( int i = 0; i < 10; i ++ )
177        {
178            fillWriteBuffer( writeBuf, i );
179            client.send( writeBuf, writeBuf.length, InetAddress
180                    .getLocalHost(), port );
181 
182            assertEquals( readBuf.length, client.receive( readBuf,
183                    readBuf.length ) );
184            assertEquals( writeBuf, readBuf );
185        }
186 
187        try
188        {
189            client.receive( readBuf );
190            fail( "Unexpected incoming data." );
191        }
192        catch( SocketTimeoutException e )
193        {
194        }
195 
196        client.close();
197    }
198 
199    private void fillWriteBuffer( byte[] writeBuf, int i )
200    {
201        for( int j = writeBuf.length - 1; j >= 0; j -- )
202        {
203            writeBuf[ j ] = ( byte ) ( j + i );
204        }
205    }
206 
207    public static void main( String[] args )
208    {
209        junit.textui.TestRunner.run( AcceptorTest.class );
210    }
211}

[all classes][org.apache.mina.examples.echoserver]
EMMA 2.0.4217 (C) Vladimir Roubtsov