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.filter;
019
020import org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HBaseTestingUtility;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.*;
024import org.apache.hadoop.hbase.testclassification.FilterTests;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.AfterClass;
028import org.junit.Assert;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Ignore;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in
041 * TestFilterList and TestFilterFromRegionSide instead.
042 */
043@Category({ MediumTests.class, FilterTests.class })
044public class TestFilterListOnMini {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestFilterListOnMini.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestFilterListOnMini.class);
051  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052
053  @Rule
054  public TestName name = new TestName();
055
056  @BeforeClass
057  public static void setUpBeforeClass() throws Exception {
058    TEST_UTIL.startMiniCluster(1);
059  }
060
061  @AfterClass
062  public static void tearDownAfterClass() throws Exception {
063    TEST_UTIL.shutdownMiniCluster();
064  }
065
066  @Test
067  public void testFiltersWithOR() throws Exception {
068    TableName tn = TableName.valueOf(name.getMethodName());
069    Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" });
070    byte[] CF1 = Bytes.toBytes("cf1");
071    byte[] CF2 = Bytes.toBytes("cf2");
072    Put put1 = new Put(Bytes.toBytes("0"));
073    put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0));
074    table.put(put1);
075    Put put2 = new Put(Bytes.toBytes("0"));
076    put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0));
077    table.put(put2);
078    FamilyFilter filterCF1 =
079        new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF1));
080    FamilyFilter filterCF2 =
081        new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF2));
082    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
083    filterList.addFilter(filterCF1);
084    filterList.addFilter(filterCF2);
085    Scan scan = new Scan();
086    scan.setFilter(filterList);
087    ResultScanner scanner = table.getScanner(scan);
088    LOG.info("Filter list: " + filterList);
089    for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
090      Assert.assertEquals(2, rr.size());
091    }
092  }
093}