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.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.io.IOException;
27  import java.util.Arrays;
28  import java.util.Set;
29  
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.filter.FilterList;
32  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
33  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
34  import org.apache.hadoop.hbase.security.visibility.Authorizations;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.junit.Assert;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  // TODO: cover more test cases
42  @Category(SmallTests.class)
43  public class TestScan {
44    @Test
45    public void testAttributesSerialization() throws IOException {
46      Scan scan = new Scan();
47      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
48      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
49      scan.setAttribute("attribute3", Bytes.toBytes("value3"));
50  
51      ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
52  
53      Scan scan2 = ProtobufUtil.toScan(scanProto);
54  
55      Assert.assertNull(scan2.getAttribute("absent"));
56      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
57      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
58      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
59      Assert.assertEquals(3, scan2.getAttributesMap().size());
60    }
61  
62    @Test
63    public void testGetToScan() throws IOException {
64      Get get = new Get(Bytes.toBytes(1));
65      get.setCacheBlocks(true).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
66          .setId("get").setIsolationLevel(IsolationLevel.READ_COMMITTED)
67          .setLoadColumnFamiliesOnDemand(false).setMaxResultsPerColumnFamily(1000)
68          .setMaxVersions(9999).setRowOffsetPerColumnFamily(5).setTimeRange(0, 13)
69          .setAttribute("att_v0", Bytes.toBytes("att_v0"))
70          .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123);
71      Scan scan = new Scan(get);
72      assertEquals(get.getCacheBlocks(), scan.getCacheBlocks());
73      assertEquals(get.getConsistency(), scan.getConsistency());
74      assertEquals(get.getFilter(), scan.getFilter());
75      assertEquals(get.getId(), scan.getId());
76      assertEquals(get.getIsolationLevel(), scan.getIsolationLevel());
77      assertEquals(get.getLoadColumnFamiliesOnDemandValue(),
78        scan.getLoadColumnFamiliesOnDemandValue());
79      assertEquals(get.getMaxResultsPerColumnFamily(), scan.getMaxResultsPerColumnFamily());
80      assertEquals(get.getMaxVersions(), scan.getMaxVersions());
81      assertEquals(get.getRowOffsetPerColumnFamily(), scan.getRowOffsetPerColumnFamily());
82      assertEquals(get.getTimeRange().getMin(), scan.getTimeRange().getMin());
83      assertEquals(get.getTimeRange().getMax(), scan.getTimeRange().getMax());
84      assertTrue(Bytes.equals(get.getAttribute("att_v0"), scan.getAttribute("att_v0")));
85      assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin(),
86        scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin());
87      assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax(),
88        scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax());
89    }
90  
91    @Test
92    public void testScanAttributes() {
93      Scan scan = new Scan();
94      Assert.assertTrue(scan.getAttributesMap().isEmpty());
95      Assert.assertNull(scan.getAttribute("absent"));
96  
97      scan.setAttribute("absent", null);
98      Assert.assertTrue(scan.getAttributesMap().isEmpty());
99      Assert.assertNull(scan.getAttribute("absent"));
100 
101     // adding attribute
102     scan.setAttribute("attribute1", Bytes.toBytes("value1"));
103     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
104     Assert.assertEquals(1, scan.getAttributesMap().size());
105     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), 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"), scan.getAttributesMap().get("attribute1")));
112 
113     // adding another attribute
114     scan.setAttribute("attribute2", Bytes.toBytes("value2"));
115     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
116     Assert.assertEquals(2, scan.getAttributesMap().size());
117     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
118 
119     // removing attribute
120     scan.setAttribute("attribute2", null);
121     Assert.assertNull(scan.getAttribute("attribute2"));
122     Assert.assertEquals(1, scan.getAttributesMap().size());
123     Assert.assertNull(scan.getAttributesMap().get("attribute2"));
124 
125     // removing non-existed attribute
126     scan.setAttribute("attribute2", null);
127     Assert.assertNull(scan.getAttribute("attribute2"));
128     Assert.assertEquals(1, scan.getAttributesMap().size());
129     Assert.assertNull(scan.getAttributesMap().get("attribute2"));
130 
131     // removing another attribute
132     scan.setAttribute("attribute1", null);
133     Assert.assertNull(scan.getAttribute("attribute1"));
134     Assert.assertTrue(scan.getAttributesMap().isEmpty());
135     Assert.assertNull(scan.getAttributesMap().get("attribute1"));
136   }
137 
138   @Test
139   public void testNullQualifier() {
140     Scan scan = new Scan();
141     byte[] family = Bytes.toBytes("family");
142     scan.addColumn(family, null);
143     Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
144     Assert.assertEquals(1, qualifiers.size());
145   }
146 
147   @Test
148   public void testSetAuthorizations() {
149     Scan scan = new Scan();
150     try {
151       scan.setAuthorizations(new Authorizations("\u002b|\u0029"));
152       scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
153       scan.setAuthorizations(new Authorizations("A|B"));
154       scan.setAuthorizations(new Authorizations("A&B"));
155       scan.setAuthorizations(new Authorizations("!B"));
156       scan.setAuthorizations(new Authorizations("A", "(A)"));
157       scan.setAuthorizations(new Authorizations("A", "{A"));
158       scan.setAuthorizations(new Authorizations(" "));
159       scan.setAuthorizations(new Authorizations(":B"));
160       scan.setAuthorizations(new Authorizations("-B"));
161       scan.setAuthorizations(new Authorizations(".B"));
162       scan.setAuthorizations(new Authorizations("/B"));
163     } catch (IllegalArgumentException e) {
164       fail("should not throw exception");
165     }
166   }
167 
168   @Test
169   public void testSetStartRowAndSetStopRow() {
170     Scan scan = new Scan();
171     scan.setStartRow(null);
172     scan.setStartRow(new byte[1]);
173     scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH]);
174     try {
175       scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH+1]);
176       fail("should've thrown exception");
177     } catch (IllegalArgumentException iae) {
178     } catch (Exception e) {
179       fail("expected IllegalArgumentException to be thrown");
180     }
181 
182     scan.setStopRow(null);
183     scan.setStopRow(new byte[1]);
184     scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH]);
185     try {
186       scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH+1]);
187       fail("should've thrown exception");
188     } catch (IllegalArgumentException iae) {
189     } catch (Exception e) {
190       fail("expected IllegalArgumentException to be thrown");
191     }
192   }
193 }
194