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