KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > PathMatcherTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author Alef Arendsen
23  */

24 public class PathMatcherTests extends TestCase {
25
26     public void testAntPathMatcher() {
27         PathMatcher pathMatcher = new AntPathMatcher();
28
29         // test exact matching
30
assertTrue(pathMatcher.match("test", "test"));
31         assertTrue(pathMatcher.match("/test", "/test"));
32         assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));
33         assertFalse(pathMatcher.match("test", "/test"));
34         assertFalse(pathMatcher.match("/test", "test"));
35
36         // test matching with ?'s
37
assertTrue(pathMatcher.match("t?st", "test"));
38         assertTrue(pathMatcher.match("??st", "test"));
39         assertTrue(pathMatcher.match("tes?", "test"));
40         assertTrue(pathMatcher.match("te??", "test"));
41         assertTrue(pathMatcher.match("?es?", "test"));
42         assertFalse(pathMatcher.match("tes?", "tes"));
43         assertFalse(pathMatcher.match("tes?", "testt"));
44         assertFalse(pathMatcher.match("tes?", "tsst"));
45
46         // test matchin with *'s
47
assertTrue(pathMatcher.match("*", "test"));
48         assertTrue(pathMatcher.match("test*", "test"));
49         assertTrue(pathMatcher.match("test*", "testTest"));
50         assertTrue(pathMatcher.match("*test*", "AnothertestTest"));
51         assertTrue(pathMatcher.match("*test", "Anothertest"));
52         assertTrue(pathMatcher.match("*.*", "test."));
53         assertTrue(pathMatcher.match("*.*", "test.test"));
54         assertTrue(pathMatcher.match("*.*", "test.test.test"));
55         assertTrue(pathMatcher.match("test*aaa", "testblaaaa"));
56         assertFalse(pathMatcher.match("test*", "tst"));
57         assertFalse(pathMatcher.match("test*", "tsttest"));
58         assertFalse(pathMatcher.match("*test*", "tsttst"));
59         assertFalse(pathMatcher.match("*test", "tsttst"));
60         assertFalse(pathMatcher.match("*.*", "tsttst"));
61         assertFalse(pathMatcher.match("test*aaa", "test"));
62         assertFalse(pathMatcher.match("test*aaa", "testblaaab"));
63
64         // test matching with ?'s and /'s
65
assertTrue(pathMatcher.match("/?", "/a"));
66         assertTrue(pathMatcher.match("/?/a", "/a/a"));
67         assertTrue(pathMatcher.match("/a/?", "/a/b"));
68         assertTrue(pathMatcher.match("/??/a", "/aa/a"));
69         assertTrue(pathMatcher.match("/a/??", "/a/bb"));
70         assertTrue(pathMatcher.match("/?", "/a"));
71
72         // test matching with **'s
73
assertTrue(pathMatcher.match("/**", "/testing/testing"));
74         assertTrue(pathMatcher.match("/*/**", "/testing/testing"));
75         assertTrue(pathMatcher.match("/**/*", "/testing/testing"));
76         assertTrue(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla"));
77         assertTrue(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla"));
78         assertTrue(pathMatcher.match("/**/test", "/bla/bla/test"));
79         assertTrue(pathMatcher.match("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla"));
80         assertTrue(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test"));
81         assertTrue(pathMatcher.match("/*bla/test", "/XXXbla/test"));
82         assertFalse(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));
83         assertFalse(pathMatcher.match("/*bla/test", "XXXblab/test"));
84         assertFalse(pathMatcher.match("/*bla/test", "XXXbl/test"));
85
86         assertFalse(pathMatcher.match("/????", "/bala/bla"));
87         assertFalse(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));
88
89         assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));
90         assertTrue(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));
91         assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));
92         assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));
93
94         assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/"));
95         assertTrue(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing"));
96         assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing"));
97         assertFalse(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing"));
98
99         assertFalse(pathMatcher.match("/x/x/x/", "/x/x/**/bla"));
100
101         assertTrue(pathMatcher.match("", ""));
102         assertTrue(pathMatcher.match("", ""));
103         assertTrue(pathMatcher.match("", ""));
104         assertTrue(pathMatcher.match("", ""));
105         assertTrue(pathMatcher.match("", ""));
106         assertTrue(pathMatcher.match("", ""));
107     }
108
109 }
110
Popular Tags