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