1   /*
2    * Copyright 2009 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  
21  package org.apache.hadoop.hbase.filter;
22  
23  import junit.framework.TestCase;
24  import org.apache.hadoop.hbase.HConstants;
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.experimental.categories.Category;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.DataInputStream;
32  import java.io.DataOutputStream;
33  import java.io.UnsupportedEncodingException;
34  
35  @Category(SmallTests.class)
36  public class TestPrefixFilter extends TestCase {
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 byte [] GOOD_BYTES = null;
42  
43    static {
44      try {
45        GOOD_BYTES = "abc".getBytes(HConstants.UTF8_ENCODING);
46      } catch (UnsupportedEncodingException e) {
47        fail();
48      }
49    }
50  
51    protected void setUp() throws Exception {
52      super.setUp();
53      this.mainFilter = new PrefixFilter(Bytes.toBytes(HOST_PREFIX));
54    }
55  
56    public void testPrefixOnRow() throws Exception {
57      prefixRowTests(mainFilter);
58    }
59  
60    public void testPrefixOnRowInsideWhileMatchRow() throws Exception {
61      prefixRowTests(new WhileMatchFilter(this.mainFilter), true);
62    }
63  
64    public void testSerialization() throws Exception {
65      // Decompose mainFilter to bytes.
66      ByteArrayOutputStream stream = new ByteArrayOutputStream();
67      DataOutputStream out = new DataOutputStream(stream);
68      mainFilter.write(out);
69      out.close();
70      byte[] buffer = stream.toByteArray();
71  
72      // Recompose filter.
73      DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
74      Filter newFilter = new PrefixFilter();
75      newFilter.readFields(in);
76  
77      // Ensure the serialization preserved the filter by running all test.
78      prefixRowTests(newFilter);
79    }
80  
81    private void prefixRowTests(Filter filter) throws Exception {
82      prefixRowTests(filter, false);
83    }
84  
85    private void prefixRowTests(Filter filter, boolean lastFilterAllRemaining)
86    throws Exception {
87      for (char c = FIRST_CHAR; c <= LAST_CHAR; c++) {
88        byte [] t = createRow(c);
89        assertFalse("Failed with character " + c,
90          filter.filterRowKey(t, 0, t.length));
91        assertFalse(filter.filterAllRemaining());
92      }
93      String yahooSite = "com.yahoo.www";
94      byte [] yahooSiteBytes = Bytes.toBytes(yahooSite);
95      assertTrue("Failed with character " +
96        yahooSite, filter.filterRowKey(yahooSiteBytes, 0, yahooSiteBytes.length));
97      assertEquals(filter.filterAllRemaining(), lastFilterAllRemaining);
98    }
99  
100   private byte [] createRow(final char c) {
101     return Bytes.toBytes(HOST_PREFIX + Character.toString(c));
102   }
103 
104   @org.junit.Rule
105   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
106     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
107 }
108