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.util.Bytes;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * Tests the inclusive stop row filter
33   */
34  public class TestInclusiveStopFilter extends TestCase {
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    @Override
42    protected void setUp() throws Exception {
43      super.setUp();
44      mainFilter = new InclusiveStopFilter(STOP_ROW);
45    }
46  
47    /**
48     * Tests identification of the stop row
49     * @throws Exception
50     */
51    public void testStopRowIdentification() throws Exception {
52      stopRowTests(mainFilter);
53    }
54  
55    /**
56     * Tests serialization
57     * @throws Exception
58     */
59    public void testSerialization() throws Exception {
60      // Decompose mainFilter to bytes.
61      ByteArrayOutputStream stream = new ByteArrayOutputStream();
62      DataOutputStream out = new DataOutputStream(stream);
63      mainFilter.write(out);
64      out.close();
65      byte[] buffer = stream.toByteArray();
66  
67      // Recompose mainFilter.
68      DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
69      Filter newFilter = new InclusiveStopFilter();
70      newFilter.readFields(in);
71  
72      // Ensure the serialization preserved the filter by running a full test.
73      stopRowTests(newFilter);
74    }
75  
76    private void stopRowTests(Filter filter) throws Exception {
77      assertFalse("Filtering on " + Bytes.toString(GOOD_ROW),
78        filter.filterRowKey(GOOD_ROW, 0, GOOD_ROW.length));
79      assertFalse("Filtering on " + Bytes.toString(STOP_ROW),
80        filter.filterRowKey(STOP_ROW, 0, STOP_ROW.length));
81      assertTrue("Filtering on " + Bytes.toString(PAST_STOP_ROW),
82        filter.filterRowKey(PAST_STOP_ROW, 0, PAST_STOP_ROW.length));
83  
84      assertTrue("FilterAllRemaining", filter.filterAllRemaining());
85      assertFalse("FilterNotNull", filter.filterRow());
86  
87      assertFalse("Filter a null", filter.filterRowKey(null, 0, 0));
88    }
89  }