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