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.handler.demux;
    21
    22  import org.apache.mina.core.session.IoSession;
    23  import org.easymock.EasyMock;
    24  import org.junit.Before;
    25  import org.junit.Test;
    26
    27  /**
    28   * Tests {@link org.apache.mina.handler.demux.DemuxingIoHandler}.
    29   *
    30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    31   */
    32  @SuppressWarnings("unchecked")
    33  public class DemuxingIoHandlerTest {
    34      MessageHandler handler1;
    35
    36      MessageHandler handler2;
    37
    38      MessageHandler handler3;
    39
    40      IoSession session;
    41
    42      Object[] msg;
    43
    44      @Before
    45      public void setUp() throws Exception {
    46          /*
    47           * Create the messages.
    48           */
    49          msg = new Object[9];
    50          msg[0] = new C1();
    51          msg[1] = new C2();
    52          msg[2] = new C3();
    53          msg[3] = new C1();
    54          msg[4] = new C2();
    55          msg[5] = new C3();
    56          msg[6] = new C1();
    57          msg[7] = new C2();
    58          msg[8] = new C3();
    59
    60          /*
    61           * Create mocks.
    62           */
    63          handler1 = EasyMock.createMock(MessageHandler.class);
    64          handler2 = EasyMock.createMock(MessageHandler.class);
    65          handler3 = EasyMock.createMock(MessageHandler.class);
    66
    67          session = EasyMock.createMock(IoSession.class);
    68      }
    69
    70      @Test
    71      public void testFindHandlerByClass() throws Exception {
    72          /*
    73           * Record expectations.
    74           */
    75          handler1.handleMessage(session, msg[0]);
    76          handler1.handleMessage(session, msg[1]);
    77          handler1.handleMessage(session, msg[2]);
    78          handler1.handleMessage(session, msg[3]);
    79          handler2.handleMessage(session, msg[4]);
    80          handler2.handleMessage(session, msg[5]);
    81          handler1.handleMessage(session, msg[6]);
    82          handler2.handleMessage(session, msg[7]);
    83          handler3.handleMessage(session, msg[8]);
    84
    85          /*
    86           * Replay.
    87           */
    88          EasyMock.replay(handler1);
    89          EasyMock.replay(handler2);
    90          EasyMock.replay(handler3);
    91
    92          DemuxingIoHandler ioHandler = new DemuxingIoHandler();
    93
    94          /*
    95           * First round. All messages should be handled by handler1
    96           */
    97          ioHandler.addReceivedMessageHandler(C1.class, handler1);
    98          ioHandler.messageReceived(session, msg[0]);
    99          ioHandler.messageReceived(session, msg[1]);
    100         ioHandler.messageReceived(session, msg[2]);
    101
    102         /*
    103          * Second round. C1 messages should be handled by handler1. C2 and C3
    104          * messages should be handled by handler2.
    105          */
    106         ioHandler.addReceivedMessageHandler(C2.class, handler2);
    107         ioHandler.messageReceived(session, msg[3]);
    108         ioHandler.messageReceived(session, msg[4]);
    109         ioHandler.messageReceived(session, msg[5]);
    110
    111         /*
    112          * Third round. C1 messages should be handled by handler1, C2 by
    113          * handler2 and C3 by handler3.
    114          */
    115         ioHandler.addReceivedMessageHandler(C3.class, handler3);
    116         ioHandler.messageReceived(session, msg[6]);
    117         ioHandler.messageReceived(session, msg[7]);
    118         ioHandler.messageReceived(session, msg[8]);
    119
    120         /*
    121          * Verify.
    122          */
    123         EasyMock.verify(handler1);
    124         EasyMock.verify(handler2);
    125         EasyMock.verify(handler3);
    126     }
    127
    128     @Test
    129     public void testFindHandlerByInterface() throws Exception {
    130         /*
    131          * Record expectations.
    132          */
    133         handler1.handleMessage(session, msg[0]);
    134         handler1.handleMessage(session, msg[1]);
    135         handler1.handleMessage(session, msg[2]);
    136         handler1.handleMessage(session, msg[3]);
    137         handler2.handleMessage(session, msg[4]);
    138         handler1.handleMessage(session, msg[5]);
    139         handler3.handleMessage(session, msg[6]);
    140         handler2.handleMessage(session, msg[7]);
    141         handler3.handleMessage(session, msg[8]);
    142
    143         /*
    144          * Replay.
    145          */
    146         EasyMock.replay(handler1);
    147         EasyMock.replay(handler2);
    148         EasyMock.replay(handler3);
    149
    150         DemuxingIoHandler ioHandler = new DemuxingIoHandler();
    151
    152         /*
    153          * First round. All messages should be handled by handler1
    154          */
    155         ioHandler.addReceivedMessageHandler(I4.class, handler1);
    156         ioHandler.messageReceived(session, msg[0]);
    157         ioHandler.messageReceived(session, msg[1]);
    158         ioHandler.messageReceived(session, msg[2]);
    159
    160         /*
    161          * Second round. C1 and C3 messages should be handled by handler1. C2
    162          * messages should be handled by handler2.
    163          */
    164         ioHandler.addReceivedMessageHandler(I6.class, handler2);
    165         ioHandler.messageReceived(session, msg[3]);
    166         ioHandler.messageReceived(session, msg[4]);
    167         ioHandler.messageReceived(session, msg[5]);
    168
    169         /*
    170          * Third round. C1 and C3 messages should be handled by handler3. C2
    171          * messages should be handled by handler2.
    172          */
    173         ioHandler.addReceivedMessageHandler(I3.class, handler3);
    174         ioHandler.messageReceived(session, msg[6]);
    175         ioHandler.messageReceived(session, msg[7]);
    176         ioHandler.messageReceived(session, msg[8]);
    177
    178         /*
    179          * Verify.
    180          */
    181         EasyMock.verify(handler1);
    182         EasyMock.verify(handler2);
    183         EasyMock.verify(handler3);
    184     }
    185
    186     /*
    187      * Define some interfaces and classes used when testing the findHandler
    188      * method. This is what the hierarchy looks like:
    189      *
    190      * C3 - I7 - I9
    191      *  |    |   /\
    192      *  |   I8  I3 I4
    193      *  |
    194      * C2 - I5 - I6
    195      *  |
    196      * C1 - I1 - I2 - I4
    197      *            |
    198      *           I3
    199      */
    200
    201     public interface I1 {
    202         // Do nothing
    203     }
    204
    205     public interface I2 extends I3 {
    206         // Do nothing
    207     }
    208
    209     public interface I3 {
    210         // Do nothing
    211     }
    212
    213     public interface I4 {
    214         // Do nothing
    215     }
    216
    217     public static class C1 implements I1, I2, I4 {
    218         // Do nothing
    219     }
    220
    221     public interface I5 {
    222         // Do nothing
    223     }
    224
    225     public interface I6 {
    226         // Do nothing
    227     }
    228
    229     public static class C2 extends C1 implements I5, I6 {
    230         // Do nothing
    231     }
    232
    233     public interface I7 extends I8 {
    234         // Do nothing
    235     }
    236
    237     public interface I8 {
    238         // Do nothing
    239     }
    240
    241     public interface I9 extends I3, I4 {
    242         // Do nothing
    243     }
    244
    245     public static class C3 extends C2 implements I7, I9 {
    246         // Do nothing
    247     }
    248 }