KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > common > support > FutureTest


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.common.support;
21
22 import java.io.IOException JavaDoc;
23 import java.net.SocketAddress JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.mina.common.IoFilterChain;
28 import org.apache.mina.common.IoFuture;
29 import org.apache.mina.common.IoFutureListener;
30 import org.apache.mina.common.IoHandler;
31 import org.apache.mina.common.IoService;
32 import org.apache.mina.common.IoServiceConfig;
33 import org.apache.mina.common.IoSession;
34 import org.apache.mina.common.IoSessionConfig;
35 import org.apache.mina.common.TransportType;
36
37 /**
38  * Tests {@link IoFuture} implementations.
39  *
40  * @author The Apache Directory Project (mina-dev@directory.apache.org)
41  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
42  */

43 public class FutureTest extends TestCase {
44
45     public void testCloseFuture() throws Exception JavaDoc {
46         DefaultCloseFuture future = new DefaultCloseFuture(null);
47         assertFalse(future.isReady());
48         assertFalse(future.isClosed());
49
50         TestThread thread = new TestThread(future);
51         thread.start();
52
53         future.setClosed();
54         thread.join();
55
56         assertTrue(thread.success);
57         assertTrue(future.isReady());
58         assertTrue(future.isClosed());
59     }
60
61     public void testConnectFuture() throws Exception JavaDoc {
62         DefaultConnectFuture future = new DefaultConnectFuture();
63         assertFalse(future.isReady());
64         assertFalse(future.isConnected());
65         assertNull(future.getSession());
66
67         TestThread thread = new TestThread(future);
68         thread.start();
69
70         IoSession session = new BaseIoSession() {
71             public IoHandler getHandler() {
72                 return null;
73             }
74
75             public IoFilterChain getFilterChain() {
76                 return null;
77             }
78
79             public TransportType getTransportType() {
80                 return null;
81             }
82
83             public SocketAddress JavaDoc getRemoteAddress() {
84                 return null;
85             }
86
87             public SocketAddress JavaDoc getLocalAddress() {
88                 return null;
89             }
90
91             public int getScheduledWriteRequests() {
92                 return 0;
93             }
94
95             @Override JavaDoc
96             protected void updateTrafficMask() {
97             }
98
99             @Override JavaDoc
100             public boolean isClosing() {
101                 return false;
102             }
103
104             public IoService getService() {
105                 return null;
106             }
107
108             public IoSessionConfig getConfig() {
109                 return null;
110             }
111
112             public SocketAddress JavaDoc getServiceAddress() {
113                 return null;
114             }
115
116             public int getScheduledWriteBytes() {
117                 return 0;
118             }
119
120             public IoServiceConfig getServiceConfig() {
121                 return null;
122             }
123         };
124
125         future.setSession(session);
126         thread.join();
127
128         assertTrue(thread.success);
129         assertTrue(future.isReady());
130         assertTrue(future.isConnected());
131         assertEquals(session, future.getSession());
132
133         future = new DefaultConnectFuture();
134         thread = new TestThread(future);
135         thread.start();
136         future.setException(new IOException JavaDoc());
137         thread.join();
138
139         assertTrue(thread.success);
140         assertTrue(future.isReady());
141         assertFalse(future.isConnected());
142
143         try {
144             future.getSession();
145             fail("IOException should be thrown.");
146         } catch (Exception JavaDoc e) {
147         }
148     }
149
150     public void testWriteFuture() throws Exception JavaDoc {
151         DefaultWriteFuture future = new DefaultWriteFuture(null);
152         assertFalse(future.isReady());
153         assertFalse(future.isWritten());
154
155         TestThread thread = new TestThread(future);
156         thread.start();
157
158         future.setWritten(true);
159         thread.join();
160
161         assertTrue(thread.success);
162         assertTrue(future.isReady());
163         assertTrue(future.isWritten());
164
165         future = new DefaultWriteFuture(null);
166         thread = new TestThread(future);
167         thread.start();
168
169         future.setWritten(false);
170         thread.join();
171
172         assertTrue(thread.success);
173         assertTrue(future.isReady());
174         assertFalse(future.isWritten());
175     }
176
177     public void testAddListener() throws Exception JavaDoc {
178         DefaultCloseFuture future = new DefaultCloseFuture(null);
179         assertFalse(future.isReady());
180         assertFalse(future.isClosed());
181
182         TestListener listener1 = new TestListener();
183         TestListener listener2 = new TestListener();
184         future.addListener(listener1);
185         future.addListener(listener2);
186
187         TestThread thread = new TestThread(future);
188         thread.start();
189
190         future.setClosed();
191         thread.join();
192
193         assertTrue(thread.success);
194         assertTrue(future.isReady());
195         assertTrue(future.isClosed());
196
197         assertSame(future, listener1.notifiedFuture);
198         assertSame(future, listener2.notifiedFuture);
199     }
200
201     public void testLateAddListener() throws Exception JavaDoc {
202         DefaultCloseFuture future = new DefaultCloseFuture(null);
203         assertFalse(future.isReady());
204         assertFalse(future.isClosed());
205
206         TestThread thread = new TestThread(future);
207         thread.start();
208
209         future.setClosed();
210         thread.join();
211
212         assertTrue(thread.success);
213         assertTrue(future.isReady());
214         assertTrue(future.isClosed());
215
216         TestListener listener = new TestListener();
217         future.addListener(listener);
218         assertSame(future, listener.notifiedFuture);
219     }
220
221     public void testRemoveListener1() throws Exception JavaDoc {
222         DefaultCloseFuture future = new DefaultCloseFuture(null);
223         assertFalse(future.isReady());
224         assertFalse(future.isClosed());
225
226         TestListener listener1 = new TestListener();
227         TestListener listener2 = new TestListener();
228         future.addListener(listener1);
229         future.addListener(listener2);
230         future.removeListener(listener1);
231
232         TestThread thread = new TestThread(future);
233         thread.start();
234
235         future.setClosed();
236         thread.join();
237
238         assertTrue(thread.success);
239         assertTrue(future.isReady());
240         assertTrue(future.isClosed());
241
242         assertSame(null, listener1.notifiedFuture);
243         assertSame(future, listener2.notifiedFuture);
244     }
245
246     public void testRemoveListener2() throws Exception JavaDoc {
247         DefaultCloseFuture future = new DefaultCloseFuture(null);
248         assertFalse(future.isReady());
249         assertFalse(future.isClosed());
250
251         TestListener listener1 = new TestListener();
252         TestListener listener2 = new TestListener();
253         future.addListener(listener1);
254         future.addListener(listener2);
255         future.removeListener(listener2);
256
257         TestThread thread = new TestThread(future);
258         thread.start();
259
260         future.setClosed();
261         thread.join();
262
263         assertTrue(thread.success);
264         assertTrue(future.isReady());
265         assertTrue(future.isClosed());
266
267         assertSame(future, listener1.notifiedFuture);
268         assertSame(null, listener2.notifiedFuture);
269     }
270
271     private static class TestThread extends Thread JavaDoc {
272         private final IoFuture future;
273
274         private boolean success;
275
276         TestThread(IoFuture future) {
277             this.future = future;
278         }
279
280         @Override JavaDoc
281         public void run() {
282             success = future.join(10000);
283         }
284     }
285
286     private static class TestListener implements IoFutureListener {
287         private IoFuture notifiedFuture;
288
289         public void operationComplete(IoFuture future) {
290             this.notifiedFuture = future;
291         }
292     }
293 }
294
Popular Tags