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 static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellComparatorImpl;
028import org.apache.hadoop.hbase.CompareOperator;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.testclassification.FilterTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038/**
039 * Tests for {@link SingleColumnValueExcludeFilter}. Because this filter extends
040 * {@link SingleColumnValueFilter}, only the added functionality is tested. That is, method
041 * filterCell(Cell).
042 */
043@Category({ FilterTests.class, SmallTests.class })
044public class TestSingleColumnValueExcludeFilter {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestSingleColumnValueExcludeFilter.class);
049
050  private static final byte[] ROW = Bytes.toBytes("test");
051  private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
052  private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
053  private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
054  private static final byte[] VAL_1 = Bytes.toBytes("a");
055  private static final byte[] VAL_2 = Bytes.toBytes("ab");
056
057  /**
058   * Test the overridden functionality of filterCell(Cell) n
059   */
060  @Test
061  public void testFilterCell() throws Exception {
062    Filter filter = new SingleColumnValueExcludeFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
063      CompareOperator.EQUAL, VAL_1);
064
065    // A 'match' situation
066    List<Cell> kvs = new ArrayList<>();
067    KeyValue c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
068
069    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
070    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1));
071    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
072
073    filter.filterRowCells(kvs);
074
075    assertEquals("resultSize", 2, kvs.size());
076    assertTrue("leftKV1", CellComparatorImpl.COMPARATOR.compare(kvs.get(0), c) == 0);
077    assertTrue("leftKV2", CellComparatorImpl.COMPARATOR.compare(kvs.get(1), c) == 0);
078    assertFalse("allRemainingWhenMatch", filter.filterAllRemaining());
079
080    // A 'mismatch' situation
081    filter.reset();
082    // INCLUDE expected because test column has not yet passed
083    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
084    assertTrue("otherColumn", filter.filterCell(c) == Filter.ReturnCode.INCLUDE);
085    // Test column will pass (wont match), expect NEXT_ROW
086    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2);
087    assertTrue("testedMismatch", filter.filterCell(c) == Filter.ReturnCode.NEXT_ROW);
088    // After a mismatch (at least with LatestVersionOnly), subsequent columns are EXCLUDE
089    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
090    assertTrue("otherColumn", filter.filterCell(c) == Filter.ReturnCode.NEXT_ROW);
091  }
092
093}