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.InetAddress;
26  import java.net.InetSocketAddress;
27  
28  import org.apache.mina.core.buffer.IoBuffer;
29  import org.apache.mina.core.filterchain.IoFilter;
30  import org.apache.mina.core.filterchain.IoFilterAdapter;
31  import org.apache.mina.core.future.ConnectFuture;
32  import org.apache.mina.core.future.WriteFuture;
33  import org.apache.mina.core.service.IoAcceptor;
34  import org.apache.mina.core.service.IoConnector;
35  import org.apache.mina.core.service.IoHandler;
36  import org.apache.mina.core.service.IoHandlerAdapter;
37  import org.apache.mina.core.session.IoSession;
38  import org.apache.mina.util.AvailablePortFinder;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  
43  /**
44   * Tests if {@link NioDatagramAcceptor} session is configured properly.
45   *
46   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
47   */
48  public class DatagramConfigTest {
49      private IoAcceptor acceptor;
50  
51      private IoConnector connector;
52  
53      String result;
54  
55      public DatagramConfigTest() {
56          // Do nothing
57      }
58  
59      @Before
60      public void setUp() throws Exception {
61          result = "";
62          acceptor = new NioDatagramAcceptor();
63          connector = new NioDatagramConnector();
64      }
65  
66      @After
67      public void tearDown() throws Exception {
68          acceptor.dispose();
69          connector.dispose();
70      }
71  
72      @Test
73      public void testAcceptorFilterChain() throws Exception {
74          int port = AvailablePortFinder.getNextAvailable(1024 + 1000);
75          IoFilter mockFilter = new MockFilter();
76          IoHandler mockHandler = new MockHandler();
77  
78          acceptor.getFilterChain().addLast("mock", mockFilter);
79          acceptor.setHandler(mockHandler);
80          acceptor.bind(new InetSocketAddress(port));
81  
82          try {
83              connector.setHandler(new IoHandlerAdapter());
84              ConnectFuture future = connector.connect(new InetSocketAddress(InetAddress.getByName(null), port));
85              future.awaitUninterruptibly();
86  
87              WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip());
88              writeFuture.awaitUninterruptibly();
89              assertTrue(writeFuture.isWritten());
90  
91              future.getSession().closeNow();
92  
93              for (int i = 0; i < 30; i++) {
94                  if (result.length() == 2) {
95                      break;
96                  }
97                  Thread.sleep(100);
98              }
99  
100             assertEquals("FH", result);
101         } finally {
102             acceptor.unbind();
103         }
104     }
105 
106     private class MockFilter extends IoFilterAdapter {
107         /**
108          * Default constructor
109          */
110         public MockFilter() {
111             super();
112         }
113 
114         @Override
115         public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
116             result += "F";
117             nextFilter.messageReceived(session, message);
118         }
119 
120     }
121 
122     private class MockHandler extends IoHandlerAdapter {
123         /**
124          * Default constructor
125          */
126         public MockHandler() {
127             super();
128         }
129 
130         @Override
131         public void messageReceived(IoSession session, Object message) throws Exception {
132             result += "H";
133         }
134     }
135 }