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  package org.apache.mina.transport.socket.nio;
    21
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertTrue;
    24
    25  import java.net.InetSocketAddress;
    26
    27  import org.apache.mina.core.buffer.IoBuffer;
    28  import org.apache.mina.core.filterchain.IoFilter;
    29  import org.apache.mina.core.filterchain.IoFilterAdapter;
    30  import org.apache.mina.core.future.ConnectFuture;
    31  import org.apache.mina.core.future.WriteFuture;
    32  import org.apache.mina.core.service.IoAcceptor;
    33  import org.apache.mina.core.service.IoConnector;
    34  import org.apache.mina.core.service.IoHandler;
    35  import org.apache.mina.core.service.IoHandlerAdapter;
    36  import org.apache.mina.core.session.IoSession;
    37  import org.apache.mina.util.AvailablePortFinder;
    38  import org.junit.After;
    39  import org.junit.Before;
    40  import org.junit.Test;
    41
    42  /**
    43   * Tests if {@link NioDatagramAcceptor} session is configured properly.
    44   *
    45   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    46   */
    47  public class DatagramConfigTest {
    48      private IoAcceptor acceptor;
    49
    50      private IoConnector connector;
    51
    52      String result;
    53
    54      public DatagramConfigTest() {
    55          // Do nothing
    56      }
    57
    58      @Before
    59      public void setUp() throws Exception {
    60          result = "";
    61          acceptor = new NioDatagramAcceptor();
    62          connector = new NioDatagramConnector();
    63      }
    64
    65      @After
    66      public void tearDown() throws Exception {
    67          acceptor.dispose();
    68          connector.dispose();
    69      }
    70
    71      @Test
    72      public void testAcceptorFilterChain() throws Exception {
    73          int port = AvailablePortFinder.getNextAvailable(1024 + 1000);
    74          IoFilter mockFilter = new MockFilter();
    75          IoHandler mockHandler = new MockHandler();
    76
    77          acceptor.getFilterChain().addLast("mock", mockFilter);
    78          acceptor.setHandler(mockHandler);
    79          acceptor.bind(new InetSocketAddress(port));
    80
    81          try {
    82              connector.setHandler(new IoHandlerAdapter());
    83              ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port));
    84              future.awaitUninterruptibly();
    85
    86              WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip());
    87              writeFuture.awaitUninterruptibly();
    88              assertTrue(writeFuture.isWritten());
    89
    90              future.getSession().close(true);
    91
    92              for (int i = 0; i < 30; i++) {
    93                  if (result.length() == 2) {
    94                      break;
    95                  }
    96                  Thread.sleep(100);
    97              }
    98
    99              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 }