1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.filter;
22  
23  import org.apache.log4j.Logger;
24  
25  import junit.framework.TestCase;
26  
27  /***
28   * TODO Add docs
29   * Created on 04.12.2004
30   *
31   * Contributing Author(s):
32   *
33   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
34   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
35   *
36   * @author vlads
37   * @version $Revision: 1.4 $ ($Author: vlads $) $Date: 2004/12/06 19:53:46 $
38   */
39  public class MatchStringFilterTest extends TestCase {
40  	
41  	protected static final Logger log = Logger.getLogger(MatchStringFilterTest.class);
42  	
43  	public static void main(String[] args) {
44  		MatchStringFilterTest b = new MatchStringFilterTest();
45  		b.testStringListPatterns();
46  	}
47  	
48  	private void verify(String pattern, String text, boolean expect) { 
49  		log.debug("verify [" + pattern + "] [" + text + "]");
50  		MatchStringFilter f = new MatchStringFilter(pattern);
51  		if (log.isDebugEnabled()) {
52  			f.debug();
53  		}
54  		assertEquals(pattern + " " + text, expect, f.match(text));
55  	}
56  
57  	private void verifyList(String pattern, String text, boolean expect) {
58  		log.debug("verifyList [" + pattern + "] [" + text + "]"); 
59  		MatchStringListFilter f = new MatchStringListFilter(pattern);
60  		if (log.isDebugEnabled()) {
61  			f.debug();
62  		}
63  		assertEquals(pattern + " " + text, expect, f.match(text));
64  	}
65  	
66  	public void testStringPatterns() {
67  		verify(null, null, false);
68  		verify("", "", false);
69  		verify(null, "", false);
70  		verify("", null, false);
71  		
72  		verify("", "bob", false);
73  		verify("bob", "", false);
74  		
75  		verify("bob", "bob", true);
76  		verify("bob", "bobY", false);
77  		verify("bob*", "bobY", true);
78  		
79  		verify("*bar*", "bar", true);
80  		verify("*bar*", "anybar", true);
81  
82  		verify("*bar.*", "bar", false);
83  		verify("*bar.*", "bar.man", true);
84  		verify("*bar.*", "anybar", false);
85  		verify("*bar.*", "anybar.man", true);
86  		
87  		verify("bar[]", "bar", false);
88  		verify("bar[]", "bar[]", true);
89  		verify("*.bar[]", "any.bar[]", true);
90  		
91  		verify("*.bar.*", "bar", false);
92  		verify("*.bar.*", "anybar", false);
93  		
94  		verify("foo.bar", "foo.bar", true);
95  		verify("foo.bar", "any.bar", false);
96  		verify("foo.bar", "foo.bar2", false);
97  		verify("foo.bar", "2foo.bar", false);
98  		
99  		verify("bob.*", "bob.noe", true);
100 		verify("*.noe", "bob.noe", true);
101 		verify("!bob.*", "john.noe", true);
102 	}
103 	
104 	public void testStringListPatterns() {
105 		verifyList(null, null, false);
106 		verifyList("", "", false);
107 		verifyList(null, "", false);
108 		verifyList("", null, false);
109 		
110 		verifyList("", "bob", false);
111 		verifyList(";", "bob", false);
112 		
113 		verifyList("bob.*", "bob.noe", true);
114 		verifyList("*.noe", "bob.noe", true);
115 		verifyList("*;!bob.*", "john.noe", true);
116 		
117 		verifyList("foo.*;bar.*", "foo.bar", true);
118 		verifyList("foo.*;bar.*", "bar.foo", true);
119 		verifyList("*; !foo.* ; !bar.* ", "bar.foo", false);
120 		verifyList("!foo.*;bar.*", "foo.bar", false);
121 		verifyList("!foo.*;bar.*", "bar.foo.bar", true);
122 	}
123 
124 }