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