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.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
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
70 DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
71 Filter newFilter = new PrefixFilter();
72 newFilter.readFields(in);
73
74
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 }