001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.nio.ByteBuffer;
028import java.nio.channels.FileChannel;
029import org.apache.hadoop.hbase.testclassification.RPCTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.jupiter.api.AfterEach;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037import org.apache.hbase.thirdparty.com.google.common.base.Charsets;
038import org.apache.hbase.thirdparty.com.google.common.io.Files;
039
040@Tag(RPCTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestBufferChain {
043
044  private File tmpFile;
045
046  private static final byte[][] HELLO_WORLD_CHUNKS =
047    new byte[][] { "hello".getBytes(Charsets.UTF_8), " ".getBytes(Charsets.UTF_8),
048      "world".getBytes(Charsets.UTF_8) };
049
050  @BeforeEach
051  public void setup() throws IOException {
052    tmpFile = File.createTempFile("TestBufferChain", "txt");
053  }
054
055  @AfterEach
056  public void teardown() {
057    tmpFile.delete();
058  }
059
060  @Test
061  public void testGetBackBytesWePutIn() {
062    ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
063    BufferChain chain = new BufferChain(bufs);
064    assertTrue(Bytes.equals(Bytes.toBytes("hello world"), chain.getBytes()));
065  }
066
067  @Test
068  public void testLimitOffset() throws IOException {
069    ByteBuffer[] bufs = new ByteBuffer[] { stringBuf("XXXhelloYYY", 3, 5), stringBuf(" ", 0, 1),
070      stringBuf("XXXXworldY", 4, 5) };
071    BufferChain chain = new BufferChain(bufs);
072    writeAndVerify(chain, "hello world");
073    assertNoRemaining(bufs);
074  }
075
076  private ByteBuffer stringBuf(String string, int position, int length) {
077    ByteBuffer buf = ByteBuffer.wrap(string.getBytes(Charsets.UTF_8));
078    buf.position(position);
079    buf.limit(position + length);
080    assertTrue(buf.hasRemaining());
081    return buf;
082  }
083
084  private void assertNoRemaining(ByteBuffer[] bufs) {
085    for (ByteBuffer buf : bufs) {
086      assertFalse(buf.hasRemaining());
087    }
088  }
089
090  private ByteBuffer[] wrapArrays(byte[][] arrays) {
091    ByteBuffer[] ret = new ByteBuffer[arrays.length];
092    for (int i = 0; i < arrays.length; i++) {
093      ret[i] = ByteBuffer.wrap(arrays[i]);
094    }
095    return ret;
096  }
097
098  private void writeAndVerify(BufferChain chain, String string) throws IOException {
099    FileOutputStream fos = new FileOutputStream(tmpFile);
100    FileChannel ch = fos.getChannel();
101    try {
102      long remaining = string.length();
103      while (chain.hasRemaining()) {
104        long n = chain.write(ch);
105        remaining -= n;
106      }
107      assertEquals(0, remaining);
108    } finally {
109      fos.close();
110    }
111    assertFalse(chain.hasRemaining());
112    assertEquals(string, Files.toString(tmpFile, Charsets.UTF_8));
113  }
114}