1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  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,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.mina.filter.firewall;
22  
23  import java.net.InetSocketAddress;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.mina.core.session.DummySession;
28  
29  /**
30   * TODO Add documentation
31   * 
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class ConnectionThrottleFilterTest extends TestCase
35  {
36      private ConnectionThrottleFilter filter;
37  
38      private DummySession sessionOne;
39      private DummySession sessionTwo;
40  
41      @Override
42      protected void setUp() throws Exception
43      {
44          filter = new ConnectionThrottleFilter();
45  
46          sessionOne = new DummySession();
47          sessionOne.setRemoteAddress( new InetSocketAddress(1234) );
48          sessionTwo = new DummySession();
49          sessionTwo.setRemoteAddress( new InetSocketAddress(1235) );
50      }
51  
52      @Override
53      protected void tearDown() throws Exception
54      {
55          filter = null;
56      }
57  
58      public void testGoodConnection(){
59          filter.setAllowedInterval( 100 );
60          filter.isConnectionOk( sessionOne );
61          try
62          {
63              Thread.sleep( 1000 );
64          }
65          catch ( InterruptedException e )
66          {
67              //e.printStackTrace();
68          }
69  
70          boolean result = filter.isConnectionOk( sessionOne );
71          assertTrue( result );
72      }
73  
74      public void testBadConnection(){
75          filter.setAllowedInterval( 1000 );
76          filter.isConnectionOk( sessionTwo );
77          assertFalse(filter.isConnectionOk( sessionTwo ));
78      }
79  
80      public static void main(String[] args) {
81       junit.textui.TestRunner.run( ConnectionThrottleFilterTest.class );
82      }
83  }