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.service.executor;
    21
    22  import static org.mockito.Mockito.mock;
    23  import static org.mockito.Mockito.verify;
    24  import static org.mockito.Mockito.verifyNoMoreInteractions;
    25  import static org.mockito.Mockito.when;
    26
    27  import org.apache.mina.api.IdleStatus;
    28  import org.apache.mina.api.IoHandler;
    29  import org.apache.mina.api.IoService;
    30  import org.apache.mina.api.IoSession;
    31  import org.junit.Before;
    32  import org.junit.Test;
    33
    34  /**
    35   * Unit test for {@link HandlerCaller}
    36   *
    37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    38   */
    39  public class HandlerCallerTest {
    40      private HandlerCaller caller = new HandlerCaller();
    41
    42      private IoSession session;
    43
    44      private IoService service;
    45
    46      private IoHandler handler;
    47
    48      @Before
    49      public void setup() {
    50          session = mock(IoSession.class);
    51          service = mock(IoService.class);
    52          handler = mock(IoHandler.class);
    53          when(session.getService()).thenReturn(service);
    54          when(service.getIoHandler()).thenReturn(handler);
    55      }
    56
    57      @Test
    58      public void call_open() {
    59          // prepare
    60          OpenEvent event = mock(OpenEvent.class);
    61          when(event.getSession()).thenReturn(session);
    62
    63          // run
    64          caller.visit(event);
    65
    66          // verify
    67          verify(event).getSession();
    68          verify(session).getService();
    69          verify(service).getIoHandler();
    70          verify(handler).sessionOpened(session);
    71
    72          verifyNoMoreInteractions(session, event, handler);
    73      }
    74
    75      @Test
    76      public void call_close() {
    77          // prepare
    78          CloseEvent event = mock(CloseEvent.class);
    79          when(event.getSession()).thenReturn(session);
    80
    81          // run
    82          caller.visit(event);
    83
    84          // verify
    85          verify(event).getSession();
    86          verify(session).getService();
    87          verify(service).getIoHandler();
    88          verify(handler).sessionClosed(session);
    89
    90          verifyNoMoreInteractions(session, event, handler);
    91      }
    92
    93      @Test
    94      public void call_idle() {
    95          // prepare
    96          IdleEvent event = mock(IdleEvent.class);
    97          IdleStatus status = IdleStatus.READ_IDLE;
    98          when(event.getIdleStatus()).thenReturn(status);
    99          when(event.getSession()).thenReturn(session);
    100
    101         // run
    102         caller.visit(event);
    103
    104         // verify
    105         verify(event).getSession();
    106         verify(event).getIdleStatus();
    107         verify(session).getService();
    108         verify(service).getIoHandler();
    109         verify(handler).sessionIdle(session, status);
    110
    111         verifyNoMoreInteractions(session, event, handler);
    112     }
    113
    114     @Test
    115     public void call_receive() {
    116         // prepare
    117         ReceiveEvent event = mock(ReceiveEvent.class);
    118         Object msg = mock(Object.class);
    119         when(event.getMessage()).thenReturn(msg);
    120         when(event.getSession()).thenReturn(session);
    121
    122         // run
    123         caller.visit(event);
    124
    125         // verify
    126         verify(event).getSession();
    127         verify(event).getMessage();
    128         verify(session).getService();
    129         verify(service).getIoHandler();
    130         verify(handler).messageReceived(session, msg);
    131
    132         verifyNoMoreInteractions(session, event, handler);
    133     }
    134
    135     @Test
    136     public void call_msg_sent() {
    137         // prepare
    138         SentEvent event = mock(SentEvent.class);
    139         Object msg = mock(Object.class);
    140         when(event.getMessage()).thenReturn(msg);
    141         when(event.getSession()).thenReturn(session);
    142
    143         // run
    144         caller.visit(event);
    145
    146         // verify
    147         verify(event).getSession();
    148         verify(event).getMessage();
    149         verify(session).getService();
    150         verify(service).getIoHandler();
    151         verify(handler).messageSent(session, msg);
    152
    153         verifyNoMoreInteractions(session, event, handler);
    154     }
    155
    156 }