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.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  
25  import junit.framework.Assert;
26  import junit.framework.TestCase;
27  
28  import org.apache.mina.common.IoAcceptor;
29  import org.apache.mina.common.IoAcceptorConfig;
30  import org.apache.mina.common.IoConnector;
31  import org.apache.mina.common.IoFilterChain;
32  import org.apache.mina.common.IoHandler;
33  import org.apache.mina.common.IoService;
34  import org.apache.mina.common.IoServiceConfig;
35  import org.apache.mina.common.IoServiceListener;
36  import org.apache.mina.common.IoSessionConfig;
37  import org.apache.mina.common.TransportType;
38  import org.easymock.MockControl;
39  
40  /**
41   * Tests {@link IoServiceListenerSupport}.
42   * 
43   * @author The Apache Directory Project (mina-dev@directory.apache.org)
44   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $ 
45   */
46  public class IoServiceListenerSupportTest extends TestCase {
47      private static final SocketAddress ADDRESS = new InetSocketAddress(8080);
48  
49      public void testServiceLifecycle() throws Exception {
50          IoServiceListenerSupport support = new IoServiceListenerSupport();
51  
52          MockControl listenerControl = MockControl
53                  .createStrictControl(IoServiceListener.class);
54          IoServiceListener listener = (IoServiceListener) listenerControl
55                  .getMock();
56  
57          // Test activation
58          listener.serviceActivated(null, ADDRESS, null, null);
59  
60          listenerControl.replay();
61  
62          support.add(listener);
63          support.fireServiceActivated(null, ADDRESS, null, null);
64  
65          listenerControl.verify();
66  
67          Assert.assertEquals(1, support.getManagedServiceAddresses().size());
68          Assert.assertTrue(support.getManagedServiceAddresses()
69                  .contains(ADDRESS));
70  
71          // Test deactivation & other side effects
72          listenerControl.reset();
73          listener.serviceDeactivated(null, ADDRESS, null, null);
74  
75          listenerControl.replay();
76          //// Activate more than once
77          support.fireServiceActivated(null, ADDRESS, null, null);
78          //// Deactivate
79          support.fireServiceDeactivated(null, ADDRESS, null, null);
80          //// Deactivate more than once
81          support.fireServiceDeactivated(null, ADDRESS, null, null);
82  
83          listenerControl.verify();
84  
85          Assert.assertEquals(0, support.getManagedServiceAddresses().size());
86          Assert.assertFalse(support.getManagedServiceAddresses().contains(
87                  ADDRESS));
88      }
89  
90      public void testSessionLifecycle() throws Exception {
91          IoServiceListenerSupport support = new IoServiceListenerSupport();
92  
93          TestSession session = new TestSession(ADDRESS);
94  
95          MockControl chainControl = MockControl
96                  .createStrictControl(IoFilterChain.class);
97          IoFilterChain chain = (IoFilterChain) chainControl.getMock();
98          session.setFilterChain(chain);
99  
100         MockControl listenerControl = MockControl
101                 .createStrictControl(IoServiceListener.class);
102         IoServiceListener listener = (IoServiceListener) listenerControl
103                 .getMock();
104 
105         // Test creation
106         listener.sessionCreated(session);
107         chain.fireSessionCreated(session);
108         chain.fireSessionOpened(session);
109 
110         listenerControl.replay();
111         chainControl.replay();
112 
113         support.add(listener);
114         support.fireSessionCreated(session);
115 
116         listenerControl.verify();
117         chainControl.verify();
118 
119         Assert.assertEquals(1, support.getManagedSessions(ADDRESS).size());
120         Assert
121                 .assertTrue(support.getManagedSessions(ADDRESS).contains(
122                         session));
123 
124         // Test destruction & other side effects
125         listenerControl.reset();
126         chainControl.reset();
127         chain.fireSessionClosed(session);
128         listener.sessionDestroyed(session);
129 
130         listenerControl.replay();
131         //// Activate more than once
132         support.fireSessionCreated(session);
133         //// Deactivate
134         support.fireSessionDestroyed(session);
135         //// Deactivate more than once
136         support.fireSessionDestroyed(session);
137 
138         listenerControl.verify();
139 
140         Assert.assertFalse(session.isClosing());
141         Assert.assertEquals(0, support.getManagedSessions(ADDRESS).size());
142         Assert.assertFalse(support.getManagedSessions(ADDRESS)
143                 .contains(session));
144     }
145 
146     public void testDisconnectOnUnbind() throws Exception {
147         final IoServiceListenerSupport support = new IoServiceListenerSupport();
148 
149         MockControl acceptorControl = MockControl
150                 .createStrictControl(IoAcceptor.class);
151         IoAcceptor acceptor = (IoAcceptor) acceptorControl.getMock();
152 
153         final TestSession session = new TestSession(acceptor, ADDRESS);
154 
155         MockControl configControl = MockControl
156                 .createStrictControl(IoAcceptorConfig.class);
157         IoAcceptorConfig config = (IoAcceptorConfig) configControl.getMock();
158 
159         MockControl chainControl = MockControl
160                 .createStrictControl(IoFilterChain.class);
161         IoFilterChain chain = (IoFilterChain) chainControl.getMock();
162         session.setFilterChain(chain);
163 
164         MockControl listenerControl = MockControl
165                 .createStrictControl(IoServiceListener.class);
166         IoServiceListener listener = (IoServiceListener) listenerControl
167                 .getMock();
168 
169         // Activate a service and create a session.
170         listener.serviceActivated(acceptor, ADDRESS, null, config);
171         listener.sessionCreated(session);
172         chain.fireSessionCreated(session);
173         chain.fireSessionOpened(session);
174 
175         listenerControl.replay();
176         chainControl.replay();
177 
178         support.add(listener);
179         support.fireServiceActivated(acceptor, ADDRESS, null, config);
180         support.fireSessionCreated(session);
181 
182         listenerControl.verify();
183         chainControl.verify();
184 
185         // Deactivate a service and make sure the session is closed & destroyed.
186         listenerControl.reset();
187         chainControl.reset();
188 
189         listener.serviceDeactivated(acceptor, ADDRESS, null, config);
190         configControl.expectAndReturn(config.isDisconnectOnUnbind(), true);
191         listener.sessionDestroyed(session);
192         chain.fireSessionClosed(session);
193 
194         listenerControl.replay();
195         configControl.replay();
196         chainControl.replay();
197 
198         new Thread() {
199             // Emulate I/O service
200             public void run() {
201                 try {
202                     Thread.sleep(500);
203                 } catch (InterruptedException e) {
204                     e.printStackTrace();
205                 }
206                 support.fireSessionDestroyed(session);
207             }
208         }.start();
209         support.fireServiceDeactivated(acceptor, ADDRESS, null, config);
210 
211         listenerControl.verify();
212         configControl.verify();
213         chainControl.verify();
214 
215         Assert.assertTrue(session.isClosing());
216         Assert.assertEquals(0, support.getManagedSessions(ADDRESS).size());
217         Assert.assertFalse(support.getManagedSessions(ADDRESS)
218                 .contains(session));
219     }
220 
221     public void testConnectorActivation() throws Exception {
222         IoServiceListenerSupport support = new IoServiceListenerSupport();
223 
224         MockControl connectorControl = MockControl
225                 .createStrictControl(IoConnector.class);
226         IoConnector connector = (IoConnector) connectorControl.getMock();
227 
228         final TestSession session = new TestSession(connector, ADDRESS);
229 
230         MockControl chainControl = MockControl
231                 .createStrictControl(IoFilterChain.class);
232         IoFilterChain chain = (IoFilterChain) chainControl.getMock();
233         session.setFilterChain(chain);
234 
235         MockControl listenerControl = MockControl
236                 .createStrictControl(IoServiceListener.class);
237         IoServiceListener listener = (IoServiceListener) listenerControl
238                 .getMock();
239 
240         // Creating a session should activate a service automatically.
241         listener.serviceActivated(connector, ADDRESS, null, null);
242         listener.sessionCreated(session);
243         chain.fireSessionCreated(session);
244         chain.fireSessionOpened(session);
245 
246         listenerControl.replay();
247         chainControl.replay();
248 
249         support.add(listener);
250         support.fireSessionCreated(session);
251 
252         listenerControl.verify();
253         chainControl.verify();
254 
255         // Destroying a session should deactivate a service automatically.
256         listenerControl.reset();
257         chainControl.reset();
258         listener.sessionDestroyed(session);
259         chain.fireSessionClosed(session);
260         listener.serviceDeactivated(connector, ADDRESS, null, null);
261 
262         listenerControl.replay();
263         chainControl.replay();
264 
265         support.fireSessionDestroyed(session);
266 
267         listenerControl.verify();
268         chainControl.verify();
269 
270         Assert.assertEquals(0, support.getManagedSessions(ADDRESS).size());
271         Assert.assertFalse(support.getManagedSessions(ADDRESS)
272                 .contains(session));
273     }
274 
275     private static class TestSession extends BaseIoSession {
276         private final IoService service;
277 
278         private final SocketAddress serviceAddress;
279 
280         private IoFilterChain filterChain;
281 
282         TestSession(SocketAddress serviceAddress) {
283             this(null, serviceAddress);
284         }
285 
286         TestSession(IoService service, SocketAddress serviceAddress) {
287             this.service = service;
288             this.serviceAddress = serviceAddress;
289         }
290 
291         protected void updateTrafficMask() {
292         }
293 
294         public IoSessionConfig getConfig() {
295             return null;
296         }
297 
298         public IoFilterChain getFilterChain() {
299             return filterChain;
300         }
301 
302         public void setFilterChain(IoFilterChain filterChain) {
303             this.filterChain = filterChain;
304         }
305 
306         public IoHandler getHandler() {
307             return null;
308         }
309 
310         public SocketAddress getLocalAddress() {
311             return null;
312         }
313 
314         public SocketAddress getRemoteAddress() {
315             return null;
316         }
317 
318         public int getScheduledWriteBytes() {
319             return 0;
320         }
321 
322         public int getScheduledWriteRequests() {
323             return 0;
324         }
325 
326         public IoService getService() {
327             return service;
328         }
329 
330         public SocketAddress getServiceAddress() {
331             return serviceAddress;
332         }
333 
334         public IoServiceConfig getServiceConfig() {
335             return null;
336         }
337 
338         public TransportType getTransportType() {
339             return null;
340         }
341     }
342 }