View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.client;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.io.IOException;
26  import java.util.Arrays;
27  import java.util.Set;
28  
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.filter.FilterList;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
33  import org.apache.hadoop.hbase.security.visibility.Authorizations;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.junit.Assert;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  // TODO: cover more test cases
41  @Category(SmallTests.class)
42  public class TestScan {
43    @Test
44    public void testAttributesSerialization() throws IOException {
45      Scan scan = new Scan();
46      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
47      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
48      scan.setAttribute("attribute3", Bytes.toBytes("value3"));
49  
50      ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
51  
52      Scan scan2 = ProtobufUtil.toScan(scanProto);
53  
54      Assert.assertNull(scan2.getAttribute("absent"));
55      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
56      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
57      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
58      Assert.assertEquals(3, scan2.getAttributesMap().size());
59    }
60  
61    @Test
62    public void testGetToScan() throws IOException {
63      Get get = new Get(Bytes.toBytes(1));
64      get.setCacheBlocks(true).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
65          .setId("get").setIsolationLevel(IsolationLevel.READ_COMMITTED)
66          .setLoadColumnFamiliesOnDemand(false).setMaxResultsPerColumnFamily(1000)
67          .setMaxVersions(9999).setRowOffsetPerColumnFamily(5).setTimeRange(0, 13)
68          .setAttribute("att_v0", Bytes.toBytes("att_v0"))
69          .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123);
70      Scan scan = new Scan(get);
71      assertEquals(get.getCacheBlocks(), scan.getCacheBlocks());
72      assertEquals(get.getConsistency(), scan.getConsistency());
73      assertEquals(get.getFilter(), scan.getFilter());
74      assertEquals(get.getId(), scan.getId());
75      assertEquals(get.getIsolationLevel(), scan.getIsolationLevel());
76      assertEquals(get.getLoadColumnFamiliesOnDemandValue(),
77          scan.getLoadColumnFamiliesOnDemandValue());
78      assertEquals(get.getMaxResultsPerColumnFamily(), scan.getMaxResultsPerColumnFamily());
79      assertEquals(get.getMaxVersions(), scan.getMaxVersions());
80      assertEquals(get.getRowOffsetPerColumnFamily(), scan.getRowOffsetPerColumnFamily());
81      assertEquals(get.getTimeRange().getMin(), scan.getTimeRange().getMin());
82      assertEquals(get.getTimeRange().getMax(), scan.getTimeRange().getMax());
83      assertTrue(Bytes.equals(get.getAttribute("att_v0"), scan.getAttribute("att_v0")));
84      assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin(),
85        scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin());
86      assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax(),
87        scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax());
88    }
89  
90    @Test
91    public void testScanAttributes() {
92      Scan scan = new Scan();
93      Assert.assertTrue(scan.getAttributesMap().isEmpty());
94      Assert.assertNull(scan.getAttribute("absent"));
95  
96      scan.setAttribute("absent", null);
97      Assert.assertTrue(scan.getAttributesMap().isEmpty());
98      Assert.assertNull(scan.getAttribute("absent"));
99  
100     // adding attribute
101     scan.setAttribute("attribute1", Bytes.toBytes("value1"));
102     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
103     Assert.assertEquals(1, scan.getAttributesMap().size());
104     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"),
105         scan.getAttributesMap().get("attribute1")));
106 
107     // overriding attribute value
108     scan.setAttribute("attribute1", Bytes.toBytes("value12"));
109     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
110     Assert.assertEquals(1, scan.getAttributesMap().size());
111     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"),
112         scan.getAttributesMap().get("attribute1")));
113 
114     // adding another attribute
115     scan.setAttribute("attribute2", Bytes.toBytes("value2"));
116     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
117     Assert.assertEquals(2, scan.getAttributesMap().size());
118     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"),
119         scan.getAttributesMap().get("attribute2")));
120 
121     // removing attribute
122     scan.setAttribute("attribute2", null);
123     Assert.assertNull(scan.getAttribute("attribute2"));
124     Assert.assertEquals(1, scan.getAttributesMap().size());
125     Assert.assertNull(scan.getAttributesMap().get("attribute2"));
126 
127     // removing non-existed attribute
128     scan.setAttribute("attribute2", null);
129     Assert.assertNull(scan.getAttribute("attribute2"));
130     Assert.assertEquals(1, scan.getAttributesMap().size());
131     Assert.assertNull(scan.getAttributesMap().get("attribute2"));
132 
133     // removing another attribute
134     scan.setAttribute("attribute1", null);
135     Assert.assertNull(scan.getAttribute("attribute1"));
136     Assert.assertTrue(scan.getAttributesMap().isEmpty());
137     Assert.assertNull(scan.getAttributesMap().get("attribute1"));
138   }
139 
140   @Test
141   public void testNullQualifier() {
142     Scan scan = new Scan();
143     byte[] family = Bytes.toBytes("family");
144     scan.addColumn(family, null);
145     Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
146     Assert.assertEquals(1, qualifiers.size());
147   }
148 
149   @Test
150   public void testSetAuthorizations() {
151     Scan scan = new Scan();
152     try {
153       scan.setAuthorizations(new Authorizations("\u002b|\u0029"));
154       scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
155       scan.setAuthorizations(new Authorizations("A|B"));
156       scan.setAuthorizations(new Authorizations("A&B"));
157       scan.setAuthorizations(new Authorizations("!B"));
158       scan.setAuthorizations(new Authorizations("A", "(A)"));
159       scan.setAuthorizations(new Authorizations("A", "{A"));
160       scan.setAuthorizations(new Authorizations(" "));
161       scan.setAuthorizations(new Authorizations(":B"));
162       scan.setAuthorizations(new Authorizations("-B"));
163       scan.setAuthorizations(new Authorizations(".B"));
164       scan.setAuthorizations(new Authorizations("/B"));
165     } catch (IllegalArgumentException e) {
166       fail("should not throw exception");
167     }
168   }
169 
170   @Test
171   public void testSetStartRowAndSetStopRow() {
172     Scan scan = new Scan();
173     scan.setStartRow(null);
174     scan.setStartRow(new byte[1]);
175     scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH]);
176     try {
177       scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH+1]);
178       fail("should've thrown exception");
179     } catch (IllegalArgumentException iae) {
180     } catch (Exception e) {
181       fail("expected IllegalArgumentException to be thrown");
182     }
183 
184     scan.setStopRow(null);
185     scan.setStopRow(new byte[1]);
186     scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH]);
187     try {
188       scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH+1]);
189       fail("should've thrown exception");
190     } catch (IllegalArgumentException iae) {
191     } catch (Exception e) {
192       fail("expected IllegalArgumentException to be thrown");
193     }
194   }
195 }