View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Set;
25  
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.Assert;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  // TODO: cover more test cases
35  @Category(SmallTests.class)
36  public class TestScan {
37    @Test
38    public void testAttributesSerialization() throws IOException {
39      Scan scan = new Scan();
40      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
41      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
42      scan.setAttribute("attribute3", Bytes.toBytes("value3"));
43  
44      ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
45  
46      Scan scan2 = ProtobufUtil.toScan(scanProto);
47  
48      Assert.assertNull(scan2.getAttribute("absent"));
49      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
50      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
51      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
52      Assert.assertEquals(3, scan2.getAttributesMap().size());
53    }
54  
55    @Test
56    public void testScanAttributes() {
57      Scan scan = new Scan();
58      Assert.assertTrue(scan.getAttributesMap().isEmpty());
59      Assert.assertNull(scan.getAttribute("absent"));
60  
61      scan.setAttribute("absent", null);
62      Assert.assertTrue(scan.getAttributesMap().isEmpty());
63      Assert.assertNull(scan.getAttribute("absent"));
64  
65      // adding attribute
66      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
67      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
68      Assert.assertEquals(1, scan.getAttributesMap().size());
69      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
70  
71      // overriding attribute value
72      scan.setAttribute("attribute1", Bytes.toBytes("value12"));
73      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
74      Assert.assertEquals(1, scan.getAttributesMap().size());
75      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
76  
77      // adding another attribute
78      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
79      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
80      Assert.assertEquals(2, scan.getAttributesMap().size());
81      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
82  
83      // removing attribute
84      scan.setAttribute("attribute2", null);
85      Assert.assertNull(scan.getAttribute("attribute2"));
86      Assert.assertEquals(1, scan.getAttributesMap().size());
87      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
88  
89      // removing non-existed attribute
90      scan.setAttribute("attribute2", null);
91      Assert.assertNull(scan.getAttribute("attribute2"));
92      Assert.assertEquals(1, scan.getAttributesMap().size());
93      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
94  
95      // removing another attribute
96      scan.setAttribute("attribute1", null);
97      Assert.assertNull(scan.getAttribute("attribute1"));
98      Assert.assertTrue(scan.getAttributesMap().isEmpty());
99      Assert.assertNull(scan.getAttributesMap().get("attribute1"));
100   }
101 
102   @Test
103   public void testNullQualifier() {
104     Scan scan = new Scan();
105     byte[] family = Bytes.toBytes("family");
106     scan.addColumn(family, null);
107     Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
108     Assert.assertEquals(1, qualifiers.size());
109   }
110 }
111