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.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.filter.FilterList;
030import org.apache.hadoop.hbase.security.visibility.Authorizations;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.Assert;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
040import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
041
042// TODO: cover more test cases
043@Category({ClientTests.class, SmallTests.class})
044public class TestScan {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestScan.class);
049
050  @Test
051  public void testAttributesSerialization() throws IOException {
052    Scan scan = new Scan();
053    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
054    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
055    scan.setAttribute("attribute3", Bytes.toBytes("value3"));
056
057    ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
058
059    Scan scan2 = ProtobufUtil.toScan(scanProto);
060
061    Assert.assertNull(scan2.getAttribute("absent"));
062    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
063    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
064    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
065    Assert.assertEquals(3, scan2.getAttributesMap().size());
066  }
067
068  @Test
069  public void testGetToScan() throws IOException {
070    Get get = new Get(Bytes.toBytes(1));
071    get.setCacheBlocks(true)
072            .setConsistency(Consistency.TIMELINE)
073            .setFilter(new FilterList())
074            .setId("get")
075            .setIsolationLevel(IsolationLevel.READ_COMMITTED)
076            .setLoadColumnFamiliesOnDemand(false)
077            .setMaxResultsPerColumnFamily(1000)
078            .setMaxVersions(9999)
079            .setRowOffsetPerColumnFamily(5)
080            .setTimeRange(0, 13)
081            .setAttribute("att_v0", Bytes.toBytes("att_v0"))
082            .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123);
083    Scan scan = new Scan(get);
084    assertEquals(get.getCacheBlocks(), scan.getCacheBlocks());
085    assertEquals(get.getConsistency(), scan.getConsistency());
086    assertEquals(get.getFilter(), scan.getFilter());
087    assertEquals(get.getId(), scan.getId());
088    assertEquals(get.getIsolationLevel(), scan.getIsolationLevel());
089    assertEquals(get.getLoadColumnFamiliesOnDemandValue(), scan.getLoadColumnFamiliesOnDemandValue());
090    assertEquals(get.getMaxResultsPerColumnFamily(), scan.getMaxResultsPerColumnFamily());
091    assertEquals(get.getMaxVersions(), scan.getMaxVersions());
092    assertEquals(get.getRowOffsetPerColumnFamily(), scan.getRowOffsetPerColumnFamily());
093    assertEquals(get.getTimeRange().getMin(), scan.getTimeRange().getMin());
094    assertEquals(get.getTimeRange().getMax(), scan.getTimeRange().getMax());
095    assertTrue(Bytes.equals(get.getAttribute("att_v0"), scan.getAttribute("att_v0")));
096    assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin(),
097            scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin());
098    assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax(),
099            scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax());
100  }
101
102  @Test
103  public void testScanAttributes() {
104    Scan scan = new Scan();
105    Assert.assertTrue(scan.getAttributesMap().isEmpty());
106    Assert.assertNull(scan.getAttribute("absent"));
107
108    scan.setAttribute("absent", null);
109    Assert.assertTrue(scan.getAttributesMap().isEmpty());
110    Assert.assertNull(scan.getAttribute("absent"));
111
112    // adding attribute
113    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
114    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
115    Assert.assertEquals(1, scan.getAttributesMap().size());
116    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
117
118    // overriding attribute value
119    scan.setAttribute("attribute1", Bytes.toBytes("value12"));
120    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
121    Assert.assertEquals(1, scan.getAttributesMap().size());
122    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
123
124    // adding another attribute
125    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
126    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
127    Assert.assertEquals(2, scan.getAttributesMap().size());
128    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
129
130    // removing attribute
131    scan.setAttribute("attribute2", null);
132    Assert.assertNull(scan.getAttribute("attribute2"));
133    Assert.assertEquals(1, scan.getAttributesMap().size());
134    Assert.assertNull(scan.getAttributesMap().get("attribute2"));
135
136    // removing non-existed 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 another attribute
143    scan.setAttribute("attribute1", null);
144    Assert.assertNull(scan.getAttribute("attribute1"));
145    Assert.assertTrue(scan.getAttributesMap().isEmpty());
146    Assert.assertNull(scan.getAttributesMap().get("attribute1"));
147  }
148
149  @Test
150  public void testNullQualifier() {
151    Scan scan = new Scan();
152    byte[] family = Bytes.toBytes("family");
153    scan.addColumn(family, null);
154    Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
155    Assert.assertEquals(1, qualifiers.size());
156  }
157
158  @Test
159  public void testSetAuthorizations() {
160    Scan scan = new Scan();
161    try {
162      scan.setAuthorizations(new Authorizations("\u002b|\u0029"));
163      scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
164      scan.setAuthorizations(new Authorizations("A|B"));
165      scan.setAuthorizations(new Authorizations("A&B"));
166      scan.setAuthorizations(new Authorizations("!B"));
167      scan.setAuthorizations(new Authorizations("A", "(A)"));
168      scan.setAuthorizations(new Authorizations("A", "{A"));
169      scan.setAuthorizations(new Authorizations(" "));
170      scan.setAuthorizations(new Authorizations(":B"));
171      scan.setAuthorizations(new Authorizations("-B"));
172      scan.setAuthorizations(new Authorizations(".B"));
173      scan.setAuthorizations(new Authorizations("/B"));
174    } catch (IllegalArgumentException e) {
175      fail("should not throw exception");
176    }
177  }
178
179  @Test
180  public void testSetStartRowAndSetStopRow() {
181    Scan scan = new Scan();
182    scan.setStartRow(null);
183    scan.setStartRow(new byte[1]);
184    scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH]);
185    try {
186      scan.setStartRow(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    scan.setStopRow(null);
194    scan.setStopRow(new byte[1]);
195    scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH]);
196    try {
197      scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH+1]);
198      fail("should've thrown exception");
199    } catch (IllegalArgumentException iae) {
200    } catch (Exception e) {
201      fail("expected IllegalArgumentException to be thrown");
202    }
203  }
204}
205