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 static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.Set;
27  
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
31  import org.apache.hadoop.hbase.security.visibility.Authorizations;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  // TODO: cover more test cases
38  @Category(SmallTests.class)
39  public class TestScan {
40    @Test
41    public void testAttributesSerialization() throws IOException {
42      Scan scan = new Scan();
43      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
44      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
45      scan.setAttribute("attribute3", Bytes.toBytes("value3"));
46  
47      ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
48  
49      Scan scan2 = ProtobufUtil.toScan(scanProto);
50  
51      Assert.assertNull(scan2.getAttribute("absent"));
52      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
53      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
54      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
55      Assert.assertEquals(3, scan2.getAttributesMap().size());
56    }
57  
58    @Test
59    public void testScanAttributes() {
60      Scan scan = new Scan();
61      Assert.assertTrue(scan.getAttributesMap().isEmpty());
62      Assert.assertNull(scan.getAttribute("absent"));
63  
64      scan.setAttribute("absent", null);
65      Assert.assertTrue(scan.getAttributesMap().isEmpty());
66      Assert.assertNull(scan.getAttribute("absent"));
67  
68      // adding attribute
69      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
70      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
71      Assert.assertEquals(1, scan.getAttributesMap().size());
72      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
73  
74      // overriding attribute value
75      scan.setAttribute("attribute1", Bytes.toBytes("value12"));
76      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
77      Assert.assertEquals(1, scan.getAttributesMap().size());
78      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
79  
80      // adding another attribute
81      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
82      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
83      Assert.assertEquals(2, scan.getAttributesMap().size());
84      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
85  
86      // removing attribute
87      scan.setAttribute("attribute2", null);
88      Assert.assertNull(scan.getAttribute("attribute2"));
89      Assert.assertEquals(1, scan.getAttributesMap().size());
90      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
91  
92      // removing non-existed attribute
93      scan.setAttribute("attribute2", null);
94      Assert.assertNull(scan.getAttribute("attribute2"));
95      Assert.assertEquals(1, scan.getAttributesMap().size());
96      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
97  
98      // removing another attribute
99      scan.setAttribute("attribute1", null);
100     Assert.assertNull(scan.getAttribute("attribute1"));
101     Assert.assertTrue(scan.getAttributesMap().isEmpty());
102     Assert.assertNull(scan.getAttributesMap().get("attribute1"));
103   }
104 
105   @Test
106   public void testNullQualifier() {
107     Scan scan = new Scan();
108     byte[] family = Bytes.toBytes("family");
109     scan.addColumn(family, null);
110     Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
111     Assert.assertEquals(1, qualifiers.size());
112   }
113 
114   @Test
115   public void testSetAuthorizations() {
116     Scan scan = new Scan();
117     scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
118     try {
119       scan.setAuthorizations(new Authorizations("A|B"));
120       fail("Should have failed for A|B.");
121     } catch (IllegalArgumentException e) {
122     }
123     try {
124       scan.setAuthorizations(new Authorizations("A&B"));
125       fail("Should have failed for A&B.");
126     } catch (IllegalArgumentException e) {
127     }
128     try {
129       scan.setAuthorizations(new Authorizations("!B"));
130       fail("Should have failed for !B.");
131     } catch (IllegalArgumentException e) {
132     }
133     try {
134       scan.setAuthorizations(new Authorizations("A", "(A)"));
135       fail("Should have failed for (A).");
136     } catch (IllegalArgumentException e) {
137     }
138     try {
139       scan.setAuthorizations(new Authorizations("A", "{A"));
140       fail("Should have failed for {A.");
141     } catch (IllegalArgumentException e) {
142     }
143     try {
144       scan.setAuthorizations(new Authorizations(" "));
145       fail("Should have failed for empty");
146     } catch (IllegalArgumentException e) {
147     }
148     try {
149       scan.setAuthorizations(new Authorizations(":B"));
150     } catch (IllegalArgumentException e) {
151       fail("Should not have failed for :B");
152     }
153     try {
154       scan.setAuthorizations(new Authorizations("-B"));
155     } catch (IllegalArgumentException e) {
156       fail("Should not have failed for -B");
157     }
158     try {
159       scan.setAuthorizations(new Authorizations(".B"));
160     } catch (IllegalArgumentException e) {
161       fail("Should not have failed for .B");
162     }
163     try {
164       scan.setAuthorizations(new Authorizations("/B"));
165     } catch (IllegalArgumentException e) {
166       fail("Should not have failed for /B");
167     }
168   }
169 }
170