View Javadoc

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.core;
21  
22  import java.io.IOException;
23  
24  import org.apache.mina.core.future.DefaultCloseFuture;
25  import org.apache.mina.core.future.DefaultConnectFuture;
26  import org.apache.mina.core.future.DefaultWriteFuture;
27  import org.apache.mina.core.future.IoFuture;
28  import org.apache.mina.core.future.IoFutureListener;
29  import org.apache.mina.core.session.DummySession;
30  import org.apache.mina.core.session.IoSession;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  import static org.junit.Assert.assertNull;
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertSame;
38  import static org.junit.Assert.fail;
39  
40  /**
41   * Tests {@link IoFuture} implementations.
42   *
43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
44   */
45  public class FutureTest {
46  
47      @Test
48      public void testCloseFuture() throws Exception {
49          DefaultCloseFuture future = new DefaultCloseFuture(null);
50          assertFalse(future.isDone());
51          assertFalse(future.isClosed());
52  
53          TestThread thread = new TestThread(future);
54          thread.start();
55  
56          future.setClosed();
57          thread.join();
58  
59          assertTrue(thread.success);
60          assertTrue(future.isDone());
61          assertTrue(future.isClosed());
62      }
63  
64      @Test
65      public void testConnectFuture() throws Exception {
66          DefaultConnectFuture future = new DefaultConnectFuture();
67          assertFalse(future.isDone());
68          assertFalse(future.isConnected());
69          assertNull(future.getSession());
70          assertNull(future.getException());
71  
72          TestThread thread = new TestThread(future);
73          thread.start();
74  
75          IoSession session = new DummySession();
76  
77          future.setSession(session);
78          thread.join();
79  
80          assertTrue(thread.success);
81          assertTrue(future.isDone());
82          assertTrue(future.isConnected());
83          assertEquals(session, future.getSession());
84          assertNull(future.getException());
85  
86          future = new DefaultConnectFuture();
87          thread = new TestThread(future);
88          thread.start();
89          future.setException(new IOException());
90          thread.join();
91  
92          assertTrue(thread.success);
93          assertTrue(future.isDone());
94          assertFalse(future.isConnected());
95          assertTrue(future.getException() instanceof IOException);
96  
97          try {
98              future.getSession();
99              fail("IOException should be thrown.");
100         } catch (Exception e) {
101             // Signifies a successful test execution
102             assertTrue(true);
103         }
104     }
105 
106     @Test
107     public void testWriteFuture() throws Exception {
108         DefaultWriteFuture future = new DefaultWriteFuture(null);
109         assertFalse(future.isDone());
110         assertFalse(future.isWritten());
111 
112         TestThread thread = new TestThread(future);
113         thread.start();
114 
115         future.setWritten();
116         thread.join();
117 
118         assertTrue(thread.success);
119         assertTrue(future.isDone());
120         assertTrue(future.isWritten());
121 
122         future = new DefaultWriteFuture(null);
123         thread = new TestThread(future);
124         thread.start();
125 
126         future.setException(new Exception());
127         thread.join();
128 
129         assertTrue(thread.success);
130         assertTrue(future.isDone());
131         assertFalse(future.isWritten());
132         assertTrue(future.getException().getClass() == Exception.class);
133     }
134 
135     @Test
136     public void testAddListener() throws Exception {
137         DefaultCloseFuture future = new DefaultCloseFuture(null);
138         assertFalse(future.isDone());
139         assertFalse(future.isClosed());
140 
141         TestListener listener1 = new TestListener();
142         TestListener listener2 = new TestListener();
143         future.addListener(listener1);
144         future.addListener(listener2);
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.isDone());
154         assertTrue(future.isClosed());
155 
156         assertSame(future, listener1.notifiedFuture);
157         assertSame(future, listener2.notifiedFuture);
158     }
159 
160     @Test
161     public void testLateAddListener() throws Exception {
162         DefaultCloseFuture future = new DefaultCloseFuture(null);
163         assertFalse(future.isDone());
164         assertFalse(future.isClosed());
165 
166         TestThread thread = new TestThread(future);
167         thread.start();
168 
169         future.setClosed();
170         thread.join();
171 
172         assertTrue(thread.success);
173         assertTrue(future.isDone());
174         assertTrue(future.isClosed());
175 
176         TestListener listener = new TestListener();
177         future.addListener(listener);
178         assertSame(future, listener.notifiedFuture);
179     }
180 
181     @Test
182     public void testRemoveListener1() throws Exception {
183         DefaultCloseFuture future = new DefaultCloseFuture(null);
184         assertFalse(future.isDone());
185         assertFalse(future.isClosed());
186 
187         TestListener listener1 = new TestListener();
188         TestListener listener2 = new TestListener();
189         future.addListener(listener1);
190         future.addListener(listener2);
191         future.removeListener(listener1);
192 
193         TestThread thread = new TestThread(future);
194         thread.start();
195 
196         future.setClosed();
197         thread.join();
198 
199         assertTrue(thread.success);
200         assertTrue(future.isDone());
201         assertTrue(future.isClosed());
202 
203         assertSame(null, listener1.notifiedFuture);
204         assertSame(future, listener2.notifiedFuture);
205     }
206 
207     @Test
208     public void testRemoveListener2() throws Exception {
209         DefaultCloseFuture future = new DefaultCloseFuture(null);
210         assertFalse(future.isDone());
211         assertFalse(future.isClosed());
212 
213         TestListener listener1 = new TestListener();
214         TestListener listener2 = new TestListener();
215         future.addListener(listener1);
216         future.addListener(listener2);
217         future.removeListener(listener2);
218 
219         TestThread thread = new TestThread(future);
220         thread.start();
221 
222         future.setClosed();
223         thread.join();
224 
225         assertTrue(thread.success);
226         assertTrue(future.isDone());
227         assertTrue(future.isClosed());
228 
229         assertSame(future, listener1.notifiedFuture);
230         assertSame(null, listener2.notifiedFuture);
231     }
232 
233     private static class TestThread extends Thread {
234         private final IoFuture future;
235 
236         boolean success;
237 
238         public TestThread(IoFuture future) {
239             this.future = future;
240         }
241 
242         @Override
243         public void run() {
244             success = future.awaitUninterruptibly(10000);
245         }
246     }
247 
248     private static class TestListener implements IoFutureListener<IoFuture> {
249         IoFuture notifiedFuture;
250 
251         /**
252          * Default constructor
253          */
254         public TestListener() {
255             super();
256         }
257 
258         public void operationComplete(IoFuture future) {
259             this.notifiedFuture = future;
260         }
261     }
262 }