1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
73 DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
74 Filter newFilter = new PrefixFilter();
75 newFilter.readFields(in);
76
77
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