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.filter.CompareFilter.CompareOp;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  /**
29   * Tests for {@link SingleColumnValueExcludeFilter}. Because this filter
30   * extends {@link SingleColumnValueFilter}, only the added functionality is
31   * tested. That is, method filterKeyValue(KeyValue).
32   *
33   * @author ferdy
34   *
35   */
36  public class TestSingleColumnValueExcludeFilter extends TestCase {
37    private static final byte[] ROW = Bytes.toBytes("test");
38    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
39    private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
40    private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
41    private static final byte[] VAL_1 = Bytes.toBytes("a");
42    private static final byte[] VAL_2 = Bytes.toBytes("ab");
43  
44    /**
45     * Test the overridden functionality of filterKeyValue(KeyValue)
46     * @throws Exception
47     */
48    public void testFilterKeyValue() throws Exception {
49      Filter filter = new SingleColumnValueExcludeFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
50          CompareOp.EQUAL, VAL_1);
51  
52      // A 'match' situation
53      KeyValue kv;
54      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
55      // INCLUDE expected because test column has not yet passed
56      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
57      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1);
58      // Test column will pass (will match), will SKIP because test columns are excluded
59      assertTrue("testedMatch", filter.filterKeyValue(kv) == Filter.ReturnCode.SKIP);
60      // Test column has already passed and matched, all subsequent columns are INCLUDE
61      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
62      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
63      assertFalse("allRemainingWhenMatch", filter.filterAllRemaining());
64  
65      // A 'mismatch' situation
66      filter.reset();
67      // INCLUDE expected because test column has not yet passed
68      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
69      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
70      // Test column will pass (wont match), expect NEXT_ROW
71      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2);
72      assertTrue("testedMismatch", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
73      // After a mismatch (at least with LatestVersionOnly), subsequent columns are EXCLUDE
74      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
75      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
76    }
77  
78  }