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 java.net.InetSocketAddress;
23  
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
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  
39  /**
40   * Tests if {@link NioDatagramAcceptor} session is configured properly.
41   *
42   * @author The Apache MINA Project (dev@mina.apache.org)
43   * @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $
44   */
45  public class DatagramConfigTest extends TestCase {
46      private IoAcceptor acceptor;
47      private IoConnector connector;
48      private String result;
49  
50      public DatagramConfigTest() {
51      }
52  
53      @Override
54      protected void setUp() throws Exception {
55          result = "";
56          acceptor = new NioDatagramAcceptor();
57          connector = new NioDatagramConnector();
58      }
59      
60      @Override
61      protected void tearDown() throws Exception {
62          acceptor.dispose();
63          connector.dispose();
64      }
65  
66      public void testAcceptorFilterChain() throws Exception {
67          int port = AvailablePortFinder.getNextAvailable(1024);
68          IoFilter mockFilter = new MockFilter();
69          IoHandler mockHandler = new MockHandler();
70  
71          acceptor.getFilterChain().addLast("mock", mockFilter);
72          acceptor.setHandler(mockHandler);
73          acceptor.bind(new InetSocketAddress(port));
74  
75          try {
76              connector.setHandler(new IoHandlerAdapter());
77              ConnectFuture future = connector.connect(
78                      new InetSocketAddress("127.0.0.1", port));
79              future.awaitUninterruptibly();
80  
81              WriteFuture writeFuture = future.getSession().write(
82                      IoBuffer.allocate(16).putInt(0).flip());
83              writeFuture.awaitUninterruptibly();
84              Assert.assertTrue(writeFuture.isWritten());
85  
86              future.getSession().close(true);
87  
88              for (int i = 0; i < 30; i++) {
89                  if (result.length() == 2) {
90                      break;
91                  }
92                  Thread.sleep(100);
93              }
94  
95              Assert.assertEquals("FH", result);
96          } finally {
97              acceptor.unbind();
98          }
99      }
100 
101     private class MockFilter extends IoFilterAdapter {
102 
103         @Override
104         public void messageReceived(NextFilter nextFilter, IoSession session,
105                 Object message) throws Exception {
106             result += "F";
107             nextFilter.messageReceived(session, message);
108         }
109 
110     }
111 
112     private class MockHandler extends IoHandlerAdapter {
113         @Override
114         public void messageReceived(IoSession session, Object message)
115                 throws Exception {
116             result += "H";
117         }
118     }
119 }