HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestMutationGetCellBuilder.java
blobdd6485473cfeac3c459a25d5658e56c2db960f15
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.junit.Assert.assertTrue;
22 import java.util.Arrays;
23 import org.apache.hadoop.hbase.Cell;
24 import org.apache.hadoop.hbase.CellBuilder;
25 import org.apache.hadoop.hbase.CellUtil;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.testclassification.ClientTests;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.ClassRule;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.junit.rules.TestName;
40 @Category({MediumTests.class, ClientTests.class})
41 public class TestMutationGetCellBuilder {
43 @ClassRule
44 public static final HBaseClassTestRule CLASS_RULE =
45 HBaseClassTestRule.forClass(TestMutationGetCellBuilder.class);
47 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
49 @Rule
50 public TestName name = new TestName();
52 @BeforeClass
53 public static void setUpBeforeClass() throws Exception {
54 TEST_UTIL.startMiniCluster();
57 @AfterClass
58 public static void tearDownAfterClass() throws Exception {
59 TEST_UTIL.shutdownMiniCluster();
62 @Test
63 public void testMutationGetCellBuilder() throws Exception {
64 final TableName tableName = TableName.valueOf(name.getMethodName());
65 final byte[] rowKey = Bytes.toBytes("12345678");
66 final byte[] uselessRowKey = Bytes.toBytes("123");
67 final byte[] family = Bytes.toBytes("cf");
68 final byte[] qualifier = Bytes.toBytes("foo");
69 final long now = System.currentTimeMillis();
70 try (Table table = TEST_UTIL.createTable(tableName, family)) {
71 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
72 // put one row
73 Put put = new Put(rowKey);
74 CellBuilder cellBuilder = put.getCellBuilder().setQualifier(qualifier)
75 .setFamily(family).setValue(Bytes.toBytes("bar")).setTimestamp(now);
76 //setRow is useless
77 cellBuilder.setRow(uselessRowKey);
78 put.add(cellBuilder.build());
79 byte[] cloneRow = CellUtil.cloneRow(cellBuilder.build());
80 assertTrue("setRow must be useless", !Arrays.equals(cloneRow, uselessRowKey));
81 table.put(put);
83 // get the row back and assert the values
84 Get get = new Get(rowKey);
85 get.setTimestamp(now);
86 Result result = table.get(get);
87 assertTrue("row key must be same", Arrays.equals(result.getRow(), rowKey));
88 assertTrue("Column foo value should be bar",
89 Bytes.toString(result.getValue(family, qualifier)).equals("bar"));
91 //Delete that row
92 Delete delete = new Delete(rowKey);
93 cellBuilder = delete.getCellBuilder().setQualifier(qualifier)
94 .setFamily(family);
95 //if this row has been deleted,then can check setType is useless.
96 cellBuilder.setType(Cell.Type.Put);
97 delete.add(cellBuilder.build());
98 table.delete(delete);
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",
105 result.getValue(family, qualifier) == null);