001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.transport.socket.nio;
021
022import static org.junit.Assert.assertEquals;
023
024import java.net.InetSocketAddress;
025
026import org.apache.mina.core.buffer.IoBuffer;
027import org.apache.mina.core.future.ConnectFuture;
028import org.apache.mina.core.future.ReadFuture;
029import org.apache.mina.core.service.IoConnector;
030import org.apache.mina.core.service.IoHandlerAdapter;
031import org.apache.mina.core.session.IoSession;
032import org.apache.mina.util.AvailablePortFinder;
033import org.junit.Test;
034
035/**
036 * Tests a generic {@link IoConnector}.
037 *
038 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
039 */
040public class DIRMINA777Test {
041
042    @Test
043    public void checkReadFuture() throws Throwable {
044        int port = AvailablePortFinder.getNextAvailable(1025);
045        NioSocketAcceptor acceptor = new NioSocketAcceptor();
046        acceptor.setReuseAddress(true);
047        acceptor.setHandler(new IoHandlerAdapter() {
048
049            @Override
050            public void sessionOpened(IoSession session) throws Exception {
051                IoBuffer buffer = IoBuffer.allocate(1);
052                buffer.put((byte) 125);
053                buffer.rewind();
054                session.write(buffer);
055            }
056            
057        });
058        acceptor.bind(new InetSocketAddress(port));
059
060        try {
061            IoConnector connector = new NioSocketConnector();
062            connector.setHandler(new IoHandlerAdapter());
063            ConnectFuture connectFuture = connector.connect(new InetSocketAddress("localhost", port));
064            connectFuture.awaitUninterruptibly();
065            if (connectFuture.getException() != null) {
066                throw connectFuture.getException();
067            }
068            connectFuture.getSession().getConfig().setUseReadOperation(true);
069            ReadFuture readFuture = connectFuture.getSession().read();
070            readFuture.awaitUninterruptibly();
071            if (readFuture.getException() != null) {
072                throw readFuture.getException();
073            }
074            IoBuffer message = (IoBuffer)readFuture.getMessage();
075            assertEquals(1, message.remaining());
076            assertEquals(125,message.get());
077            connectFuture.getSession().close(true);
078        } finally {
079            acceptor.dispose();
080        }
081    }
082
083}