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.common.ByteBuffer;
28  import org.apache.mina.common.ConnectFuture;
29  import org.apache.mina.common.IoAcceptor;
30  import org.apache.mina.common.IoConnector;
31  import org.apache.mina.common.IoFilter;
32  import org.apache.mina.common.IoFilterAdapter;
33  import org.apache.mina.common.IoHandler;
34  import org.apache.mina.common.IoHandlerAdapter;
35  import org.apache.mina.common.IoSession;
36  import org.apache.mina.common.WriteFuture;
37  import org.apache.mina.util.AvailablePortFinder;
38  
39  /**
40   * Tests if {@link DatagramAcceptor} session is configured properly.
41   * 
42   * @author The Apache Directory Project (mina-dev@directory.apache.org)
43   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ 
44   */
45  public class DatagramConfigTest extends TestCase {
46      private final IoAcceptor acceptor = new DatagramAcceptor();
47  
48      private final IoConnector connector = new DatagramConnector();
49  
50      private String result;
51  
52      public DatagramConfigTest() {
53      }
54  
55      protected void setUp() throws Exception {
56          result = "";
57      }
58  
59      public void testAcceptorFilterChain() throws Exception {
60          int port = AvailablePortFinder.getNextAvailable(1024);
61          DatagramAcceptorConfig expectedConfig = new DatagramAcceptorConfig();
62          IoFilter mockFilter = new MockFilter();
63          IoHandler mockHandler = new MockHandler();
64  
65          expectedConfig.getFilterChain().addLast("mock", mockFilter);
66          acceptor.bind(new InetSocketAddress(port), mockHandler, expectedConfig);
67  
68          try {
69              ConnectFuture future = connector.connect(new InetSocketAddress(
70                      "localhost", port), new IoHandlerAdapter());
71              future.join();
72  
73              WriteFuture writeFuture = future.getSession().write(
74                      ByteBuffer.allocate(16).putInt(0).flip());
75              writeFuture.join();
76              Assert.assertTrue(writeFuture.isWritten());
77  
78              future.getSession().close();
79  
80              for (int i = 0; i < 30; i++) {
81                  if (result.length() == 2) {
82                      break;
83                  }
84                  Thread.sleep(100);
85              }
86  
87              Assert.assertEquals("FH", result);
88          } finally {
89              acceptor.unbind(new InetSocketAddress(port));
90          }
91      }
92  
93      private class MockFilter extends IoFilterAdapter {
94  
95          public void messageReceived(NextFilter nextFilter, IoSession session,
96                  Object message) throws Exception {
97              result += "F";
98              nextFilter.messageReceived(session, message);
99          }
100 
101     }
102 
103     private class MockHandler extends IoHandlerAdapter {
104         public void messageReceived(IoSession session, Object message)
105                 throws Exception {
106             result += "H";
107         }
108     }
109 }