1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.experimental.categories.Category;
29  
30  import java.util.List;
31  import java.util.ArrayList;
32  
33  /**
34   * Tests for {@link SingleColumnValueExcludeFilter}. Because this filter
35   * extends {@link SingleColumnValueFilter}, only the added functionality is
36   * tested. That is, method filterKeyValue(KeyValue).
37   *
38   * @author ferdy
39   *
40   */
41  @Category(SmallTests.class)
42  public class TestSingleColumnValueExcludeFilter extends TestCase {
43    private static final byte[] ROW = Bytes.toBytes("test");
44    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
45    private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
46    private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
47    private static final byte[] VAL_1 = Bytes.toBytes("a");
48    private static final byte[] VAL_2 = Bytes.toBytes("ab");
49  
50    /**
51     * Test the overridden functionality of filterKeyValue(KeyValue)
52     * @throws Exception
53     */
54    public void testFilterKeyValue() throws Exception {
55      Filter filter = new SingleColumnValueExcludeFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
56          CompareOp.EQUAL, VAL_1);
57  
58      // A 'match' situation
59      List<KeyValue> kvs = new ArrayList<KeyValue>();
60      KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
61  
62      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
63      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1));
64      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
65  
66      filter.filterRow(kvs);
67  
68      assertEquals("resultSize", kvs.size(), 2);
69      assertTrue("leftKV1", KeyValue.COMPARATOR.compare(kvs.get(0), kv) == 0);
70      assertTrue("leftKV2", KeyValue.COMPARATOR.compare(kvs.get(1), kv) == 0);
71      assertFalse("allRemainingWhenMatch", filter.filterAllRemaining());
72  
73      // A 'mismatch' situation
74      filter.reset();
75      // INCLUDE expected because test column has not yet passed
76      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
77      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
78      // Test column will pass (wont match), expect NEXT_ROW
79      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2);
80      assertTrue("testedMismatch", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
81      // After a mismatch (at least with LatestVersionOnly), subsequent columns are EXCLUDE
82      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
83      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
84    }
85  
86  
87    @org.junit.Rule
88    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
89      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
90  }
91