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