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.common;
021:
022: import java.io.IOException;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests {@link IoFuture} implementations.
028: *
029: * @author The Apache MINA Project (dev@mina.apache.org)
030: * @version $Rev: 591310 $, $Date: 2007-11-02 05:57:00 -0600 (Fri, 02 Nov 2007) $
031: */
032: public class FutureTest extends TestCase {
033:
034: public void testCloseFuture() throws Exception {
035: DefaultCloseFuture future = new DefaultCloseFuture(null);
036: assertFalse(future.isReady());
037: assertFalse(future.isClosed());
038:
039: TestThread thread = new TestThread(future);
040: thread.start();
041:
042: future.setClosed();
043: thread.join();
044:
045: assertTrue(thread.success);
046: assertTrue(future.isReady());
047: assertTrue(future.isClosed());
048: }
049:
050: public void testConnectFuture() throws Exception {
051: DefaultConnectFuture future = new DefaultConnectFuture();
052: assertFalse(future.isReady());
053: assertFalse(future.isConnected());
054: assertNull(future.getSession());
055: assertNull(future.getException());
056:
057: TestThread thread = new TestThread(future);
058: thread.start();
059:
060: IoSession session = new DummySession();
061:
062: future.setSession(session);
063: thread.join();
064:
065: assertTrue(thread.success);
066: assertTrue(future.isReady());
067: assertTrue(future.isConnected());
068: assertEquals(session, future.getSession());
069: assertNull(future.getException());
070:
071: future = new DefaultConnectFuture();
072: thread = new TestThread(future);
073: thread.start();
074: future.setException(new IOException());
075: thread.join();
076:
077: assertTrue(thread.success);
078: assertTrue(future.isReady());
079: assertFalse(future.isConnected());
080: assertTrue(future.getException() instanceof IOException);
081:
082: try {
083: future.getSession();
084: fail("IOException should be thrown.");
085: } catch (Exception e) {
086: }
087: }
088:
089: public void testWriteFuture() throws Exception {
090: DefaultWriteFuture future = new DefaultWriteFuture(null);
091: assertFalse(future.isReady());
092: assertFalse(future.isWritten());
093:
094: TestThread thread = new TestThread(future);
095: thread.start();
096:
097: future.setWritten();
098: thread.join();
099:
100: assertTrue(thread.success);
101: assertTrue(future.isReady());
102: assertTrue(future.isWritten());
103:
104: future = new DefaultWriteFuture(null);
105: thread = new TestThread(future);
106: thread.start();
107:
108: future.setException(new Exception());
109: thread.join();
110:
111: assertTrue(thread.success);
112: assertTrue(future.isReady());
113: assertFalse(future.isWritten());
114: assertTrue(future.getException().getClass() == Exception.class);
115: }
116:
117: public void testAddListener() throws Exception {
118: DefaultCloseFuture future = new DefaultCloseFuture(null);
119: assertFalse(future.isReady());
120: assertFalse(future.isClosed());
121:
122: TestListener listener1 = new TestListener();
123: TestListener listener2 = new TestListener();
124: future.addListener(listener1);
125: future.addListener(listener2);
126:
127: TestThread thread = new TestThread(future);
128: thread.start();
129:
130: future.setClosed();
131: thread.join();
132:
133: assertTrue(thread.success);
134: assertTrue(future.isReady());
135: assertTrue(future.isClosed());
136:
137: assertSame(future, listener1.notifiedFuture);
138: assertSame(future, listener2.notifiedFuture);
139: }
140:
141: public void testLateAddListener() throws Exception {
142: DefaultCloseFuture future = new DefaultCloseFuture(null);
143: assertFalse(future.isReady());
144: assertFalse(future.isClosed());
145:
146: TestThread thread = new TestThread(future);
147: thread.start();
148:
149: future.setClosed();
150: thread.join();
151:
152: assertTrue(thread.success);
153: assertTrue(future.isReady());
154: assertTrue(future.isClosed());
155:
156: TestListener listener = new TestListener();
157: future.addListener(listener);
158: assertSame(future, listener.notifiedFuture);
159: }
160:
161: public void testRemoveListener1() throws Exception {
162: DefaultCloseFuture future = new DefaultCloseFuture(null);
163: assertFalse(future.isReady());
164: assertFalse(future.isClosed());
165:
166: TestListener listener1 = new TestListener();
167: TestListener listener2 = new TestListener();
168: future.addListener(listener1);
169: future.addListener(listener2);
170: future.removeListener(listener1);
171:
172: TestThread thread = new TestThread(future);
173: thread.start();
174:
175: future.setClosed();
176: thread.join();
177:
178: assertTrue(thread.success);
179: assertTrue(future.isReady());
180: assertTrue(future.isClosed());
181:
182: assertSame(null, listener1.notifiedFuture);
183: assertSame(future, listener2.notifiedFuture);
184: }
185:
186: public void testRemoveListener2() throws Exception {
187: DefaultCloseFuture future = new DefaultCloseFuture(null);
188: assertFalse(future.isReady());
189: assertFalse(future.isClosed());
190:
191: TestListener listener1 = new TestListener();
192: TestListener listener2 = new TestListener();
193: future.addListener(listener1);
194: future.addListener(listener2);
195: future.removeListener(listener2);
196:
197: TestThread thread = new TestThread(future);
198: thread.start();
199:
200: future.setClosed();
201: thread.join();
202:
203: assertTrue(thread.success);
204: assertTrue(future.isReady());
205: assertTrue(future.isClosed());
206:
207: assertSame(future, listener1.notifiedFuture);
208: assertSame(null, listener2.notifiedFuture);
209: }
210:
211: private static class TestThread extends Thread {
212: private final IoFuture future;
213:
214: private boolean success;
215:
216: public TestThread(IoFuture future) {
217: this .future = future;
218: }
219:
220: @Override
221: public void run() {
222: success = future.awaitUninterruptibly(10000);
223: }
224: }
225:
226: private static class TestListener implements
227: IoFutureListener<IoFuture> {
228: private IoFuture notifiedFuture;
229:
230: public void operationComplete(IoFuture future) {
231: this.notifiedFuture = future;
232: }
233: }
234: }
|