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  package org.apache.hadoop.hbase.filter;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.client.*;
26  import org.apache.hadoop.hbase.testclassification.FilterTests;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.AfterClass;
30  import org.junit.Assert;
31  import org.junit.BeforeClass;
32  import org.junit.Ignore;
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import org.junit.rules.TestName;
37  
38  /**
39   * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in
40   * TestFilterList and TestFilterFromRegionSide instead.
41   */
42  @Category({ MediumTests.class, FilterTests.class })
43  public class TestFilterListOnMini {
44  
45    private static final Log LOG = LogFactory.getLog(TestFilterListOnMini.class);
46    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47  
48    @Rule
49    public TestName name = new TestName();
50  
51    @BeforeClass
52    public static void setUpBeforeClass() throws Exception {
53      TEST_UTIL.startMiniCluster(1);
54    }
55  
56    @AfterClass
57    public static void tearDownAfterClass() throws Exception {
58      TEST_UTIL.shutdownMiniCluster();
59    }
60  
61    @Test
62    public void testFiltersWithOR() throws Exception {
63      TableName tn = TableName.valueOf(name.getMethodName());
64      Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" });
65      byte[] CF1 = Bytes.toBytes("cf1");
66      byte[] CF2 = Bytes.toBytes("cf2");
67      Put put1 = new Put(Bytes.toBytes("0"));
68      put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0));
69      table.put(put1);
70      Put put2 = new Put(Bytes.toBytes("0"));
71      put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0));
72      table.put(put2);
73      FamilyFilter filterCF1 =
74          new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF1));
75      FamilyFilter filterCF2 =
76          new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF2));
77      FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
78      filterList.addFilter(filterCF1);
79      filterList.addFilter(filterCF2);
80      Scan scan = new Scan();
81      scan.setFilter(filterList);
82      ResultScanner scanner = table.getScanner(scan);
83      LOG.info("Filter list: " + filterList);
84      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
85        Assert.assertEquals(2, rr.size());
86      }
87    }
88  }