EMMA Coverage Report (generated Fri Oct 21 16:16:13 KST 2005)
[all classes][org.apache.mina.io]

COVERAGE SUMMARY FOR SOURCE FILE [AbstractBindTest.java]

nameclass, %method, %block, %line, %
AbstractBindTest.java100% (1/1)86%  (6/7)44%  (76/173)55%  (24.5/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AbstractBindTest100% (1/1)86%  (6/7)44%  (76/173)55%  (24.5/45)
_testRegressively (): void 0%   (0/1)0%   (0/51)0%   (0/11)
testDuplicateUnbind (): void 100% (1/1)10%  (2/21)33%  (2/6)
testDuplicateBind (): void 100% (1/1)12%  (2/16)40%  (2/5)
setUp (): void 100% (1/1)68%  (28/41)75%  (7.5/10)
AbstractBindTest (IoAcceptor): void 100% (1/1)100% (6/6)100% (3/3)
tearDown (): void 100% (1/1)100% (11/11)100% (4/4)
testManyTimes (): void 100% (1/1)100% (27/27)100% (6/6)

1/*
2 *   @(#) $Id: AbstractBindTest.java 327113 2005-10-21 06:59:15Z 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.io;
20 
21import java.io.IOException;
22import java.net.InetSocketAddress;
23import java.util.Date;
24 
25import junit.framework.Assert;
26import junit.framework.TestCase;
27 
28import org.apache.mina.examples.echoserver.EchoProtocolHandler;
29 
30/**
31 * Tests {@link IoAcceptor} resource leakage by repeating bind and unbind.
32 * 
33 * @author The Apache Directory Project (dev@directory.apache.org)
34 * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $ 
35 */
36public class AbstractBindTest extends TestCase
37{
38    protected final IoAcceptor acceptor;
39    protected int port;
40 
41    public AbstractBindTest( IoAcceptor acceptor )
42    {
43        this.acceptor = acceptor;
44    }
45    
46    public void setUp() throws IOException
47    {
48        // Find an availble test port and bind to it.
49        boolean socketBound = false;
50 
51        // Let's start from port #1 to detect possible resource leak
52        // because test will fail in port 1-1023 if user run this test
53        // as a normal user.
54        for( port = 1; port <= 65535; port ++ )
55        {
56            socketBound = false;
57            try
58            {
59                acceptor.bind( new InetSocketAddress( port ),
60                        new EchoProtocolHandler() );
61                socketBound = true;
62                break;
63            }
64            catch( IOException e )
65            {
66            }
67        }
68 
69        // If there is no port available, test fails.
70        if( !socketBound )
71        {
72            throw new IOException( "Cannot bind any test port." );
73        }
74 
75        //System.out.println( "Using port " + port + " for testing." );
76    }
77    
78    public void tearDown()
79    {
80        try
81        {
82            acceptor.unbind( new InetSocketAddress( port ) );
83        }
84        catch( Exception e )
85        {
86            // ignore
87        }
88    }
89    
90    public void testDuplicateBind()
91    {
92        try
93        {
94            acceptor.bind( new InetSocketAddress( port ), new EchoProtocolHandler() );
95            Assert.fail( "IOException is not thrown" );
96        }
97        catch( IOException e )
98        {
99        }
100    }
101 
102    public void testDuplicateUnbind()
103    {
104        // this should succeed
105        acceptor.unbind( new InetSocketAddress( port ) );
106        
107        try
108        {
109            // this should fail
110            acceptor.unbind( new InetSocketAddress( port ) );
111            Assert.fail( "Exception is not thrown" );
112        }
113        catch( Exception e )
114        {
115        }
116    }
117    
118    public void testManyTimes() throws IOException
119    {
120        InetSocketAddress addr = new InetSocketAddress( port );
121        EchoProtocolHandler handler = new EchoProtocolHandler();
122        for( int i = 0; i < 1024; i++ ) 
123        {
124            acceptor.unbind( addr );
125            acceptor.bind( addr, handler );
126        }
127    }
128    
129    public void _testRegressively() throws IOException
130    {
131        tearDown();
132 
133        InetSocketAddress addr = new InetSocketAddress( port );
134        EchoProtocolHandler handler = new EchoProtocolHandler();
135        for( int i = 0; i < 1048576; i++ )
136        {
137            acceptor.bind( addr, handler );
138            testDuplicateBind();
139            testDuplicateUnbind();
140            if( i % 100 == 0 )
141            {
142                System.out.println( i + " (" + new Date() + ")" );
143            }
144        }
145        setUp();
146    }
147 
148}

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