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 junit.framework.TestCase;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Tests for the page filter
33   */
34  @Category(SmallTests.class)
35  public class TestPageFilter extends TestCase {
36    static final int ROW_LIMIT = 3;
37  
38    /**
39     * test page size filter
40     * @throws Exception
41     */
42    public void testPageSize() throws Exception {
43      Filter f = new PageFilter(ROW_LIMIT);
44      pageSizeTests(f);
45    }
46  
47    /**
48     * Test filter serialization
49     * @throws Exception
50     */
51    public void testSerialization() throws Exception {
52      Filter f = new PageFilter(ROW_LIMIT);
53      // Decompose mainFilter to bytes.
54      ByteArrayOutputStream stream = new ByteArrayOutputStream();
55      DataOutputStream out = new DataOutputStream(stream);
56      f.write(out);
57      out.close();
58      byte[] buffer = stream.toByteArray();
59      // Recompose mainFilter.
60      DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
61      Filter newFilter = new PageFilter();
62      newFilter.readFields(in);
63  
64      // Ensure the serialization preserved the filter by running a full test.
65      pageSizeTests(newFilter);
66    }
67  
68    private void pageSizeTests(Filter f) throws Exception {
69      testFiltersBeyondPageSize(f, ROW_LIMIT);
70    }
71  
72    private void testFiltersBeyondPageSize(final Filter f, final int pageSize) {
73      int count = 0;
74      for (int i = 0; i < (pageSize * 2); i++) {
75        boolean filterOut = f.filterRow();
76  
77        if(filterOut) {
78          break;
79        } else {
80          count++;
81        }
82  
83        // If at last row, should tell us to skip all remaining
84        if(count == pageSize) {
85          assertTrue(f.filterAllRemaining());
86        } else {
87          assertFalse(f.filterAllRemaining());
88        }
89  
90      }
91      assertEquals(pageSize, count);
92    }
93  
94    @org.junit.Rule
95    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
96      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
97  }
98