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.codec.textline;
    21
    22  import static org.junit.Assert.*;
    23
    24  import java.nio.ByteBuffer;
    25
    26  import org.apache.mina.codec.textline.TextLineDecoder.Context;
    27  import org.junit.Test;
    28
    29  /**
    30   * A {@link TextLineDecoder} test.
    31   *
    32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    33   */
    34  public class AutoTextLineDecoderTest {
    35
    36      @Test
    37      public void testThatEmptyBufferReturnsEmptyResult() {
    38          TextLineDecoder decoder = new TextLineDecoder();
    39          Context context = decoder.createDecoderState();
    40          String results = decoder.decode(ByteBuffer.allocate(0), context);
    41          assertNull(results);
    42      }
    43
    44      @Test
    45      public void testThatNonLineTerminatedStringReturnsEmptyResult() {
    46          TextLineDecoder decoder = new TextLineDecoder();
    47          Context context = decoder.createDecoderState();
    48          String results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
    49          assertNull(results);
    50          assertEquals(8, context.getBuffer().position());
    51      }
    52
    53      @Test
    54      public void testThatUnixLineTerminatedStringReturnsNonEmptyResult() {
    55          TextLineDecoder decoder = new TextLineDecoder();
    56          Context context = decoder.createDecoderState();
    57          String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
    58          assertEquals("a string", results);
    59          assertEquals(0, context.getBuffer().position());
    60      }
    61
    62      @Test
    63      public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
    64          TextLineDecoder decoder = new TextLineDecoder();
    65          Context context = decoder.createDecoderState();
    66          String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
    67          assertNotNull(results);
    68          assertEquals("a string", results);
    69          assertEquals(0, context.getBuffer().position());
    70      }
    71
    72      @Test
    73      public void testThatContextIsMaintainedBetweenMessages() {
    74          TextLineDecoder decoder = new TextLineDecoder();
    75          Context context = decoder.createDecoderState();
    76          String results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
    77          assertNotNull(results);
    78          assertEquals("a string", results);
    79          assertEquals(1, context.getBuffer().position());
    80          results = decoder.decode(ByteBuffer.wrap(" string\n".getBytes()), context);
    81          assertNotNull(results);
    82          assertEquals("a string", results);
    83          assertEquals(0, context.getBuffer().position());
    84      }
    85
    86      @Test
    87      public void testThatUnixLineTerminatedLongStringReturnsNonEmptyResult() {
    88          TextLineDecoder decoder = new TextLineDecoder();
    89          Context context = decoder.createDecoderState();
    90          StringBuffer sb = new StringBuffer();
    91          for (int i = 0; i < 100; ++i) {
    92              sb.append("a string");
    93          }
    94          String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
    95          assertNotNull(results);
    96          assertEquals(sb.toString(), results);
    97          assertEquals(0, context.getBuffer().position());
    98      }
    99
    100     @Test
    101     public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
    102         TextLineDecoder decoder = new TextLineDecoder();
    103         Context context = decoder.createDecoderState();
    104         StringBuffer sb = new StringBuffer();
    105         for (int i = 0; i < 100; ++i) {
    106             sb.append("a string");
    107         }
    108         String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
    109         assertNotNull(results);
    110         assertEquals(sb.toString(), results);
    111         assertEquals(0, context.getBuffer().position());
    112     }
    113 }