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.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
61 byte[] buffer = mainFilter.toByteArray();
62
63
64 Filter newFilter = PrefixFilter.parseFrom(buffer);
65
66
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