KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > WildcardTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import junit.framework.TestCase;
6
7 public class WildcardTest extends TestCase {
8
9     public void testMatch() {
10         assertTrue(Wildcard.match("CfgOptions.class", "C*class"));
11         assertFalse(Wildcard.match("CfgOptions.class", "C*clas"));
12         assertFalse(Wildcard.match("C*r*class", "CfgOptions.class"));
13
14         // wildcard on the start and end of the expression
15
assertTrue(Wildcard.match("CfgOptions.class", "*g*class"));
16         assertTrue(Wildcard.match("CfgOptions.class", "*C*g*class"));
17         assertTrue(Wildcard.match("CfgOptions.class", "C*g*cl*"));
18         assertTrue(Wildcard.match("CfgOptions.class", "*C*g*cl*"));
19
20         // multiple wildcards
21
assertTrue(Wildcard.match("CfgOptions.class", "*g*c**ss"));
22         assertTrue(Wildcard.match("CfgOptions.class", "*g*c**s")); // THIS IS TRUE!!!
23
assertTrue(Wildcard.match("CfgOptions.class", "*gOpti******ons.c**ss"));
24         assertTrue(Wildcard.match("CfgOptions.class", "***gOpti*ons.c**ss"));
25         assertTrue(Wildcard.match("CfgOptions.class", "***gOptions.c**"));
26
27         // wildcard '?':
28
assertTrue(Wildcard.match("CfgOptions.class", "*gOpti*c?ass"));
29         assertFalse(Wildcard.match("CfgOptions.class", "*gOpti*c?ss"));
30
31         // wildcard on the start and end of the expression:
32
assertTrue(Wildcard.match("CfgOptions.class", "*gOpti*c?as?"));
33         assertFalse(Wildcard.match("CfgOptions.class", "*gOpti*c?a?"));
34         assertTrue(Wildcard.match("CfgOptions.class", "?fgOpti*c?ass"));
35         assertFalse(Wildcard.match("CfgOptions.class", "?gOpti*c?as?"));
36         assertTrue(Wildcard.match("CfgOptions.class", "??gOpti*c?ass"));
37
38         // multiple wildcards
39
assertTrue(Wildcard.match("CfgOptions.class", "C????ti*c?ass"));
40         assertFalse(Wildcard.match("CfgOptions.class", "C???ti*c?ass"));
41
42         // wildcards '*' and '?' mixed together:
43
assertTrue(Wildcard.match("CfgOptions.class", "C??*ti*c?ass"));
44         assertTrue(Wildcard.match("CfgOptions.class", "C*ti*c?as?*"));
45         assertTrue(Wildcard.match("CfgOptions.class", "C*ti*c?a*?"));
46         assertTrue(Wildcard.match("CfgOptions.class", "C*ti*?a*"));
47         assertTrue(Wildcard.match("CfgOptions.class", "C*ti*c?a?*"));
48         assertFalse(Wildcard.match("CfgOptions.class", "C*ti*c?*la?*"));
49
50         // escaped wildcards:
51
assertFalse(Wildcard.match("CfgOptions.class", "*gOpti\\*c?ass"));
52         assertTrue(Wildcard.match("CfgOpti*class", "*gOpti\\*class"));
53         assertTrue(Wildcard.match("CfgOpti*class", "*gOpti\\*c?ass"));
54         assertFalse(Wildcard.match("CfgOpti*class", "*gOpti\\\\*c?ass"));
55         assertTrue(Wildcard.match("CfgOpti\\*class", "*gOpti\\\\*c?ass"));
56         assertTrue(Wildcard.match("CfgOpti?class", "*gOpti\\?c*ass"));
57         assertFalse(Wildcard.match("CfgOpti\\?class", "*gOpti\\?c*ass"));
58         assertTrue(Wildcard.match("CfgOptions.class", "CfgOpti\\ons.class"));
59         assertTrue(Wildcard.match("What's this?", "What*\\?"));
60     }
61
62 }
63
Popular Tags