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.delimited.ints;
    21
    22  import java.nio.ByteBuffer;
    23  import java.util.HashMap;
    24  import java.util.LinkedList;
    25  import java.util.List;
    26  import java.util.Map;
    27
    28  import org.apache.mina.codec.delimited.IoBufferDecoder;
    29  import org.apache.mina.codec.delimited.ByteBufferEncoder;
    30
    31  /**
    32   * A {@link VarInt.Encoder} and {@link VarInt.Decoder} test.
    33   *
    34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    35   */
    36  public class VarIntEncodingTest extends IntEncodingTest {
    37
    38      @Override
    39      public IoBufferDecoder<Integer> newDecoderInstance() {
    40          return new VarInt().getDecoder();
    41      }
    42
    43      @Override
    44      public ByteBufferEncoder<Integer> newEncoderInstance() {
    45          return new VarInt().getEncoder();
    46      }
    47
    48      @Override
    49      public Map<Integer, ByteBuffer> getEncodingSamples() {
    50          Map<Integer, ByteBuffer> map = new HashMap<Integer, ByteBuffer>();
    51
    52          map.put(0, ByteBuffer.wrap(new byte[] { 0 }));
    53          map.put(1 | 2 << 7 | 3 << 14 | 4 << 21 | 5 << 28,
    54                  ByteBuffer.wrap(new byte[] { 1 | (byte) 0x80, 2 | (byte) 0x80, 3 | (byte) 0x80, 4 | (byte) 0x80, 5 }));
    55          return map;
    56      }
    57
    58      @Override
    59      public Iterable<ByteBuffer> getIllegalBuffers() {
    60          List<ByteBuffer> list = new LinkedList<ByteBuffer>();
    61          list.add(ByteBuffer.wrap(new byte[] { 0 | (byte) 0x80, 0 | (byte) 0x80, 0 | (byte) 0x80, 0 | (byte) 0x80,
    62                  1 << 4 }));
    63          return list;
    64      }
    65
    66  }