KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > transport > vmpipe > VmPipeEventOrderTest


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 import org.apache.mina.common.ThreadModel;
31
32 /**
33  * Makes sure if the order of event is correct.
34  *
35  * @author The Apache MINA Project Team (dev@mina.apache.org)
36  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
37  */

38 public class VmPipeEventOrderTest extends TestCase {
39     public void testServerToClient() throws Exception JavaDoc {
40         IoAcceptor acceptor = new VmPipeAcceptor();
41         acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
42         //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
43

44         IoConnector connector = new VmPipeConnector();
45         connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
46         //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
47

48         acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() {
49             public void sessionOpened(IoSession session) throws Exception JavaDoc {
50                 session.write("B");
51             }
52
53             public void messageSent(IoSession session, Object JavaDoc message)
54                     throws Exception JavaDoc {
55                 session.close();
56             }
57         });
58
59         final StringBuffer JavaDoc actual = new StringBuffer JavaDoc();
60
61         ConnectFuture future = connector.connect(new VmPipeAddress(1),
62                 new IoHandlerAdapter() {
63
64                     public void messageReceived(IoSession session,
65                             Object JavaDoc message) throws Exception JavaDoc {
66                         actual.append(message);
67                     }
68
69                     public void sessionClosed(IoSession session)
70                             throws Exception JavaDoc {
71                         actual.append("C");
72                     }
73
74                     public void sessionOpened(IoSession session)
75                             throws Exception JavaDoc {
76                         actual.append("A");
77                     }
78
79                 });
80
81         future.join();
82         future.getSession().getCloseFuture().join();
83         acceptor.unbindAll();
84
85         // sessionClosed() might not be invoked yet
86
// even if the connection is closed.
87
while (actual.indexOf("C") < 0) {
88             Thread.yield();
89         }
90
91         Assert.assertEquals("ABC", actual.toString());
92     }
93
94     public void testClientToServer() throws Exception JavaDoc {
95         IoAcceptor acceptor = new VmPipeAcceptor();
96         acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
97         //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
98

99         IoConnector connector = new VmPipeConnector();
100         connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
101         //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
102

103         final StringBuffer JavaDoc actual = new StringBuffer JavaDoc();
104
105         acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() {
106
107             public void messageReceived(IoSession session, Object JavaDoc message)
108                     throws Exception JavaDoc {
109                 actual.append(message);
110             }
111
112             public void sessionClosed(IoSession session) throws Exception JavaDoc {
113                 actual.append("C");
114             }
115
116             public void sessionOpened(IoSession session) throws Exception JavaDoc {
117                 actual.append("A");
118             }
119
120         });
121
122         ConnectFuture future = connector.connect(new VmPipeAddress(1),
123                 new IoHandlerAdapter() {
124                     public void sessionOpened(IoSession session)
125                             throws Exception JavaDoc {
126                         session.write("B");
127                     }
128
129                     public void messageSent(IoSession session, Object JavaDoc message)
130                             throws Exception JavaDoc {
131                         session.close();
132                     }
133                 });
134
135         future.join();
136         future.getSession().getCloseFuture().join();
137         acceptor.unbindAll();
138
139         // sessionClosed() might not be invoked yet
140
// even if the connection is closed.
141
while (actual.indexOf("C") < 0) {
142             Thread.yield();
143         }
144
145         Assert.assertEquals("ABC", actual.toString());
146     }
147 }
148
Popular Tags