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;
21  
22  import java.net.InetSocketAddress;
23  
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  
27  import org.apache.mina.common.ConnectFuture;
28  import org.apache.mina.common.IoAcceptor;
29  import org.apache.mina.common.IoConnector;
30  import org.apache.mina.common.IoHandlerAdapter;
31  import org.apache.mina.common.IoSession;
32  import org.apache.mina.common.RuntimeIOException;
33  import org.apache.mina.util.AvailablePortFinder;
34  
35  /**
36   * Tests a generic {@link IoConnector}.
37   *
38   * @author The Apache Directory Project (mina-dev@directory.apache.org)
39   * @version $Rev$, $Date$ 
40   */
41  public abstract class AbstractConnectorTest extends TestCase {
42  
43      protected abstract IoAcceptor createAcceptor();
44  
45      protected abstract IoConnector createConnector();
46  
47      public void testConnectFutureSuccessTiming() throws Exception {
48          int port = AvailablePortFinder.getNextAvailable(1025);
49          IoAcceptor acceptor = createAcceptor();
50          acceptor.bind(new InetSocketAddress(port), new IoHandlerAdapter());
51  
52          try {
53              final StringBuffer buf = new StringBuffer();
54              IoConnector connector = createConnector();
55              ConnectFuture future = connector.connect(new InetSocketAddress(
56                      "localhost", port), new IoHandlerAdapter() {
57                  public void sessionCreated(IoSession session) {
58                      buf.append("1");
59                  }
60  
61                  public void sessionOpened(IoSession session) {
62                      buf.append("2");
63                  }
64  
65                  public void exceptionCaught(IoSession session, Throwable cause) {
66                      buf.append("X");
67                  }
68              });
69  
70              future.join();
71              buf.append("3");
72              future.getSession().close();
73              Assert.assertEquals("123", buf.toString());
74          } finally {
75              acceptor.unbind(new InetSocketAddress(port));
76          }
77      }
78  
79      public void testConnectFutureFailureTiming() throws Exception {
80          int port = AvailablePortFinder.getNextAvailable(1025);
81          final StringBuffer buf = new StringBuffer();
82  
83          IoConnector connector = createConnector();
84          ConnectFuture future = connector.connect(new InetSocketAddress(
85                  "localhost", port), new IoHandlerAdapter() {
86              public void sessionCreated(IoSession session) {
87                  buf.append("X");
88              }
89  
90              public void sessionOpened(IoSession session) {
91                  buf.append("Y");
92              }
93  
94              public void exceptionCaught(IoSession session, Throwable cause) {
95                  buf.append("Z");
96              }
97          });
98  
99          future.join();
100         buf.append("1");
101         try {
102             future.getSession().close();
103             fail();
104         } catch (RuntimeIOException e) {
105             // OK.
106         }
107         Assert.assertEquals("1", buf.toString());
108     }
109 }