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 junit.framework.TestCase;
24  import net.sf.jour.log.Logger;
25  
26  /**
27   * TODO Add docs
28   * Created on 04.12.2004
29   *
30   * Contributing Author(s):
31   *
32   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34   *
35   * @author vlads
36   * @version $Revision: 46 $ ($Author: vlads $) $Date: 2007-08-28 14:30:40 -0400 (Tue, 28 Aug 2007) $
37   */
38  public class MatchStringFilterTest extends TestCase {
39  	
40  	protected static final Logger log = Logger.getLogger();
41  	
42  	public static void main(String[] args) {
43  		MatchStringFilterTest b = new MatchStringFilterTest();
44  		b.testStringListPatterns();
45  	}
46  	
47  	private void verify(String pattern, String text, boolean expect) { 
48  		log.debug("verify [" + pattern + "] [" + text + "]");
49  		MatchStringFilter f = new MatchStringFilter(pattern);
50  		if (log.isDebugEnabled()) {
51  			f.debug();
52  		}
53  		assertEquals(pattern + " " + text, expect, f.match(text));
54  	}
55  
56  	private void verifyList(String pattern, String text, boolean expect) {
57  		log.debug("verifyList [" + pattern + "] [" + text + "]"); 
58  		MatchStringListFilter f = new MatchStringListFilter(pattern);
59  		if (log.isDebugEnabled()) {
60  			f.debug();
61  		}
62  		assertEquals(pattern + " " + text, expect, f.match(text));
63  	}
64  	
65  	public void testStringPatterns() {
66  		verify(null, null, false);
67  		verify("", "", false);
68  		verify(null, "", false);
69  		verify("", null, false);
70  		
71  		verify("", "bob", false);
72  		verify("bob", "", false);
73  		
74  		verify("bob", "bob", true);
75  		verify("bob", "bobY", false);
76  		verify("bob*", "bobY", true);
77  		
78  		verify("*bar*", "bar", true);
79  		verify("*bar*", "anybar", true);
80  
81  		verify("*bar.*", "bar", false);
82  		verify("*bar.*", "bar.man", true);
83  		verify("*bar.*", "anybar", false);
84  		verify("*bar.*", "anybar.man", true);
85  		
86  		verify("bar[]", "bar", false);
87  		verify("bar[]", "bar[]", true);
88  		verify("*.bar[]", "any.bar[]", true);
89  		
90  		verify("*.bar.*", "bar", false);
91  		verify("*.bar.*", "anybar", false);
92  		
93  		verify("foo.bar", "foo.bar", true);
94  		verify("foo.bar", "any.bar", false);
95  		verify("foo.bar", "foo.bar2", false);
96  		verify("foo.bar", "2foo.bar", false);
97  		
98  		verify("bob.*", "bob.noe", true);
99  		verify("*.noe", "bob.noe", true);
100 		verify("!bob.*", "john.noe", true);
101 	}
102 	
103 	public void testStringListPatterns() {
104 		verifyList(null, null, false);
105 		verifyList("", "", false);
106 		verifyList(null, "", false);
107 		verifyList("", null, false);
108 		
109 		verifyList("", "bob", false);
110 		verifyList(";", "bob", false);
111 		
112 		verifyList("bob.*", "bob.noe", true);
113 		verifyList("*.noe", "bob.noe", true);
114 		verifyList("*;!bob.*", "john.noe", true);
115 		
116 		verifyList("foo.*;bar.*", "foo.bar", true);
117 		verifyList("foo.*;bar.*", "bar.foo", true);
118 		verifyList("*; !foo.* ; !bar.* ", "bar.foo", false);
119 		verifyList("!foo.*;bar.*", "foo.bar", false);
120 		verifyList("!foo.*;bar.*", "bar.foo.bar", true);
121 		verifyList("*.examples.*; *.tests.*", "bar.tests.bar", true);
122 		verifyList("*.examples.*; *.tests.*", "bar.test.bar", false);
123 		verifyList("*.examples.*; *.tests.*", "bar.examples.bar", true);
124 		verifyList("*.examples.*; *.tests.*", "bar.example.bar", false);
125 	}
126 
127 }