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  
24  import java.net.InetSocketAddress;
25  
26  import org.apache.mina.core.buffer.IoBuffer;
27  import org.apache.mina.core.future.ConnectFuture;
28  import org.apache.mina.core.future.ReadFuture;
29  import org.apache.mina.core.service.IoConnector;
30  import org.apache.mina.core.service.IoHandlerAdapter;
31  import org.apache.mina.core.session.IoSession;
32  import org.apache.mina.util.AvailablePortFinder;
33  import org.junit.Test;
34  
35  /**
36   * Tests a generic {@link IoConnector}.
37   *
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class DIRMINA777Test {
41  
42      @Test
43      public void checkReadFuture() throws Throwable {
44          int port = AvailablePortFinder.getNextAvailable();
45          NioSocketAcceptor acceptor = new NioSocketAcceptor();
46          acceptor.setReuseAddress(true);
47          acceptor.setHandler(new IoHandlerAdapter() {
48  
49              @Override
50              public void sessionOpened(IoSession session) throws Exception {
51                  IoBuffer buffer = IoBuffer.allocate(1);
52                  buffer.put((byte) 125);
53                  buffer.rewind();
54                  session.write(buffer);
55              }
56              
57          });
58          
59          acceptor.bind(new InetSocketAddress(port));
60  
61          try {
62              IoConnector connector = new NioSocketConnector();
63              connector.setHandler(new IoHandlerAdapter());
64              ConnectFuture connectFuture = connector.connect(new InetSocketAddress("localhost", port));
65              connectFuture.awaitUninterruptibly();
66              
67              if (connectFuture.getException() != null) {
68                  throw connectFuture.getException();
69              }
70              
71              connectFuture.getSession().getConfig().setUseReadOperation(true);
72              ReadFuture readFuture = connectFuture.getSession().read();
73              readFuture.awaitUninterruptibly();
74              if (readFuture.getException() != null) {
75                  throw readFuture.getException();
76              }
77              IoBuffer message = (IoBuffer)readFuture.getMessage();
78              assertEquals(1, message.remaining());
79              assertEquals(125,message.get());
80              connectFuture.getSession().closeNow();
81          } finally {
82              acceptor.dispose();
83          }
84      }
85  }