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