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.buffer;
    21
    22  import static org.junit.Assert.assertEquals;
    23
    24  import org.apache.mina.core.buffer.IoBuffer;
    25  import org.apache.mina.core.filterchain.IoFilterAdapter;
    26  import org.apache.mina.core.session.DummySession;
    27  import org.apache.mina.core.session.IoSession;
    28  import org.apache.mina.core.write.WriteRequest;
    29  import org.apache.mina.filter.logging.LoggingFilter;
    30  import org.junit.Test;
    31  import org.slf4j.Logger;
    32  import org.slf4j.LoggerFactory;
    33
    34  /**
    35   * Tests {@link BufferedWriteFilter}.
    36   *
    37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    38   * @since MINA 2.0.0-M2
    39   */
    40  public class BufferedWriteFilterTest {
    41      static final Logger LOGGER = LoggerFactory.getLogger(BufferedWriteFilterTest.class);
    42
    43      @Test
    44      public void testNonExpandableBuffer() throws Exception {
    45          IoBuffer dest = IoBuffer.allocate(1);
    46          assertEquals(false, dest.isAutoExpand());
    47      }
    48
    49      @Test
    50      public void testBasicBuffering() {
    51          DummySession sess = new DummySession();
    52          sess.getFilterChain().addFirst("peer", new IoFilterAdapter() {
    53
    54              private int counter;
    55
    56              @Override
    57              public void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
    58                  LOGGER.debug("Filter closed !");
    59                  assertEquals(3, counter);
    60              }
    61
    62              @Override
    63              public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
    64                      throws Exception {
    65                  LOGGER.debug("New buffered message written !");
    66                  counter++;
    67                  try {
    68                      IoBuffer buf = (IoBuffer) writeRequest.getMessage();
    69                      if (counter == 3) {
    70                          assertEquals(1, buf.limit());
    71                          assertEquals(0, buf.get());
    72                      } else {
    73                          assertEquals(10, buf.limit());
    74                      }
    75                  } catch (Exception ex) {
    76                      throw new AssertionError("Wrong message type");
    77                  }
    78              }
    79
    80          });
    81          sess.getFilterChain().addFirst("logger", new LoggingFilter());
    82          BufferedWriteFilter bFilter = new BufferedWriteFilter(10);
    83          sess.getFilterChain().addLast("buffer", bFilter);
    84
    85          IoBuffer data = IoBuffer.allocate(1);
    86          for (byte i = 0; i < 20; i++) {
    87              data.put((byte) (0x30 + i));
    88              data.flip();
    89              sess.write(data);
    90              data.clear();
    91          }
    92
    93          // Add one more byte to overflow the final buffer
    94          data.put((byte) 0);
    95          data.flip();
    96          sess.write(data);
    97
    98          // Flush the final byte
    99          bFilter.flush(sess);
    100
    101         sess.close(true);
    102     }
    103 }