1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
56 byte[] buffer = mainFilter.toByteArray();
57
58
59 Filter newFilter = PrefixFilter.parseFrom(buffer);
60
61
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