Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2014 The gRPC Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.grpc.internal;
     18 
     19 import static com.google.common.base.Charsets.UTF_8;
     20 import static io.grpc.internal.ReadableBuffers.wrap;
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertSame;
     23 import static org.junit.Assert.assertTrue;
     24 
     25 import org.junit.Test;
     26 
     27 /**
     28  * Tests for the array-backed {@link ReadableBuffer} returned by {@link ReadableBuffers#wrap(byte[],
     29  * int, int)}.
     30  */
     31 public class ReadableBuffersArrayTest extends ReadableBufferTestBase {
     32 
     33   @Test
     34   public void bufferShouldExposeArray() {
     35     byte[] array = msg.getBytes(UTF_8);
     36     ReadableBuffer buffer = wrap(array, 1, msg.length() - 1);
     37     assertTrue(buffer.hasArray());
     38     assertSame(array, buffer.array());
     39     assertEquals(1, buffer.arrayOffset());
     40 
     41     // Now read a byte and verify that the offset changes.
     42     buffer.readUnsignedByte();
     43     assertEquals(2, buffer.arrayOffset());
     44   }
     45 
     46   @Override
     47   protected ReadableBuffer buffer() {
     48     return ReadableBuffers.wrap(msg.getBytes(UTF_8), 0, msg.length());
     49   }
     50 }
     51