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 static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertNull;
    24  import static org.junit.Assert.fail;
    25
    26  import java.nio.ByteBuffer;
    27  import java.util.Map;
    28
    29  import org.apache.mina.codec.IoBuffer;
    30  import org.apache.mina.codec.ProtocolDecoderException;
    31  import org.apache.mina.codec.delimited.ByteBufferEncoder;
    32  import org.apache.mina.codec.delimited.IoBufferDecoder;
    33  import org.junit.Before;
    34  import org.junit.Test;
    35
    36  /**
    37   * A generic test class for {@link ByteBufferEncoder} and {@link IoBufferDecoder}
    38   *
    39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    40   */
    41  public abstract class IntEncodingTest {
    42
    43      protected ByteBufferEncoder<Integer> encoder;
    44
    45      protected IoBufferDecoder<Integer> decoder;
    46
    47      public abstract IoBufferDecoder<Integer> newDecoderInstance();
    48
    49      public abstract ByteBufferEncoder<Integer> newEncoderInstance();
    50
    51      public abstract Map<Integer, ByteBuffer> getEncodingSamples();
    52
    53      public abstract Iterable<ByteBuffer> getIllegalBuffers();
    54
    55      @Before
    56      public void prepareDecoder() {
    57          decoder = newDecoderInstance();
    58          encoder = newEncoderInstance();
    59      }
    60
    61      @Test
    62      public void testTruncatedValues() {
    63          for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
    64
    65              IoBuffer buffer = IoBuffer.wrap(encoder.encode(value));
    66
    67              for (int i = 0; i < buffer.remaining(); i++) {
    68                  IoBuffer partialBuffer = buffer.slice();
    69                  partialBuffer.limit(partialBuffer.position() + i);
    70                  try {
    71                      assertNull(decoder.decode(partialBuffer));
    72                  } catch (ProtocolDecoderException e) {
    73                      fail("Should not throw exception");
    74                  }
    75              }
    76          }
    77      }
    78
    79      @Test
    80      public void testSizedValues() {
    81          for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
    82              ByteBuffer buffer = encoder.encode(value);
    83
    84              try {
    85                  assertEquals(value, decoder.decode(IoBuffer.wrap(buffer)).intValue());
    86              } catch (ProtocolDecoderException e) {
    87                  fail("Should not throw exception");
    88              }
    89          }
    90      }
    91
    92      @Test
    93      public void testExtendedValues() {
    94          for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
    95
    96              ByteBuffer buffer = encoder.encode(value);
    97
    98              for (int i = 1; i < 5; i++) {
    99                  int size = buffer.remaining() + i;
    100                 IoBuffer extendedBuffer = IoBuffer.wrap(ByteBuffer.allocate(size));
    101                 int start = extendedBuffer.position();
    102                 extendedBuffer.put(buffer.slice());
    103                 extendedBuffer.position(start);
    104                 extendedBuffer.limit(start + size);
    105
    106                 try {
    107                     decoder.decode(extendedBuffer);
    108                     assertEquals(i, extendedBuffer.remaining());
    109                 } catch (ProtocolDecoderException e) {
    110                     fail("Should not throw exception");
    111                 }
    112             }
    113         }
    114     }
    115
    116     @Test
    117     public void testSamples() {
    118         Map<Integer, ByteBuffer> samples = getEncodingSamples();
    119         for (Integer val : samples.keySet()) {
    120             assertEquals(samples.get(val), encoder.encode(val));
    121             try {
    122                 assertEquals(val, decoder.decode(IoBuffer.wrap(samples.get(val))));
    123             } catch (ProtocolDecoderException e) {
    124                 fail("Should not throw exception");
    125             }
    126         }
    127     }
    128
    129     @Test
    130     public void testOverflow() {
    131
    132         for (ByteBuffer buffer : getIllegalBuffers()) {
    133             try {
    134                 decoder.decode(IoBuffer.wrap(buffer));
    135                 fail("Should throw an overflow exception");
    136             } catch (ProtocolDecoderException e) {
    137                 // fine
    138             }
    139         }
    140     }
    141
    142 //    @Test
    143 //    public void testNegativeValues() {
    144 //        ByteBuffer zero = encoder.encode(0);
    145 //        for (int i : new int[] { -1, -127, Integer.MIN_VALUE }) {
    146 //            assertEquals(zero, encoder.encode(i));
    147 //        }
    148 //    }
    149 }