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: */
020: package org.apache.mina.transport.vmpipe;
021:
022: import junit.framework.Assert;
023: import junit.framework.TestCase;
024:
025: import org.apache.mina.common.ConnectFuture;
026: import org.apache.mina.common.IoAcceptor;
027: import org.apache.mina.common.IoConnector;
028: import org.apache.mina.common.IoHandlerAdapter;
029: import org.apache.mina.common.IoSession;
030:
031: /**
032: * Makes sure if the order of event is correct.
033: *
034: * @author The Apache MINA Project Team (dev@mina.apache.org)
035: * @version $Rev: 600461 $, $Date: 2007-12-03 02:55:52 -0700 (Mon, 03 Dec 2007) $
036: */
037: public class VmPipeEventOrderTest extends TestCase {
038: public void testServerToClient() throws Exception {
039: IoAcceptor acceptor = new VmPipeAcceptor();
040: //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
041:
042: IoConnector connector = new VmPipeConnector();
043: //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
044:
045: acceptor.setHandler(new IoHandlerAdapter() {
046: @Override
047: public void sessionOpened(IoSession session)
048: throws Exception {
049: session.write("B");
050: }
051:
052: @Override
053: public void messageSent(IoSession session, Object message)
054: throws Exception {
055: session.close();
056: }
057: });
058:
059: acceptor.bind(new VmPipeAddress(1));
060:
061: final StringBuffer actual = new StringBuffer();
062:
063: connector.setHandler(new IoHandlerAdapter() {
064:
065: @Override
066: public void messageReceived(IoSession session,
067: Object message) throws Exception {
068: actual.append(message);
069: }
070:
071: @Override
072: public void sessionClosed(IoSession session)
073: throws Exception {
074: actual.append("C");
075: }
076:
077: @Override
078: public void sessionOpened(IoSession session)
079: throws Exception {
080: actual.append("A");
081: }
082:
083: });
084:
085: ConnectFuture future = connector.connect(new VmPipeAddress(1));
086:
087: future.awaitUninterruptibly();
088: future.getSession().getCloseFuture().awaitUninterruptibly();
089: acceptor.dispose();
090:
091: // sessionClosed() might not be invoked yet
092: // even if the connection is closed.
093: while (actual.indexOf("C") < 0) {
094: Thread.yield();
095: }
096:
097: Assert.assertEquals("ABC", actual.toString());
098: }
099:
100: public void testClientToServer() throws Exception {
101: IoAcceptor acceptor = new VmPipeAcceptor();
102: //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
103:
104: IoConnector connector = new VmPipeConnector();
105: //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
106:
107: final StringBuffer actual = new StringBuffer();
108:
109: acceptor.setHandler(new IoHandlerAdapter() {
110:
111: @Override
112: public void messageReceived(IoSession session,
113: Object message) throws Exception {
114: actual.append(message);
115: }
116:
117: @Override
118: public void sessionClosed(IoSession session)
119: throws Exception {
120: actual.append("C");
121: }
122:
123: @Override
124: public void sessionOpened(IoSession session)
125: throws Exception {
126: actual.append("A");
127: }
128:
129: });
130:
131: acceptor.bind(new VmPipeAddress(1));
132:
133: connector.setHandler(new IoHandlerAdapter() {
134: @Override
135: public void sessionOpened(IoSession session)
136: throws Exception {
137: session.write("B");
138: }
139:
140: @Override
141: public void messageSent(IoSession session, Object message)
142: throws Exception {
143: session.close();
144: }
145: });
146:
147: ConnectFuture future = connector.connect(new VmPipeAddress(1));
148:
149: future.awaitUninterruptibly();
150: future.getSession().getCloseFuture().awaitUninterruptibly();
151: acceptor.dispose();
152: connector.dispose();
153:
154: // sessionClosed() might not be invoked yet
155: // even if the connection is closed.
156: while (actual.indexOf("C") < 0) {
157: Thread.yield();
158: }
159:
160: Assert.assertEquals("ABC", actual.toString());
161: }
162: }
|