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  
20  package org.apache.hadoop.hbase.filter;
21  
22  import org.apache.hadoop.hbase.testclassification.SmallTests;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  import static org.junit.Assert.*;
29  
30  @Category(SmallTests.class)
31  public class TestPrefixFilter {
32    Filter mainFilter;
33    static final char FIRST_CHAR = 'a';
34    static final char LAST_CHAR = 'e';
35    static final String HOST_PREFIX = "org.apache.site-";
36    static final byte [] GOOD_BYTES = Bytes.toBytes("abc");
37  
38    @Before
39    public void setUp() throws Exception {
40      this.mainFilter = new PrefixFilter(Bytes.toBytes(HOST_PREFIX));
41    }
42  
43    @Test
44    public void testPrefixOnRow() throws Exception {
45      prefixRowTests(mainFilter);
46    }
47  
48    @Test
49    public void testPrefixOnRowInsideWhileMatchRow() throws Exception {
50      prefixRowTests(new WhileMatchFilter(this.mainFilter), true);
51    }
52  
53    @Test
54    public void testSerialization() throws Exception {
55      // Decompose mainFilter to bytes.
56      byte[] buffer = mainFilter.toByteArray();
57  
58      // Recompose filter.
59      Filter newFilter = PrefixFilter.parseFrom(buffer);
60  
61      // Ensure the serialization preserved the filter by running all test.
62      prefixRowTests(newFilter);
63    }
64  
65    private void prefixRowTests(Filter filter) throws Exception {
66      prefixRowTests(filter, false);
67    }
68  
69    private void prefixRowTests(Filter filter, boolean lastFilterAllRemaining)
70    throws Exception {
71      for (char c = FIRST_CHAR; c <= LAST_CHAR; c++) {
72        byte [] t = createRow(c);
73        assertFalse("Failed with character " + c,
74          filter.filterRowKey(t, 0, t.length));
75        assertFalse(filter.filterAllRemaining());
76      }
77      String yahooSite = "com.yahoo.www";
78      byte [] yahooSiteBytes = Bytes.toBytes(yahooSite);
79      assertTrue("Failed with character " +
80        yahooSite, filter.filterRowKey(yahooSiteBytes, 0, yahooSiteBytes.length));
81      assertEquals(filter.filterAllRemaining(), lastFilterAllRemaining);
82    }
83  
84    private byte [] createRow(final char c) {
85      return Bytes.toBytes(HOST_PREFIX + Character.toString(c));
86    }
87  
88  }
89