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.util.Bytes;
29  
30  import junit.framework.TestCase;
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  public class TestColumnPaginationFilter extends TestCase
37  {
38      private static final byte[] ROW = Bytes.toBytes("row_1_test");
39      private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
40      private static final byte[] VAL_1 = Bytes.toBytes("a");
41      private static final byte [] COLUMN_QUALIFIER = Bytes.toBytes("foo");
42  
43      private Filter columnPaginationFilter;
44  
45      @Override
46      protected void setUp() throws Exception {
47          super.setUp();
48          columnPaginationFilter = getColumnPaginationFilter();
49  
50      }
51      private Filter getColumnPaginationFilter() {
52          return new ColumnPaginationFilter(1,0);
53      }
54  
55      private Filter serializationTest(Filter filter) throws Exception {
56          ByteArrayOutputStream stream = new ByteArrayOutputStream();
57          DataOutputStream out = new DataOutputStream(stream);
58          filter.write(out);
59          out.close();
60          byte[] buffer = stream.toByteArray();
61  
62          DataInputStream in =
63              new DataInputStream(new ByteArrayInputStream(buffer));
64          Filter newFilter = new ColumnPaginationFilter();
65          newFilter.readFields(in);
66  
67          return newFilter;
68      }
69  
70  
71      /**
72       * The more specific functionality tests are contained within the TestFilters class.  This class is mainly for testing
73       * serialization
74       *
75       * @param filter
76       * @throws Exception
77       */
78      private void basicFilterTests(ColumnPaginationFilter filter) throws Exception
79      {
80        KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1);
81        assertTrue("basicFilter1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
82      }
83  
84      /**
85       * Tests serialization
86       * @throws Exception
87       */
88      public void testSerialization() throws Exception {
89        Filter newFilter = serializationTest(columnPaginationFilter);
90        basicFilterTests((ColumnPaginationFilter)newFilter);
91      }
92  
93  
94  }