001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022import static org.junit.Assert.fail; 023 024import java.io.IOException; 025import java.util.Arrays; 026import java.util.Set; 027import org.apache.commons.lang3.builder.EqualsBuilder; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.client.Scan.ReadType; 031import org.apache.hadoop.hbase.filter.FilterList; 032import org.apache.hadoop.hbase.security.access.Permission; 033import org.apache.hadoop.hbase.security.visibility.Authorizations; 034import org.apache.hadoop.hbase.testclassification.ClientTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.junit.Assert; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; 044 045// TODO: cover more test cases 046@Category({ ClientTests.class, SmallTests.class }) 047public class TestScan { 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestScan.class); 050 051 @Test 052 public void testAttributesSerialization() throws IOException { 053 Scan scan = new Scan(); 054 scan.setAttribute("attribute1", Bytes.toBytes("value1")); 055 scan.setAttribute("attribute2", Bytes.toBytes("value2")); 056 scan.setAttribute("attribute3", Bytes.toBytes("value3")); 057 058 ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan); 059 060 Scan scan2 = ProtobufUtil.toScan(scanProto); 061 062 Assert.assertNull(scan2.getAttribute("absent")); 063 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1"))); 064 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2"))); 065 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3"))); 066 Assert.assertEquals(3, scan2.getAttributesMap().size()); 067 } 068 069 @Test 070 public void testGetToScan() throws Exception { 071 Get get = new Get(Bytes.toBytes(1)); 072 get.setCacheBlocks(true).setConsistency(Consistency.TIMELINE).setFilter(new FilterList()) 073 .setId("get").setIsolationLevel(IsolationLevel.READ_COMMITTED) 074 .setLoadColumnFamiliesOnDemand(false).setMaxResultsPerColumnFamily(1000).readVersions(9999) 075 .setRowOffsetPerColumnFamily(5).setTimeRange(0, 13) 076 .setAttribute("att_v0", Bytes.toBytes("att_v0")) 077 .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123).setReplicaId(3) 078 .setACL("test_user", new Permission(Permission.Action.READ)) 079 .setAuthorizations(new Authorizations("test_label")).setPriority(3); 080 081 Scan scan = new Scan(get); 082 assertEquals(get.getCacheBlocks(), scan.getCacheBlocks()); 083 assertEquals(get.getConsistency(), scan.getConsistency()); 084 assertEquals(get.getFilter(), scan.getFilter()); 085 assertEquals(get.getId(), scan.getId()); 086 assertEquals(get.getIsolationLevel(), scan.getIsolationLevel()); 087 assertEquals(get.getLoadColumnFamiliesOnDemandValue(), 088 scan.getLoadColumnFamiliesOnDemandValue()); 089 assertEquals(get.getMaxResultsPerColumnFamily(), scan.getMaxResultsPerColumnFamily()); 090 assertEquals(get.getMaxVersions(), scan.getMaxVersions()); 091 assertEquals(get.getRowOffsetPerColumnFamily(), scan.getRowOffsetPerColumnFamily()); 092 assertEquals(get.getTimeRange().getMin(), scan.getTimeRange().getMin()); 093 assertEquals(get.getTimeRange().getMax(), scan.getTimeRange().getMax()); 094 assertTrue(Bytes.equals(get.getAttribute("att_v0"), scan.getAttribute("att_v0"))); 095 assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin(), 096 scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin()); 097 assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax(), 098 scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax()); 099 assertEquals(get.getReplicaId(), scan.getReplicaId()); 100 assertEquals(get.getACL(), scan.getACL()); 101 assertEquals(get.getAuthorizations().getLabels(), scan.getAuthorizations().getLabels()); 102 assertEquals(get.getPriority(), scan.getPriority()); 103 } 104 105 @Test 106 public void testScanAttributes() { 107 Scan scan = new Scan(); 108 Assert.assertTrue(scan.getAttributesMap().isEmpty()); 109 Assert.assertNull(scan.getAttribute("absent")); 110 111 scan.setAttribute("absent", null); 112 Assert.assertTrue(scan.getAttributesMap().isEmpty()); 113 Assert.assertNull(scan.getAttribute("absent")); 114 115 // adding attribute 116 scan.setAttribute("attribute1", Bytes.toBytes("value1")); 117 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1"))); 118 Assert.assertEquals(1, scan.getAttributesMap().size()); 119 Assert.assertTrue( 120 Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1"))); 121 122 // overriding attribute value 123 scan.setAttribute("attribute1", Bytes.toBytes("value12")); 124 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1"))); 125 Assert.assertEquals(1, scan.getAttributesMap().size()); 126 Assert.assertTrue( 127 Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1"))); 128 129 // adding another attribute 130 scan.setAttribute("attribute2", Bytes.toBytes("value2")); 131 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2"))); 132 Assert.assertEquals(2, scan.getAttributesMap().size()); 133 Assert.assertTrue( 134 Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2"))); 135 136 // removing attribute 137 scan.setAttribute("attribute2", null); 138 Assert.assertNull(scan.getAttribute("attribute2")); 139 Assert.assertEquals(1, scan.getAttributesMap().size()); 140 Assert.assertNull(scan.getAttributesMap().get("attribute2")); 141 142 // removing non-existed attribute 143 scan.setAttribute("attribute2", null); 144 Assert.assertNull(scan.getAttribute("attribute2")); 145 Assert.assertEquals(1, scan.getAttributesMap().size()); 146 Assert.assertNull(scan.getAttributesMap().get("attribute2")); 147 148 // removing another attribute 149 scan.setAttribute("attribute1", null); 150 Assert.assertNull(scan.getAttribute("attribute1")); 151 Assert.assertTrue(scan.getAttributesMap().isEmpty()); 152 Assert.assertNull(scan.getAttributesMap().get("attribute1")); 153 } 154 155 @Test 156 public void testNullQualifier() { 157 Scan scan = new Scan(); 158 byte[] family = Bytes.toBytes("family"); 159 scan.addColumn(family, null); 160 Set<byte[]> qualifiers = scan.getFamilyMap().get(family); 161 Assert.assertEquals(1, qualifiers.size()); 162 } 163 164 @Test 165 public void testSetAuthorizations() { 166 Scan scan = new Scan(); 167 scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a")); 168 scan.setAuthorizations(new Authorizations("A|B")); 169 scan.setAuthorizations(new Authorizations("A&B")); 170 scan.setAuthorizations(new Authorizations("!B")); 171 scan.setAuthorizations(new Authorizations("A", "(A)")); 172 scan.setAuthorizations(new Authorizations("A", "{A")); 173 scan.setAuthorizations(new Authorizations(" ")); 174 scan.setAuthorizations(new Authorizations(":B")); 175 scan.setAuthorizations(new Authorizations("-B")); 176 scan.setAuthorizations(new Authorizations(".B")); 177 scan.setAuthorizations(new Authorizations("/B")); 178 } 179 180 @Test 181 public void testSetStartRowAndSetStopRow() { 182 Scan scan = new Scan(); 183 scan.withStartRow(null); 184 scan.withStartRow(new byte[1]); 185 scan.withStartRow(new byte[HConstants.MAX_ROW_LENGTH]); 186 try { 187 scan.withStartRow(new byte[HConstants.MAX_ROW_LENGTH + 1]); 188 fail("should've thrown exception"); 189 } catch (IllegalArgumentException iae) { 190 // Expected 191 } 192 193 scan.withStopRow(null); 194 scan.withStopRow(new byte[1]); 195 scan.withStopRow(new byte[HConstants.MAX_ROW_LENGTH]); 196 try { 197 scan.withStopRow(new byte[HConstants.MAX_ROW_LENGTH + 1]); 198 fail("should've thrown exception"); 199 } catch (IllegalArgumentException iae) { 200 // Expected 201 } 202 } 203 204 @Test 205 public void testScanCopyConstructor() throws Exception { 206 Scan scan = new Scan(); 207 208 scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q")) 209 .setACL("test_user", new Permission(Permission.Action.READ)).setAllowPartialResults(true) 210 .setAsyncPrefetch(false).setAttribute("test_key", Bytes.toBytes("test_value")) 211 .setAuthorizations(new Authorizations("test_label")).setBatch(10).setCacheBlocks(false) 212 .setCaching(10).setConsistency(Consistency.TIMELINE).setFilter(new FilterList()) 213 .setId("scan_copy_constructor").setIsolationLevel(IsolationLevel.READ_COMMITTED).setLimit(100) 214 .setLoadColumnFamiliesOnDemand(false).setMaxResultSize(100).setMaxResultsPerColumnFamily(1000) 215 .readVersions(9999).setMvccReadPoint(5).setNeedCursorResult(true).setPriority(1).setRaw(true) 216 .setReplicaId(3).setReversed(true).setRowOffsetPerColumnFamily(5) 217 .setStartStopRowForPrefixScan(Bytes.toBytes("row_")).setScanMetricsEnabled(true) 218 .setReadType(ReadType.STREAM).withStartRow(Bytes.toBytes("row_1")) 219 .withStopRow(Bytes.toBytes("row_2")).setTimeRange(0, 13); 220 221 // create a copy of existing scan object 222 Scan scanCopy = new Scan(scan); 223 224 // validate fields of copied scan object match with the original scan object 225 assertEquals(scan.getACL(), scanCopy.getACL()); 226 assertEquals(scan.getAllowPartialResults(), scanCopy.getAllowPartialResults()); 227 assertEquals(scan.getAttribute("test_key"), scanCopy.getAttribute("test_key")); 228 assertEquals(scan.getAttributeSize(), scanCopy.getAttributeSize()); 229 assertEquals(scan.getAttributesMap(), scanCopy.getAttributesMap()); 230 assertEquals(scan.getAuthorizations().getLabels(), scanCopy.getAuthorizations().getLabels()); 231 assertEquals(scan.getBatch(), scanCopy.getBatch()); 232 assertEquals(scan.getCacheBlocks(), scanCopy.getCacheBlocks()); 233 assertEquals(scan.getCaching(), scanCopy.getCaching()); 234 assertEquals(scan.getConsistency(), scanCopy.getConsistency()); 235 assertEquals(scan.getFamilies().length, scanCopy.getFamilies().length); 236 assertEquals(scan.getFamilies()[0], scanCopy.getFamilies()[0]); 237 assertEquals(scan.getFamilyMap(), scanCopy.getFamilyMap()); 238 assertEquals(scan.getFilter(), scanCopy.getFilter()); 239 assertEquals(scan.getId(), scanCopy.getId()); 240 assertEquals(scan.getIsolationLevel(), scanCopy.getIsolationLevel()); 241 assertEquals(scan.getLimit(), scanCopy.getLimit()); 242 assertEquals(scan.getLoadColumnFamiliesOnDemandValue(), 243 scanCopy.getLoadColumnFamiliesOnDemandValue()); 244 assertEquals(scan.getMaxResultSize(), scanCopy.getMaxResultSize()); 245 assertEquals(scan.getMaxResultsPerColumnFamily(), scanCopy.getMaxResultsPerColumnFamily()); 246 assertEquals(scan.getMaxVersions(), scanCopy.getMaxVersions()); 247 assertEquals(scan.getMvccReadPoint(), scanCopy.getMvccReadPoint()); 248 assertEquals(scan.getPriority(), scanCopy.getPriority()); 249 assertEquals(scan.getReadType(), scanCopy.getReadType()); 250 assertEquals(scan.getReplicaId(), scanCopy.getReplicaId()); 251 assertEquals(scan.getRowOffsetPerColumnFamily(), scanCopy.getRowOffsetPerColumnFamily()); 252 assertEquals(scan.getStartRow(), scanCopy.getStartRow()); 253 assertEquals(scan.getStopRow(), scanCopy.getStopRow()); 254 assertEquals(scan.getTimeRange(), scanCopy.getTimeRange()); 255 256 assertTrue("Make sure copy constructor adds all the fields in the copied object", 257 EqualsBuilder.reflectionEquals(scan, scanCopy)); 258 } 259 260 @Test 261 public void testScanReadType() throws Exception { 262 Scan scan = new Scan(); 263 assertEquals(ReadType.DEFAULT, scan.getReadType()); 264 Scan copyScan = new Scan(scan); 265 assertEquals(ReadType.DEFAULT, copyScan.getReadType()); 266 } 267}