1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.util;
19
20 import junit.framework.TestCase;
21 import org.apache.hadoop.chukwa.ChunkImpl;
22
23 public class TestFilter extends TestCase {
24
25 public void testBasicPatternMatching() {
26 try {
27 Filter rules = new Filter("host=foo.*&cluster=bar&datatype=Data");
28 assertEquals(3, rules.size());
29 byte[] dat = "someText".getBytes();
30 ChunkImpl chunkNone = new ChunkImpl("badData","aname", dat.length, dat, null);
31 assertFalse(rules.matches(chunkNone));
32 assertTrue(Filter.ALL.matches(chunkNone));
33
34
35
36 ChunkImpl chunkSome = new ChunkImpl("badData", "aname", dat.length, dat, null);
37 chunkSome.setSource("fooly");
38 chunkSome.addTag("cluster=\"bar\"");
39 assertFalse(rules.matches( chunkSome));
40 assertTrue(Filter.ALL.matches(chunkSome));
41
42 ChunkImpl chunkAll = new ChunkImpl("Data", "aname", dat.length, dat, null);
43 chunkAll.setSource("fooly");
44 chunkAll.addTag("cluster=\"bar\"");
45
46 assertTrue(rules.matches(chunkAll));
47 assertTrue(Filter.ALL.matches(chunkAll));
48
49
50
51 rules = new Filter("content=someText");
52 assertTrue(rules.matches(chunkAll));
53 rules = new Filter("content=some");
54 assertFalse(rules.matches( chunkAll));
55 rules = new Filter("datatype=Data&content=.*some.*");
56 assertTrue(rules.matches( chunkAll));
57
58 } catch(Exception e) {
59 fail("exception " + e);
60 }
61 }
62
63 public void testClusterPatterns() {
64 byte[] dat = "someText".getBytes();
65 ChunkImpl chunk1 = new ChunkImpl("Data", "aname", dat.length, dat, null);
66 chunk1.setSource("asource");
67 assertTrue(Filter.ALL.matches(chunk1));
68 Filter rule = new Filter("tags.foo=bar");
69
70 assertFalse(rule.matches(chunk1));
71 chunk1.addTag("foo=\"bar\"");
72 assertTrue(rule.matches(chunk1));
73 chunk1.addTag("baz=\"quux\"");
74 assertTrue(rule.matches(chunk1));
75 assertTrue(Filter.ALL.matches(chunk1));
76 }
77
78 }