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 */
020package org.apache.mina.transport.socket.nio;
021
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertTrue;
024
025import java.net.InetSocketAddress;
026
027import org.apache.mina.core.buffer.IoBuffer;
028import org.apache.mina.core.filterchain.IoFilter;
029import org.apache.mina.core.filterchain.IoFilterAdapter;
030import org.apache.mina.core.future.ConnectFuture;
031import org.apache.mina.core.future.WriteFuture;
032import org.apache.mina.core.service.IoAcceptor;
033import org.apache.mina.core.service.IoConnector;
034import org.apache.mina.core.service.IoHandler;
035import org.apache.mina.core.service.IoHandlerAdapter;
036import org.apache.mina.core.session.IoSession;
037import org.apache.mina.util.AvailablePortFinder;
038import org.junit.After;
039import org.junit.Before;
040import org.junit.Test;
041
042/**
043 * Tests if {@link NioDatagramAcceptor} session is configured properly.
044 *
045 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
046 */
047public class DatagramConfigTest {
048    private IoAcceptor acceptor;
049
050    private IoConnector connector;
051
052    String result;
053
054    public DatagramConfigTest() {
055        // Do nothing
056    }
057
058    @Before
059    public void setUp() throws Exception {
060        result = "";
061        acceptor = new NioDatagramAcceptor();
062        connector = new NioDatagramConnector();
063    }
064
065    @After
066    public void tearDown() throws Exception {
067        acceptor.dispose();
068        connector.dispose();
069    }
070
071    @Test
072    public void testAcceptorFilterChain() throws Exception {
073        int port = AvailablePortFinder.getNextAvailable(1024 + 1000);
074        IoFilter mockFilter = new MockFilter();
075        IoHandler mockHandler = new MockHandler();
076
077        acceptor.getFilterChain().addLast("mock", mockFilter);
078        acceptor.setHandler(mockHandler);
079        acceptor.bind(new InetSocketAddress(port));
080
081        try {
082            connector.setHandler(new IoHandlerAdapter());
083            ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port));
084            future.awaitUninterruptibly();
085
086            WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip());
087            writeFuture.awaitUninterruptibly();
088            assertTrue(writeFuture.isWritten());
089
090            future.getSession().close(true);
091
092            for (int i = 0; i < 30; i++) {
093                if (result.length() == 2) {
094                    break;
095                }
096                Thread.sleep(100);
097            }
098
099            assertEquals("FH", result);
100        } finally {
101            acceptor.unbind();
102        }
103    }
104
105    private class MockFilter extends IoFilterAdapter {
106        /**
107         * Default constructor
108         */
109        public MockFilter() {
110            super();
111        }
112
113        @Override
114        public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
115            result += "F";
116            nextFilter.messageReceived(session, message);
117        }
118
119    }
120
121    private class MockHandler extends IoHandlerAdapter {
122        /**
123         * Default constructor
124         */
125        public MockHandler() {
126            super();
127        }
128
129        @Override
130        public void messageReceived(IoSession session, Object message) throws Exception {
131            result += "H";
132        }
133    }
134}