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 org.apache.hadoop.hbase.testclassification.SmallTests;
22  import org.apache.hadoop.hbase.util.Bytes;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertTrue;
31  /**
32   * Tests the inclusive stop row filter
33   */
34  @Category(SmallTests.class)
35  public class TestInclusiveStopFilter {
36    private final byte [] STOP_ROW = Bytes.toBytes("stop_row");
37    private final byte [] GOOD_ROW = Bytes.toBytes("good_row");
38    private final byte [] PAST_STOP_ROW = Bytes.toBytes("zzzzzz");
39  
40    Filter mainFilter;
41  
42    @Before
43    public void setUp() throws Exception {
44      mainFilter = new InclusiveStopFilter(STOP_ROW);
45    }
46  
47    /**
48     * Tests identification of the stop row
49     * @throws Exception
50     */
51    @Test
52    public void testStopRowIdentification() throws Exception {
53      stopRowTests(mainFilter);
54    }
55  
56    /**
57     * Tests serialization
58     * @throws Exception
59     */
60    @Test
61    public void testSerialization() throws Exception {
62      // Decompose mainFilter to bytes.
63      byte[] buffer = mainFilter.toByteArray();
64  
65      // Recompose mainFilter.
66      Filter newFilter = InclusiveStopFilter.parseFrom(buffer);
67  
68      // Ensure the serialization preserved the filter by running a full test.
69      stopRowTests(newFilter);
70    }
71  
72    private void stopRowTests(Filter filter) throws Exception {
73      assertFalse("Filtering on " + Bytes.toString(GOOD_ROW),
74        filter.filterRowKey(GOOD_ROW, 0, GOOD_ROW.length));
75      assertFalse("Filtering on " + Bytes.toString(STOP_ROW),
76        filter.filterRowKey(STOP_ROW, 0, STOP_ROW.length));
77      assertTrue("Filtering on " + Bytes.toString(PAST_STOP_ROW),
78        filter.filterRowKey(PAST_STOP_ROW, 0, PAST_STOP_ROW.length));
79  
80      assertTrue("FilterAllRemaining", filter.filterAllRemaining());
81      assertFalse("FilterNotNull", filter.filterRow());
82  
83      assertFalse("Filter a null", filter.filterRowKey(null, 0, 0));
84    }
85  
86  }
87