001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.mina.filter.firewall;
022
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertTrue;
025
026import java.net.InetSocketAddress;
027
028import org.apache.mina.core.session.DummySession;
029import org.junit.After;
030import org.junit.Before;
031import org.junit.Test;
032
033/**
034 * TODO Add documentation
035 * 
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 */
038public class ConnectionThrottleFilterTest {
039    private ConnectionThrottleFilter filter;
040
041    private DummySession sessionOne;
042
043    private DummySession sessionTwo;
044
045    @Before
046    public void setUp() throws Exception {
047        filter = new ConnectionThrottleFilter();
048
049        sessionOne = new DummySession();
050        sessionOne.setRemoteAddress(new InetSocketAddress(1234));
051        sessionTwo = new DummySession();
052        sessionTwo.setRemoteAddress(new InetSocketAddress(1235));
053    }
054
055    @After
056    public void tearDown() throws Exception {
057        filter = null;
058    }
059
060    @Test
061    public void testGoodConnection() {
062        filter.setAllowedInterval(100);
063        filter.isConnectionOk(sessionOne);
064
065        try {
066            Thread.sleep(1000);
067        } catch (InterruptedException e) {
068            //e.printStackTrace();
069        }
070
071        boolean result = filter.isConnectionOk(sessionOne);
072        assertTrue(result);
073    }
074
075    @Test
076    public void testBadConnection() {
077        filter.setAllowedInterval(1000);
078        filter.isConnectionOk(sessionTwo);
079        assertFalse(filter.isConnectionOk(sessionTwo));
080    }
081}