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.filter.codec;
    21
    22  import static org.junit.Assert.fail;
    23  import static org.mockito.Mockito.*;
    24
    25  import java.nio.ByteBuffer;
    26
    27  import org.apache.mina.api.IoSession;
    28  import org.apache.mina.codec.ProtocolDecoder;
    29  import org.apache.mina.codec.ProtocolEncoder;
    30  import org.apache.mina.filterchain.ReadFilterChainController;
    31  import org.apache.mina.filterchain.WriteFilterChainController;
    32  import org.apache.mina.session.AttributeKey;
    33  import org.apache.mina.session.WriteRequest;
    34  import org.junit.Before;
    35  import org.junit.Test;
    36
    37  /**
    38   * Unit tests for {@link ProtocolCodecFilter}
    39   *
    40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    41   */
    42  @SuppressWarnings({ "rawtypes", "unchecked" })
    43  public class ProtocolCodecFilterTest {
    44
    45      private ProtocolCodecFilter filter;
    46
    47      private ProtocolEncoder encoder = mock(ProtocolEncoder.class);
    48
    49      private ProtocolDecoder decoder = mock(ProtocolDecoder.class);
    50
    51      @Before
    52      public void setup() {
    53          filter = new ProtocolCodecFilter(encoder, decoder);
    54      }
    55
    56      @Test
    57      public void null_check() {
    58          try {
    59              new ProtocolCodecFilter(encoder, null);
    60              fail();
    61          } catch (IllegalArgumentException ex) {
    62              // happy
    63          }
    64
    65          try {
    66              new ProtocolCodecFilter(null, decoder);
    67              fail();
    68          } catch (IllegalArgumentException ex) {
    69              // happy
    70          }
    71      }
    72
    73      @Test
    74      public void create_states() {
    75          // prepare
    76          Object decodingState = new Object();
    77          Object encodingState = new Object();
    78          IoSession session = mock(IoSession.class);
    79
    80          when(decoder.createDecoderState()).thenReturn(decodingState);
    81          when(encoder.createEncoderState()).thenReturn(encodingState);
    82
    83          // run
    84          filter.sessionOpened(session);
    85
    86          // verify
    87          verify(decoder).createDecoderState();
    88          verify(session).setAttribute(new AttributeKey<Object>(Object.class, "internal_decoder"), decodingState);
    89          verify(encoder).createEncoderState();
    90          verify(session).setAttribute(new AttributeKey<Object>(Object.class, "internal_encoder"), encodingState);
    91          verifyNoMoreInteractions(encoder, decoder, session);
    92      }
    93
    94      @Test
    95      public void loop_decode_twice() {
    96          // prepare
    97          IoSession session = mock(IoSession.class);
    98          ByteBuffer buff = ByteBuffer.wrap("test".getBytes());
    99
    100         Object decodingState = new Object();
    101
    102         when(session.getAttribute(new AttributeKey<Object>(Object.class, "internal_decoder")))
    103                 .thenReturn(decodingState);
    104
    105         Object decoded = new Object();
    106
    107         when(decoder.decode(buff, decodingState)).thenReturn(decoded).thenReturn(decoded).thenReturn(null);
    108
    109         ReadFilterChainController ctrl = mock(ReadFilterChainController.class);
    110
    111         // run
    112         filter.messageReceived(session, buff, ctrl);
    113
    114         // verify
    115         verify(decoder, times(3)).decode(buff, decodingState);
    116         verify(ctrl, times(2)).callReadNextFilter(decoded);
    117         verify(session).getAttribute(new AttributeKey<Object>(Object.class, "internal_decoder"));
    118         verifyNoMoreInteractions(encoder, decoder, session, ctrl);
    119     }
    120
    121     @Test
    122     public void encode() {
    123         // prepare
    124         IoSession session = mock(IoSession.class);
    125         ByteBuffer buff = ByteBuffer.wrap("test".getBytes());
    126         Object encodingState = new Object();
    127
    128         when(session.getAttribute(new AttributeKey<Object>(Object.class, "internal_encoder")))
    129                 .thenReturn(encodingState);
    130
    131         Object toEncode = new Object();
    132         WriteRequest wrq = mock(WriteRequest.class);
    133         when(wrq.getMessage()).thenReturn(toEncode);
    134
    135         when(encoder.encode(toEncode, encodingState)).thenReturn(buff);
    136
    137         WriteFilterChainController ctrl = mock(WriteFilterChainController.class);
    138
    139         // run
    140         filter.messageWriting(session, wrq, ctrl);
    141
    142         // verify
    143
    144         verify(encoder).encode(toEncode, encodingState);
    145         verify(wrq).getMessage();
    146         verify(wrq).setMessage(buff);
    147
    148         verify(ctrl).callWriteNextFilter(wrq);
    149         verify(session).getAttribute(new AttributeKey<Object>(Object.class, "internal_encoder"));
    150         verifyNoMoreInteractions(encoder, decoder, session, ctrl, wrq);
    151     }
    152 }