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.vmpipe;
21  
22  import junit.framework.Assert;
23  import junit.framework.TestCase;
24  
25  import org.apache.mina.common.ConnectFuture;
26  import org.apache.mina.common.IoAcceptor;
27  import org.apache.mina.common.IoConnector;
28  import org.apache.mina.common.IoHandlerAdapter;
29  import org.apache.mina.common.IoSession;
30  
31  /**
32   * Makes sure if the order of event is correct.
33   *
34   * @author The Apache MINA Project Team (dev@mina.apache.org)
35   * @version $Rev: 600461 $, $Date: 2007-12-03 02:55:52 -0700 (Mon, 03 Dec 2007) $
36   */
37  public class VmPipeEventOrderTest extends TestCase {
38      public void testServerToClient() throws Exception {
39          IoAcceptor acceptor = new VmPipeAcceptor();
40          //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
41  
42          IoConnector connector = new VmPipeConnector();
43          //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
44  
45          acceptor.setHandler(new IoHandlerAdapter() {
46              @Override
47              public void sessionOpened(IoSession session) throws Exception {
48                  session.write("B");
49              }
50  
51              @Override
52              public void messageSent(IoSession session, Object message)
53                      throws Exception {
54                  session.close();
55              }
56          });
57  
58          acceptor.bind(new VmPipeAddress(1));
59  
60          final StringBuffer actual = new StringBuffer();
61  
62          connector.setHandler(new IoHandlerAdapter() {
63  
64              @Override
65              public void messageReceived(IoSession session, Object message)
66                      throws Exception {
67                  actual.append(message);
68              }
69  
70              @Override
71              public void sessionClosed(IoSession session) throws Exception {
72                  actual.append("C");
73              }
74  
75              @Override
76              public void sessionOpened(IoSession session) throws Exception {
77                  actual.append("A");
78              }
79  
80          });
81  
82          ConnectFuture future = connector.connect(new VmPipeAddress(1));
83  
84          future.awaitUninterruptibly();
85          future.getSession().getCloseFuture().awaitUninterruptibly();
86          acceptor.dispose();
87  
88          // sessionClosed() might not be invoked yet
89          // even if the connection is closed.
90          while (actual.indexOf("C") < 0) {
91              Thread.yield();
92          }
93  
94          Assert.assertEquals("ABC", actual.toString());
95      }
96  
97      public void testClientToServer() throws Exception {
98          IoAcceptor acceptor = new VmPipeAcceptor();
99          //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
100 
101         IoConnector connector = new VmPipeConnector();
102         //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
103 
104         final StringBuffer actual = new StringBuffer();
105 
106         acceptor.setHandler(new IoHandlerAdapter() {
107 
108             @Override
109             public void messageReceived(IoSession session, Object message)
110                     throws Exception {
111                 actual.append(message);
112             }
113 
114             @Override
115             public void sessionClosed(IoSession session) throws Exception {
116                 actual.append("C");
117             }
118 
119             @Override
120             public void sessionOpened(IoSession session) throws Exception {
121                 actual.append("A");
122             }
123 
124         });
125 
126         acceptor.bind(new VmPipeAddress(1));
127 
128         connector.setHandler(new IoHandlerAdapter() {
129             @Override
130             public void sessionOpened(IoSession session) throws Exception {
131                 session.write("B");
132             }
133 
134             @Override
135             public void messageSent(IoSession session, Object message)
136                     throws Exception {
137                 session.close();
138             }
139         });
140 
141         ConnectFuture future = connector.connect(new VmPipeAddress(1));
142 
143         future.awaitUninterruptibly();
144         future.getSession().getCloseFuture().awaitUninterruptibly();
145         acceptor.dispose();
146         connector.dispose();
147 
148         // sessionClosed() might not be invoked yet
149         // even if the connection is closed.
150         while (actual.indexOf("C") < 0) {
151             Thread.yield();
152         }
153 
154         Assert.assertEquals("ABC", actual.toString());
155     }
156 }