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 The Apache MINA Project (dev@mina.apache.org)
33   * @version $Rev$, $Date$
34   */
35  public class ConnectionThrottleFilterTest extends TestCase
36  {
37      private ConnectionThrottleFilter filter;
38  
39      private DummySession sessionOne;
40      private DummySession sessionTwo;
41  
42      @Override
43      protected void setUp() throws Exception
44      {
45          filter = new ConnectionThrottleFilter();
46  
47          sessionOne = new DummySession();
48          sessionOne.setRemoteAddress( new InetSocketAddress(1234) );
49          sessionTwo = new DummySession();
50          sessionTwo.setRemoteAddress( new InetSocketAddress(1235) );
51      }
52  
53      @Override
54      protected void tearDown() throws Exception
55      {
56          filter = null;
57      }
58  
59      public void testGoodConnection(){
60          filter.setAllowedInterval( 100 );
61          filter.isConnectionOk( sessionOne );
62          try
63          {
64              Thread.sleep( 1000 );
65          }
66          catch ( InterruptedException e )
67          {
68              //e.printStackTrace();
69          }
70  
71          boolean result = filter.isConnectionOk( sessionOne );
72          assertTrue( result );
73      }
74  
75      public void testBadConnection(){
76          filter.setAllowedInterval( 1000 );
77          filter.isConnectionOk( sessionTwo );
78          assertFalse(filter.isConnectionOk( sessionTwo ));
79      }
80  
81      public static void main(String[] args) {
82       junit.textui.TestRunner.run( ConnectionThrottleFilterTest.class );
83      }
84  }