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.client;
019
020import static org.junit.jupiter.api.Assertions.assertArrayEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.junit.jupiter.api.AfterAll;
032import org.junit.jupiter.api.BeforeAll;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035
036/**
037 * Testcase for HBASE-21032, where use the wrong readType from a Scan instance which is actually a
038 * get scan and cause returning only 1 cell per rpc call.
039 */
040@Tag(ClientTests.TAG)
041@Tag(MediumTests.TAG)
042public class TestGetScanColumnsWithNewVersionBehavior {
043
044  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045  private static final TableName TABLE = TableName.valueOf("table");
046  private static final byte[] CF = { 'c', 'f' };
047  private static final byte[] ROW = { 'r', 'o', 'w' };
048  private static final byte[] COLA = { 'a' };
049  private static final byte[] COLB = { 'b' };
050  private static final byte[] COLC = { 'c' };
051  private static final long TS = 42;
052
053  @BeforeAll
054  public static void setUp() throws Exception {
055    TEST_UTIL.startMiniCluster(1);
056    ColumnFamilyDescriptor cd =
057      ColumnFamilyDescriptorBuilder.newBuilder(CF).setNewVersionBehavior(true).build();
058    TEST_UTIL.createTable(TableDescriptorBuilder.newBuilder(TABLE).setColumnFamily(cd).build(),
059      null);
060  }
061
062  @AfterAll
063  public static void tearDown() throws Exception {
064    TEST_UTIL.shutdownMiniCluster();
065  }
066
067  @Test
068  public void test() throws IOException {
069    try (Table t = TEST_UTIL.getConnection().getTable(TABLE)) {
070      Cell[] expected = new Cell[2];
071      expected[0] = new KeyValue(ROW, CF, COLA, TS, COLA);
072      expected[1] = new KeyValue(ROW, CF, COLC, TS, COLC);
073
074      Put p = new Put(ROW);
075      p.addColumn(CF, COLA, TS, COLA);
076      p.addColumn(CF, COLB, TS, COLB);
077      p.addColumn(CF, COLC, TS, COLC);
078      t.put(p);
079
080      // check get request
081      Get get = new Get(ROW);
082      get.addColumn(CF, COLA);
083      get.addColumn(CF, COLC);
084      Result getResult = t.get(get);
085      assertArrayEquals(expected, getResult.rawCells());
086
087      // check scan request
088      Scan scan = new Scan().withStartRow(ROW);
089      scan.addColumn(CF, COLA);
090      scan.addColumn(CF, COLC);
091      ResultScanner scanner = t.getScanner(scan);
092      List scanResult = new ArrayList<Cell>();
093      for (Result result = scanner.next(); (result != null); result = scanner.next()) {
094        scanResult.addAll(result.listCells());
095      }
096      assertArrayEquals(expected, scanResult.toArray(new Cell[scanResult.size()]));
097    }
098  }
099}