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 java.util.Arrays; 021import org.apache.hadoop.hbase.HBaseClassTestRule; 022import org.apache.hadoop.hbase.testclassification.ClientTests; 023import org.apache.hadoop.hbase.testclassification.SmallTests; 024import org.apache.hadoop.hbase.util.Bytes; 025import org.junit.Assert; 026import org.junit.ClassRule; 027import org.junit.Test; 028import org.junit.experimental.categories.Category; 029 030@Category({ ClientTests.class, SmallTests.class }) 031public class TestAttributes { 032 @ClassRule 033 public static final HBaseClassTestRule CLASS_RULE = 034 HBaseClassTestRule.forClass(TestAttributes.class); 035 036 private static final byte[] ROW = new byte[] { 'r' }; 037 038 @Test 039 public void testPutAttributes() { 040 Put put = new Put(ROW); 041 Assert.assertTrue(put.getAttributesMap().isEmpty()); 042 Assert.assertNull(put.getAttribute("absent")); 043 044 put.setAttribute("absent", null); 045 Assert.assertTrue(put.getAttributesMap().isEmpty()); 046 Assert.assertNull(put.getAttribute("absent")); 047 048 // adding attribute 049 put.setAttribute("attribute1", Bytes.toBytes("value1")); 050 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), put.getAttribute("attribute1"))); 051 Assert.assertEquals(1, put.getAttributesMap().size()); 052 Assert 053 .assertTrue(Arrays.equals(Bytes.toBytes("value1"), put.getAttributesMap().get("attribute1"))); 054 055 // overriding attribute value 056 put.setAttribute("attribute1", Bytes.toBytes("value12")); 057 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), put.getAttribute("attribute1"))); 058 Assert.assertEquals(1, put.getAttributesMap().size()); 059 Assert.assertTrue( 060 Arrays.equals(Bytes.toBytes("value12"), put.getAttributesMap().get("attribute1"))); 061 062 // adding another attribute 063 put.setAttribute("attribute2", Bytes.toBytes("value2")); 064 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), put.getAttribute("attribute2"))); 065 Assert.assertEquals(2, put.getAttributesMap().size()); 066 Assert 067 .assertTrue(Arrays.equals(Bytes.toBytes("value2"), put.getAttributesMap().get("attribute2"))); 068 069 // removing attribute 070 put.setAttribute("attribute2", null); 071 Assert.assertNull(put.getAttribute("attribute2")); 072 Assert.assertEquals(1, put.getAttributesMap().size()); 073 Assert.assertNull(put.getAttributesMap().get("attribute2")); 074 075 // removing non-existed attribute 076 put.setAttribute("attribute2", null); 077 Assert.assertNull(put.getAttribute("attribute2")); 078 Assert.assertEquals(1, put.getAttributesMap().size()); 079 Assert.assertNull(put.getAttributesMap().get("attribute2")); 080 081 // removing another attribute 082 put.setAttribute("attribute1", null); 083 Assert.assertNull(put.getAttribute("attribute1")); 084 Assert.assertTrue(put.getAttributesMap().isEmpty()); 085 Assert.assertNull(put.getAttributesMap().get("attribute1")); 086 } 087 088 @Test 089 public void testDeleteAttributes() { 090 Delete del = new Delete(new byte[] { 'r' }); 091 Assert.assertTrue(del.getAttributesMap().isEmpty()); 092 Assert.assertNull(del.getAttribute("absent")); 093 094 del.setAttribute("absent", null); 095 Assert.assertTrue(del.getAttributesMap().isEmpty()); 096 Assert.assertNull(del.getAttribute("absent")); 097 098 // adding attribute 099 del.setAttribute("attribute1", Bytes.toBytes("value1")); 100 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), del.getAttribute("attribute1"))); 101 Assert.assertEquals(1, del.getAttributesMap().size()); 102 Assert 103 .assertTrue(Arrays.equals(Bytes.toBytes("value1"), del.getAttributesMap().get("attribute1"))); 104 105 // overriding attribute value 106 del.setAttribute("attribute1", Bytes.toBytes("value12")); 107 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), del.getAttribute("attribute1"))); 108 Assert.assertEquals(1, del.getAttributesMap().size()); 109 Assert.assertTrue( 110 Arrays.equals(Bytes.toBytes("value12"), del.getAttributesMap().get("attribute1"))); 111 112 // adding another attribute 113 del.setAttribute("attribute2", Bytes.toBytes("value2")); 114 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), del.getAttribute("attribute2"))); 115 Assert.assertEquals(2, del.getAttributesMap().size()); 116 Assert 117 .assertTrue(Arrays.equals(Bytes.toBytes("value2"), del.getAttributesMap().get("attribute2"))); 118 119 // removing attribute 120 del.setAttribute("attribute2", null); 121 Assert.assertNull(del.getAttribute("attribute2")); 122 Assert.assertEquals(1, del.getAttributesMap().size()); 123 Assert.assertNull(del.getAttributesMap().get("attribute2")); 124 125 // removing non-existed attribute 126 del.setAttribute("attribute2", null); 127 Assert.assertNull(del.getAttribute("attribute2")); 128 Assert.assertEquals(1, del.getAttributesMap().size()); 129 Assert.assertNull(del.getAttributesMap().get("attribute2")); 130 131 // removing another attribute 132 del.setAttribute("attribute1", null); 133 Assert.assertNull(del.getAttribute("attribute1")); 134 Assert.assertTrue(del.getAttributesMap().isEmpty()); 135 Assert.assertNull(del.getAttributesMap().get("attribute1")); 136 } 137 138 @Test 139 public void testGetId() { 140 Get get = new Get(ROW); 141 Assert.assertNull("Make sure id is null if unset", get.toMap().get("id")); 142 get.setId("myId"); 143 Assert.assertEquals("myId", get.toMap().get("id")); 144 } 145 146 @Test 147 public void testAppendId() { 148 Append append = new Append(ROW); 149 Assert.assertNull("Make sure id is null if unset", append.toMap().get("id")); 150 append.setId("myId"); 151 Assert.assertEquals("myId", append.toMap().get("id")); 152 } 153 154 @Test 155 public void testDeleteId() { 156 Delete delete = new Delete(ROW); 157 Assert.assertNull("Make sure id is null if unset", delete.toMap().get("id")); 158 delete.setId("myId"); 159 Assert.assertEquals("myId", delete.toMap().get("id")); 160 } 161 162 @Test 163 public void testPutId() { 164 Put put = new Put(ROW); 165 Assert.assertNull("Make sure id is null if unset", put.toMap().get("id")); 166 put.setId("myId"); 167 Assert.assertEquals("myId", put.toMap().get("id")); 168 } 169 170 @Test 171 public void testScanId() { 172 Scan scan = new Scan(); 173 Assert.assertNull("Make sure id is null if unset", scan.toMap().get("id")); 174 scan.setId("myId"); 175 Assert.assertEquals("myId", scan.toMap().get("id")); 176 } 177}