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  package org.apache.mina.session;
    20
    21  import static org.junit.Assert.*;
    22  import static org.mockito.Matchers.*;
    23  import static org.mockito.Mockito.*;
    24
    25  import java.net.SocketAddress;
    26  import java.nio.ByteBuffer;
    27
    28  import org.apache.mina.api.AbstractIoFilter;
    29  import org.apache.mina.api.IoFilter;
    30  import org.apache.mina.api.IoFuture;
    31  import org.apache.mina.api.IoService;
    32  import org.apache.mina.api.IoSessionConfig;
    33  import org.apache.mina.filterchain.ReadFilterChainController;
    34  import org.apache.mina.filterchain.WriteFilterChainController;
    35  import org.junit.Before;
    36  import org.junit.Test;
    37
    38  /**
    39   * A test class for IoSession
    40   *
    41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    42   */
    43  public class AbstractIoSessionTest {
    44
    45      private class DummySession extends AbstractIoSession {
    46          private DummySession(final IoService service) {
    47              super(service, null);
    48          }
    49
    50          @Override
    51          public IoFuture<Void> close(final boolean immediately) {
    52              return null;
    53          }
    54
    55          @Override
    56          public IoSessionConfig getConfig() {
    57              return null;
    58          }
    59
    60          @Override
    61          public SocketAddress getLocalAddress() {
    62              return null;
    63          }
    64
    65          @Override
    66          public SocketAddress getRemoteAddress() {
    67              return null;
    68          }
    69
    70          @Override
    71          public boolean isConnected() {
    72              return false;
    73          }
    74
    75          @Override
    76          public boolean isReadSuspended() {
    77              return false;
    78          }
    79
    80          @Override
    81          public boolean isWriteSuspended() {
    82              return false;
    83          }
    84
    85          @Override
    86          public void resumeRead() {
    87          }
    88
    89          @Override
    90          public void resumeWrite() {
    91          }
    92
    93          @Override
    94          public void suspendRead() {
    95          }
    96
    97          @Override
    98          public void suspendWrite() {
    99          }
    100
    101         @Override
    102         public boolean isSecuring() {
    103             return false;
    104         }
    105
    106         @Override
    107         public boolean isSecured() {
    108             return false;
    109         }
    110
    111         @Override
    112         public boolean isClosed() {
    113             return false;
    114         }
    115
    116         @Override
    117         public WriteRequest enqueueWriteRequest(WriteRequest writeRequest) {
    118             return null;
    119         }
    120     }
    121
    122     private IoService service = null;
    123
    124     private final IoFilter filter1 = spy(new PassthruFilter());
    125
    126     private final IoFilter filter2 = spy(new PassthruFilter());
    127
    128     private final IoFilter filter3 = spy(new PassthruFilter());
    129
    130     @Before
    131     public void setup() {
    132         service = mock(IoService.class);
    133
    134         when(service.getFilters()).thenReturn(new IoFilter[] { filter1, filter2, filter3 });
    135     }
    136
    137     @Test
    138     public void testGetId() {
    139
    140         assertNotSame((new DummySession(service)).getId(), (new DummySession(service)).getId());
    141
    142     }
    143
    144     @Test
    145     public void testCreationTime() {
    146         final long before = System.currentTimeMillis();
    147         final long creation = (new DummySession(service)).getCreationTime();
    148         final long after = System.currentTimeMillis();
    149         assertTrue(creation <= after);
    150         assertTrue(creation >= before);
    151     }
    152
    153     @Test
    154     public void testAttachment() {
    155         final AbstractIoSession aio = new DummySession(service);
    156         final String value = "value";
    157         final AttributeKey<String> key = new AttributeKey<String>(String.class, "test");
    158         assertNull(aio.getAttribute(key, null));
    159         assertEquals(null, aio.setAttribute(key, value));
    160
    161         assertEquals(aio.getAttributeKeys().size(), 1);
    162         assertEquals(value, aio.setAttribute(key, value));
    163         assertEquals(aio.getAttributeKeys().size(), 1);
    164         assertEquals(value, aio.getAttribute(key, null));
    165         assertEquals(value, aio.removeAttribute(key));
    166         assertEquals(aio.getAttributeKeys().size(), 0);
    167
    168         assertEquals(null, aio.getAttribute(key, null));
    169         assertNotNull(aio.getService());
    170     }
    171
    172     @Test
    173     public void chain_reads() {
    174         final DummySession session = new DummySession(service);
    175         final ByteBuffer buffer = ByteBuffer.allocate(1024);
    176
    177         final long before = System.currentTimeMillis();
    178         session.processMessageReceived(buffer);
    179         verify(filter1).messageReceived(eq(session), eq(buffer), any(ReadFilterChainController.class));
    180         verify(filter2).messageReceived(eq(session), eq(buffer), any(ReadFilterChainController.class));
    181         verify(filter3).messageReceived(eq(session), eq(buffer), any(ReadFilterChainController.class));
    182         assertEquals(1024L, session.getReadBytes());
    183         final long lastRead = session.getLastReadTime();
    184         assertTrue(lastRead - before < 100);
    185     }
    186
    187     @Test
    188     public void chain_writes() {
    189         final DummySession session = new DummySession(service);
    190         final WriteRequest buffer = mock(DefaultWriteRequest.class);
    191         session.processMessageWriting(buffer, null);
    192         verify(filter1).messageWriting(eq(session), eq(buffer), any(WriteFilterChainController.class));
    193         verify(filter2).messageWriting(eq(session), eq(buffer), any(WriteFilterChainController.class));
    194         verify(filter3).messageWriting(eq(session), eq(buffer), any(WriteFilterChainController.class));
    195     }
    196
    197     @Test
    198     public void chain_open() {
    199         final DummySession session = new DummySession(service);
    200         session.processSessionOpen();
    201         verify(filter1).sessionOpened(eq(session));
    202         verify(filter2).sessionOpened(eq(session));
    203         verify(filter3).sessionOpened(eq(session));
    204     }
    205
    206     @Test
    207     public void chain_close() {
    208         final DummySession session = new DummySession(service);
    209         session.processSessionClosed();
    210         verify(filter1).sessionClosed(eq(session));
    211         verify(filter2).sessionClosed(eq(session));
    212         verify(filter3).sessionClosed(eq(session));
    213     }
    214
    215     @Test
    216     public void increment_written_bytes() {
    217         final DummySession session = new DummySession(service);
    218         assertEquals(0, session.getWrittenBytes());
    219         session.incrementWrittenBytes(1024);
    220         assertEquals(1024, session.getWrittenBytes());
    221     }
    222
    223     private class PassthruFilter extends AbstractIoFilter {
    224
    225     }
    226 }