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.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039
040/**
041 * Run Append tests that use the HBase clients;
042 */
043@Category(MediumTests.class)
044public class TestAppendFromClientSide {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestAppendFromClientSide.class);
049
050  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
051  private static byte[] ROW = Bytes.toBytes("testRow");
052  private static byte[] FAMILY = Bytes.toBytes("testFamily");
053  private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
054  @Rule
055  public TestName name = new TestName();
056
057  @BeforeClass
058  public static void beforeClass() throws Exception {
059    Configuration conf = TEST_UTIL.getConfiguration();
060    TEST_UTIL.startMiniCluster(3);
061  }
062
063  @AfterClass
064  public static void afterClass() throws Exception {
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testAppendWithCustomTimestamp() throws IOException {
070    TableName TABLENAME = TableName.valueOf(name.getMethodName());
071    Table table = TEST_UTIL.createTable(TABLENAME, FAMILY);
072    long timestamp = 999;
073    Append append = new Append(ROW);
074    append.add(CellUtil.createCell(ROW, FAMILY, QUALIFIER, timestamp, KeyValue.Type.Put.getCode(),
075      Bytes.toBytes(100L)));
076    Result r = table.append(append);
077    assertEquals(1, r.size());
078    assertEquals(timestamp, r.rawCells()[0].getTimestamp());
079    r = table.get(new Get(ROW));
080    assertEquals(1, r.size());
081    assertEquals(timestamp, r.rawCells()[0].getTimestamp());
082    r = table.append(append);
083    assertEquals(1, r.size());
084    assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
085    r = table.get(new Get(ROW));
086    assertEquals(1, r.size());
087    assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
088  }
089}