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.assertTrue;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.KeyValue;
024import org.apache.hadoop.hbase.testclassification.FilterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.Before;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
034
035/**
036 * Test for the ColumnPaginationFilter, used mainly to test the successful serialization of the filter.
037 * More test functionality can be found within {@link org.apache.hadoop.hbase.filter.TestFilter#testColumnPaginationFilter()}
038 */
039@Category({FilterTests.class, SmallTests.class})
040public class TestColumnPaginationFilter
041{
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestColumnPaginationFilter.class);
046
047    private static final byte[] ROW = Bytes.toBytes("row_1_test");
048    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
049    private static final byte[] VAL_1 = Bytes.toBytes("a");
050    private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
051
052    private Filter columnPaginationFilterOffset;
053    private Filter columnPaginationFilter;
054
055    @Before
056    public void setUp() throws Exception {
057        columnPaginationFilter = getColumnPaginationFilter();
058        columnPaginationFilterOffset = getColumnPaginationFilterOffset();
059    }
060
061    private Filter getColumnPaginationFilter() {
062        return new ColumnPaginationFilter(1, 0);
063    }
064
065    private Filter getColumnPaginationFilterOffset() {
066        return new ColumnPaginationFilter(1, COLUMN_QUALIFIER);
067    }
068
069    private Filter serializationTest(Filter filter) throws Exception {
070      FilterProtos.Filter filterProto = ProtobufUtil.toFilter(filter);
071      Filter newFilter = ProtobufUtil.toFilter(filterProto);
072
073      return newFilter;
074    }
075
076
077    /**
078     * The more specific functionality tests are contained within the TestFilters class.  This class is mainly for testing
079     * serialization
080     *
081     * @param filter
082     * @throws Exception
083     */
084    private void basicFilterTests(ColumnPaginationFilter filter) throws Exception
085    {
086      KeyValue c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1);
087      assertTrue("basicFilter1", filter.filterCell(c) == Filter.ReturnCode.INCLUDE_AND_NEXT_COL);
088    }
089
090    /**
091     * Tests serialization
092     * @throws Exception
093     */
094    @Test
095    public void testSerialization() throws Exception {
096      Filter newFilter = serializationTest(columnPaginationFilter);
097      basicFilterTests((ColumnPaginationFilter)newFilter);
098
099      Filter newFilterOffset = serializationTest(columnPaginationFilterOffset);
100      basicFilterTests((ColumnPaginationFilter)newFilterOffset);
101    }
102
103
104
105}
106