View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.filter;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  /**
33   * Test for the ColumnPaginationFilter, used mainly to test the successful serialization of the filter.
34   * More test functionality can be found within {@link org.apache.hadoop.hbase.filter.TestFilter#testColumnPaginationFilter()}
35   */
36  @Category(SmallTests.class)
37  public class TestColumnPaginationFilter
38  {
39      private static final byte[] ROW = Bytes.toBytes("row_1_test");
40      private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
41      private static final byte[] VAL_1 = Bytes.toBytes("a");
42      private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
43  
44      private Filter columnPaginationFilterOffset;
45      private Filter columnPaginationFilter;
46  
47      @Before
48      public void setUp() throws Exception {
49          columnPaginationFilter = getColumnPaginationFilter();
50          columnPaginationFilterOffset = getColumnPaginationFilterOffset();
51      }
52  
53      private Filter getColumnPaginationFilter() {
54          return new ColumnPaginationFilter(1, 0);
55      }
56  
57      private Filter getColumnPaginationFilterOffset() {
58          return new ColumnPaginationFilter(1, COLUMN_QUALIFIER);
59      }
60  
61      private Filter serializationTest(Filter filter) throws Exception {
62        FilterProtos.Filter filterProto = ProtobufUtil.toFilter(filter);
63        Filter newFilter = ProtobufUtil.toFilter(filterProto);
64  
65        return newFilter;
66      }
67  
68  
69      /**
70       * The more specific functionality tests are contained within the TestFilters class.  This class is mainly for testing
71       * serialization
72       *
73       * @param filter
74       * @throws Exception
75       */
76      private void basicFilterTests(ColumnPaginationFilter filter) throws Exception
77      {
78        KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1);
79        assertTrue("basicFilter1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE_AND_NEXT_COL);
80      }
81  
82      /**
83       * Tests serialization
84       * @throws Exception
85       */
86      @Test
87      public void testSerialization() throws Exception {
88        Filter newFilter = serializationTest(columnPaginationFilter);
89        basicFilterTests((ColumnPaginationFilter)newFilter);
90  
91        Filter newFilterOffset = serializationTest(columnPaginationFilterOffset);
92        basicFilterTests((ColumnPaginationFilter)newFilterOffset);
93      }
94  
95  
96  
97  }
98