001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.Assert.*;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.KeyValueUtil;
024import org.apache.hadoop.hbase.testclassification.FilterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.Before;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({FilterTests.class, SmallTests.class})
033public class TestPrefixFilter {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037      HBaseClassTestRule.forClass(TestPrefixFilter.class);
038
039  Filter mainFilter;
040  static final char FIRST_CHAR = 'a';
041  static final char LAST_CHAR = 'e';
042  static final String HOST_PREFIX = "org.apache.site-";
043  static final byte [] GOOD_BYTES = Bytes.toBytes("abc");
044
045  @Before
046  public void setUp() throws Exception {
047    this.mainFilter = new PrefixFilter(Bytes.toBytes(HOST_PREFIX));
048  }
049
050  @Test
051  public void testPrefixOnRow() throws Exception {
052    prefixRowTests(mainFilter);
053  }
054
055  @Test
056  public void testPrefixOnRowInsideWhileMatchRow() throws Exception {
057    prefixRowTests(new WhileMatchFilter(this.mainFilter), true);
058  }
059
060  @Test
061  public void testSerialization() throws Exception {
062    // Decompose mainFilter to bytes.
063    byte[] buffer = mainFilter.toByteArray();
064
065    // Recompose filter.
066    Filter newFilter = PrefixFilter.parseFrom(buffer);
067
068    // Ensure the serialization preserved the filter by running all test.
069    prefixRowTests(newFilter);
070  }
071
072  private void prefixRowTests(Filter filter) throws Exception {
073    prefixRowTests(filter, false);
074  }
075
076  private void prefixRowTests(Filter filter, boolean lastFilterAllRemaining)
077  throws Exception {
078    for (char c = FIRST_CHAR; c <= LAST_CHAR; c++) {
079      byte [] t = createRow(c);
080      assertFalse("Failed with character " + c,
081        filter.filterRowKey(KeyValueUtil.createFirstOnRow(t)));
082      assertFalse(filter.filterAllRemaining());
083    }
084    String yahooSite = "com.yahoo.www";
085    byte [] yahooSiteBytes = Bytes.toBytes(yahooSite);
086    assertTrue("Failed with character " +
087      yahooSite, filter.filterRowKey(KeyValueUtil.createFirstOnRow(yahooSiteBytes)));
088    assertEquals(filter.filterAllRemaining(), lastFilterAllRemaining);
089  }
090
091  private byte [] createRow(final char c) {
092    return Bytes.toBytes(HOST_PREFIX + Character.toString(c));
093  }
094
095}
096