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