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