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 UnixTextLineDecoderTest {
    35
    36      @Test
    37      public void testThatEmptyBufferReturnsEmptyResult() {
    38          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
    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(LineDelimiter.UNIX);
    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(LineDelimiter.UNIX);
    56          Context context = decoder.createDecoderState();
    57          String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
    58          assertNotNull(results);
    59          assertEquals("a string", results);
    60          assertEquals(0, context.getBuffer().position());
    61      }
    62
    63      @Test
    64      public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
    65          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
    66          Context context = decoder.createDecoderState();
    67          String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
    68          assertNotNull(results);
    69          assertEquals("a string\r", results);
    70          assertEquals(0, context.getBuffer().position());
    71      }
    72
    73      @Test
    74      public void testThatContextIsMaintainedBetweenMessages() {
    75          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
    76          Context context = decoder.createDecoderState();
    77          String results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
    78          assertNotNull(results);
    79          assertEquals("a string", results);
    80          assertEquals(1, context.getBuffer().position());
    81          results = decoder.decode(ByteBuffer.wrap(" string\n".getBytes()), context);
    82          assertNotNull(results);
    83          assertEquals("a string", results);
    84          assertEquals(0, context.getBuffer().position());
    85      }
    86
    87      @Test
    88      public void testThatUnixLineTerminatedLongStringReturnsNonEmptyResult() {
    89          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
    90          Context context = decoder.createDecoderState();
    91          StringBuffer sb = new StringBuffer();
    92          for (int i = 0; i < 100; ++i) {
    93              sb.append("a string");
    94          }
    95          String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
    96          assertNotNull(results);
    97          assertEquals(sb.toString(), results);
    98          assertEquals(0, context.getBuffer().position());
    99      }
    100
    101     @Test
    102     public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
    103         TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
    104         Context context = decoder.createDecoderState();
    105         StringBuffer sb = new StringBuffer();
    106         for (int i = 0; i < 100; ++i) {
    107             sb.append("a string");
    108         }
    109         String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
    110         assertNotNull(results);
    111         assertEquals(sb.toString() + "\r", results);
    112         assertEquals(0, context.getBuffer().position());
    113     }
    114 }