View Javadoc

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 static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.net.InetSocketAddress;
27  
28  import org.apache.mina.core.session.DummySession;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  
34  /**
35   * TODO Add documentation
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class ConnectionThrottleFilterTest
40  {
41      private ConnectionThrottleFilter filter;
42  
43      private DummySession sessionOne;
44      private DummySession sessionTwo;
45  
46      @Before
47      public void setUp() throws Exception
48      {
49          filter = new ConnectionThrottleFilter();
50  
51          sessionOne = new DummySession();
52          sessionOne.setRemoteAddress( new InetSocketAddress(1234) );
53          sessionTwo = new DummySession();
54          sessionTwo.setRemoteAddress( new InetSocketAddress(1235) );
55      }
56  
57      @After
58      public void tearDown() throws Exception
59      {
60          filter = null;
61      }
62  
63      @Test
64      public void testGoodConnection(){
65          filter.setAllowedInterval( 100 );
66          filter.isConnectionOk( sessionOne );
67          
68          try
69          {
70              Thread.sleep( 1000 );
71          }
72          catch ( InterruptedException e )
73          {
74              //e.printStackTrace();
75          }
76  
77          boolean result = filter.isConnectionOk( sessionOne );
78          assertTrue( result );
79      }
80  
81      @Test
82      public void testBadConnection(){
83          filter.setAllowedInterval( 1000 );
84          filter.isConnectionOk( sessionTwo );
85          assertFalse(filter.isConnectionOk( sessionTwo ));
86      }
87  }