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 java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import junit.framework.TestCase;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * Test for the ColumnPaginationFilter, used mainly to test the successful serialization of the filter.
36   * More test functionality can be found within {@link org.apache.hadoop.hbase.filter.TestFilter#testColumnPaginationFilter()}
37   */
38  @Category(SmallTests.class)
39  public class TestColumnPaginationFilter extends TestCase
40  {
41      private static final byte[] ROW = Bytes.toBytes("row_1_test");
42      private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
43      private static final byte[] VAL_1 = Bytes.toBytes("a");
44      private static final byte [] COLUMN_QUALIFIER = Bytes.toBytes("foo");
45  
46      private Filter columnPaginationFilter;
47  
48      @Override
49      protected void setUp() throws Exception {
50          super.setUp();
51          columnPaginationFilter = getColumnPaginationFilter();
52  
53      }
54      private Filter getColumnPaginationFilter() {
55          return new ColumnPaginationFilter(1,0);
56      }
57  
58      private Filter serializationTest(Filter filter) throws Exception {
59          ByteArrayOutputStream stream = new ByteArrayOutputStream();
60          DataOutputStream out = new DataOutputStream(stream);
61          filter.write(out);
62          out.close();
63          byte[] buffer = stream.toByteArray();
64  
65          DataInputStream in =
66              new DataInputStream(new ByteArrayInputStream(buffer));
67          Filter newFilter = new ColumnPaginationFilter();
68          newFilter.readFields(in);
69  
70          return newFilter;
71      }
72  
73  
74      /**
75       * The more specific functionality tests are contained within the TestFilters class.  This class is mainly for testing
76       * serialization
77       *
78       * @param filter
79       * @throws Exception
80       */
81      private void basicFilterTests(ColumnPaginationFilter filter) throws Exception
82      {
83        KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1);
84        assertTrue("basicFilter1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE_AND_NEXT_COL);
85      }
86  
87      /**
88       * Tests serialization
89       * @throws Exception
90       */
91      public void testSerialization() throws Exception {
92        Filter newFilter = serializationTest(columnPaginationFilter);
93        basicFilterTests((ColumnPaginationFilter)newFilter);
94      }
95  
96  
97  
98    @org.junit.Rule
99    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
100     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
101 }
102