View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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         //do the right thing on a non-match
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         //check that we match content correctly
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  }