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