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.util;
    20
    21  import static org.junit.Assert.assertEquals;
    22
    23  import java.nio.ByteBuffer;
    24
    25  import org.junit.Test;
    26
    27  /**
    28   *
    29   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    30   */
    31  public class ByteBufferDumperTest {
    32
    33      @Test
    34      public void stringTest() {
    35          String toTest = "yopYOP\n\r";
    36          byte[] charData = toTest.getBytes();
    37          assertEquals(toTest.length(), charData.length);
    38
    39          ByteBuffer myBuffer = ByteBuffer.allocate(toTest.length());
    40          for (int i = 0; i < toTest.length(); i++) {
    41              myBuffer.put(charData[i]);
    42          }
    43          myBuffer.flip();
    44
    45          int remaining = myBuffer.remaining();
    46          int pos = myBuffer.position();
    47          String dump = ByteBufferDumper.dump(myBuffer);
    48          assertEquals("ByteBuffer[len=8,str='" + toTest + "']", dump);
    49          assertEquals(remaining, myBuffer.remaining());
    50          assertEquals(pos, myBuffer.position());
    51      }
    52
    53      @Test
    54      public void binaryTest() {
    55          ByteBuffer myBuffer = ByteBuffer.allocate(4);
    56          myBuffer.put((byte) 0x88);
    57          myBuffer.put((byte) 0x03);
    58          myBuffer.put((byte) 0xFF);
    59          myBuffer.flip();
    60
    61          int remaining = myBuffer.remaining();
    62          int pos = myBuffer.position();
    63          String dump = ByteBufferDumper.dump(myBuffer);
    64          System.err.println(dump);
    65          assertEquals("ByteBuffer[len=3,bytes='0x88 0x03 0xFF']", dump);
    66          assertEquals(remaining, myBuffer.remaining());
    67          assertEquals(pos, myBuffer.position());
    68      }
    69
    70      @Test
    71      public void testWithSizeLimit() {
    72          ByteBuffer bb = ByteBuffer.allocate(10);
    73          bb.put(new byte[] { 0x01, (byte) 0x8F, 0x04, 0x7A, (byte) 0xc2, 0x23, (byte) 0xA0, 0x08, 0x44 });
    74          bb.flip();
    75
    76          assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']", ByteBufferDumper.dump(bb, 5, false));
    77          assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']", ByteBufferDumper.dump(bb, 5, true));
    78          assertEquals("ByteBuffer[len=9,str='']", ByteBufferDumper.dump(bb, 0, true));
    79          assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23 0xA0 0x08 0x44']",
    80                  ByteBufferDumper.dump(bb, 10, true));
    81          assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23 0xA0 0x08 0x44']",
    82                  ByteBufferDumper.dump(bb, -1, false));
    83      }
    84
    85      @Test
    86      public void toHex() {
    87          ByteBuffer bb = ByteBuffer.allocate(4);
    88          bb.put((byte) 0);
    89          bb.put((byte) 1);
    90          bb.put((byte) 2);
    91          bb.put((byte) 254);
    92          bb.flip();
    93          assertEquals("000102FE", ByteBufferDumper.toHex(bb));
    94      }
    95  }