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      private IoConnector connector;
50      String result;
51  
52      public DatagramConfigTest() {
53          // Do nothing
54      }
55  
56      @Before
57      public void setUp() throws Exception {
58          result = "";
59          acceptor = new NioDatagramAcceptor();
60          connector = new NioDatagramConnector();
61      }
62      
63      @After
64      public void tearDown() throws Exception {
65          acceptor.dispose();
66          connector.dispose();
67      }
68  
69      @Test
70      public void testAcceptorFilterChain() throws Exception {
71          int port = AvailablePortFinder.getNextAvailable(1024 + 1000);
72          IoFilter mockFilter = new MockFilter();
73          IoHandler mockHandler = new MockHandler();
74  
75          acceptor.getFilterChain().addLast("mock", mockFilter);
76          acceptor.setHandler(mockHandler);
77          acceptor.bind(new InetSocketAddress(port));
78  
79          try {
80              connector.setHandler(new IoHandlerAdapter());
81              ConnectFuture future = connector.connect(
82                      new InetSocketAddress("127.0.0.1", port));
83              future.awaitUninterruptibly();
84  
85              WriteFuture writeFuture = future.getSession().write(
86                      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,
115                 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)
132                 throws Exception {
133             result += "H";
134         }
135     }
136 }