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; 019 020import static org.junit.Assert.assertEquals; 021 022import org.apache.hadoop.hbase.testclassification.MiscTests; 023import org.apache.hadoop.hbase.testclassification.SmallTests; 024import org.junit.ClassRule; 025import org.junit.Test; 026import org.junit.experimental.categories.Category; 027 028@Category({MiscTests.class, SmallTests.class}) 029public class TestCellBuilder { 030 031 @ClassRule 032 public static final HBaseClassTestRule CLASS_RULE = 033 HBaseClassTestRule.forClass(TestCellBuilder.class); 034 035 private static final byte OLD_DATA = 87; 036 private static final byte NEW_DATA = 100; 037 038 @Test 039 public void testCellBuilderWithDeepCopy() { 040 byte[] row = new byte[]{OLD_DATA}; 041 byte[] family = new byte[]{OLD_DATA}; 042 byte[] qualifier = new byte[]{OLD_DATA}; 043 byte[] value = new byte[]{OLD_DATA}; 044 Cell cell = CellBuilderFactory.create(CellBuilderType.DEEP_COPY) 045 .setRow(row) 046 .setFamily(family) 047 .setQualifier(qualifier) 048 .setType(Cell.Type.Put) 049 .setValue(value) 050 .build(); 051 row[0] = NEW_DATA; 052 family[0] = NEW_DATA; 053 qualifier[0] = NEW_DATA; 054 value[0] = NEW_DATA; 055 assertEquals(OLD_DATA, cell.getRowArray()[cell.getRowOffset()]); 056 assertEquals(OLD_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]); 057 assertEquals(OLD_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]); 058 assertEquals(OLD_DATA, cell.getValueArray()[cell.getValueOffset()]); 059 } 060 061 @Test 062 public void testCellBuilderWithShallowCopy() { 063 byte[] row = new byte[]{OLD_DATA}; 064 byte[] family = new byte[]{OLD_DATA}; 065 byte[] qualifier = new byte[]{OLD_DATA}; 066 byte[] value = new byte[]{OLD_DATA}; 067 Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY) 068 .setRow(row) 069 .setFamily(family) 070 .setQualifier(qualifier) 071 .setType(Cell.Type.Put) 072 .setValue(value) 073 .build(); 074 row[0] = NEW_DATA; 075 family[0] = NEW_DATA; 076 qualifier[0] = NEW_DATA; 077 value[0] = NEW_DATA; 078 assertEquals(NEW_DATA, cell.getRowArray()[cell.getRowOffset()]); 079 assertEquals(NEW_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]); 080 assertEquals(NEW_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]); 081 assertEquals(NEW_DATA, cell.getValueArray()[cell.getValueOffset()]); 082 } 083 084 @Test 085 public void testExtendedCellBuilderWithShallowCopy() { 086 byte[] row = new byte[]{OLD_DATA}; 087 byte[] family = new byte[]{OLD_DATA}; 088 byte[] qualifier = new byte[]{OLD_DATA}; 089 byte[] value = new byte[]{OLD_DATA}; 090 byte[] tags = new byte[]{OLD_DATA}; 091 long seqId = 999; 092 Cell cell = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY) 093 .setRow(row) 094 .setFamily(family) 095 .setQualifier(qualifier) 096 .setType(KeyValue.Type.Put.getCode()) 097 .setValue(value) 098 .setTags(tags) 099 .setSequenceId(seqId) 100 .build(); 101 row[0] = NEW_DATA; 102 family[0] = NEW_DATA; 103 qualifier[0] = NEW_DATA; 104 value[0] = NEW_DATA; 105 tags[0] = NEW_DATA; 106 assertEquals(NEW_DATA, cell.getRowArray()[cell.getRowOffset()]); 107 assertEquals(NEW_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]); 108 assertEquals(NEW_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]); 109 assertEquals(NEW_DATA, cell.getValueArray()[cell.getValueOffset()]); 110 assertEquals(NEW_DATA, cell.getTagsArray()[cell.getTagsOffset()]); 111 assertEquals(seqId, cell.getSequenceId()); 112 } 113 114 @Test 115 public void testExtendedCellBuilderWithDeepCopy() { 116 byte[] row = new byte[]{OLD_DATA}; 117 byte[] family = new byte[]{OLD_DATA}; 118 byte[] qualifier = new byte[]{OLD_DATA}; 119 byte[] value = new byte[]{OLD_DATA}; 120 byte[] tags = new byte[]{OLD_DATA}; 121 long seqId = 999; 122 Cell cell = ExtendedCellBuilderFactory.create(CellBuilderType.DEEP_COPY) 123 .setRow(row) 124 .setFamily(family) 125 .setQualifier(qualifier) 126 .setType(KeyValue.Type.Put.getCode()) 127 .setValue(value) 128 .setTags(tags) 129 .setSequenceId(seqId) 130 .build(); 131 row[0] = NEW_DATA; 132 family[0] = NEW_DATA; 133 qualifier[0] = NEW_DATA; 134 value[0] = NEW_DATA; 135 tags[0] = NEW_DATA; 136 assertEquals(OLD_DATA, cell.getRowArray()[cell.getRowOffset()]); 137 assertEquals(OLD_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]); 138 assertEquals(OLD_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]); 139 assertEquals(OLD_DATA, cell.getValueArray()[cell.getValueOffset()]); 140 assertEquals(OLD_DATA, cell.getTagsArray()[cell.getTagsOffset()]); 141 assertEquals(seqId, cell.getSequenceId()); 142 } 143}