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.assertTrue; 021 022import java.util.Arrays; 023import org.apache.hadoop.hbase.Cell; 024import org.apache.hadoop.hbase.CellBuilder; 025import org.apache.hadoop.hbase.CellUtil; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.testclassification.ClientTests; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Rule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.junit.rules.TestName; 040 041@Category({ MediumTests.class, ClientTests.class }) 042public class TestMutationGetCellBuilder { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestMutationGetCellBuilder.class); 047 048 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 049 050 @Rule 051 public TestName name = new TestName(); 052 053 @BeforeClass 054 public static void setUpBeforeClass() throws Exception { 055 TEST_UTIL.startMiniCluster(); 056 } 057 058 @AfterClass 059 public static void tearDownAfterClass() throws Exception { 060 TEST_UTIL.shutdownMiniCluster(); 061 } 062 063 @Test 064 public void testMutationGetCellBuilder() throws Exception { 065 final TableName tableName = TableName.valueOf(name.getMethodName()); 066 final byte[] rowKey = Bytes.toBytes("12345678"); 067 final byte[] uselessRowKey = Bytes.toBytes("123"); 068 final byte[] family = Bytes.toBytes("cf"); 069 final byte[] qualifier = Bytes.toBytes("foo"); 070 final long now = EnvironmentEdgeManager.currentTime(); 071 try (Table table = TEST_UTIL.createTable(tableName, family)) { 072 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000); 073 // put one row 074 Put put = new Put(rowKey); 075 CellBuilder cellBuilder = put.getCellBuilder().setQualifier(qualifier).setFamily(family) 076 .setValue(Bytes.toBytes("bar")).setTimestamp(now); 077 // setRow is useless 078 cellBuilder.setRow(uselessRowKey); 079 put.add(cellBuilder.build()); 080 byte[] cloneRow = CellUtil.cloneRow(cellBuilder.build()); 081 assertTrue("setRow must be useless", !Arrays.equals(cloneRow, uselessRowKey)); 082 table.put(put); 083 084 // get the row back and assert the values 085 Get get = new Get(rowKey); 086 get.setTimestamp(now); 087 Result result = table.get(get); 088 assertTrue("row key must be same", Arrays.equals(result.getRow(), rowKey)); 089 assertTrue("Column foo value should be bar", 090 Bytes.toString(result.getValue(family, qualifier)).equals("bar")); 091 092 // Delete that row 093 Delete delete = new Delete(rowKey); 094 cellBuilder = delete.getCellBuilder().setQualifier(qualifier).setFamily(family); 095 // if this row has been deleted,then can check setType is useless. 096 cellBuilder.setType(Cell.Type.Put); 097 delete.add(cellBuilder.build()); 098 table.delete(delete); 099 100 // check this row whether exist 101 get = new Get(rowKey); 102 get.setTimestamp(now); 103 result = table.get(get); 104 assertTrue("Column foo should not exist", result.getValue(family, qualifier) == null); 105 } 106 } 107}