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