所有分类
  • 所有分类
  • 未分类

Spring工具类-路径匹配(AntPathMatcher)的使用

简介

整个Spring(SpringBoot)框架的路径解析都是按照Ant的风格来的,比如:Controller的请求路径、文件路径、包的路径。所以,掌握Ant的路径匹配很重要。

Spring中的具体实现: org.springframework.util.AntPathMatcher。其注释里边有解释,翻译成中文如下表:

符号作用示例
?匹配一个字符。 不能匹配目录:这个字符不能是代表路径分隔符的//dir/app? 匹配:/dir/app1、/dir/app2 不匹配:/dir/app、/dir/app12、index/
*匹配0到多个字符。/dir/app* 匹配:/dir/app、/dir/app1、/dir/app12、/dir/appa/ 不匹配:/dir/app/a
**匹配多级目录。/dir/**/app* 匹配:/dir/xxx/app* /dir/xxx/yyy/app* /dir/app/** 匹配:dir/app/aa/bcd/e
{spring:[a-z]+}将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量。 必须是完全匹配才行,在SpringMVC中只有完全匹配才会进入controller层的方法@RequestMapping(“/index/{username:[a-b]+}”)
@ResponseBody
public String index(@PathVariable(“username”) String username){
    System.out.println(username);
    return username;
} 结果 index/ab           true  输出 ab
index/abbaaa    true  输出 abbaaa
index/a             false 404错误
index/ac            false 404错误

大全(有实例)

摘录自Spring的官网测试,不得不说 Spring 的测试用例写的实在是太完善了。 其网址为:https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

其测试太长了,本处将其分类。下边这行是公共代码:

private final AntPathMatcher pathMatcher = new AntPathMatcher();

match

// test exact matching
assertThat(pathMatcher.match("test", "test")).isTrue();
assertThat(pathMatcher.match("/test", "/test")).isTrue();
// SPR-14141
assertThat(pathMatcher.match("https://example.org", "https://example.org")).isTrue();
assertThat(pathMatcher.match("/test.jpg", "test.jpg")).isFalse();
assertThat(pathMatcher.match("test", "/test")).isFalse();
assertThat(pathMatcher.match("/test", "test")).isFalse();

// test matching with ?'s
assertThat(pathMatcher.match("t?st", "test")).isTrue();
assertThat(pathMatcher.match("??st", "test")).isTrue();
assertThat(pathMatcher.match("tes?", "test")).isTrue();
assertThat(pathMatcher.match("te??", "test")).isTrue();
assertThat(pathMatcher.match("?es?", "test")).isTrue();
assertThat(pathMatcher.match("tes?", "tes")).isFalse();
assertThat(pathMatcher.match("tes?", "testt")).isFalse();
assertThat(pathMatcher.match("tes?", "tsst")).isFalse();

// test matching with *'s
assertThat(pathMatcher.match("*", "test")).isTrue();
assertThat(pathMatcher.match("test*", "test")).isTrue();
assertThat(pathMatcher.match("test*", "testTest")).isTrue();
assertThat(pathMatcher.match("test/*", "test/Test")).isTrue();
assertThat(pathMatcher.match("test/*", "test/t")).isTrue();
assertThat(pathMatcher.match("test/*", "test/")).isTrue();
assertThat(pathMatcher.match("*test*", "AnothertestTest")).isTrue();
assertThat(pathMatcher.match("*test", "Anothertest")).isTrue();
assertThat(pathMatcher.match("*.*", "test.")).isTrue();
assertThat(pathMatcher.match("*.*", "test.test")).isTrue();
assertThat(pathMatcher.match("*.*", "test.test.test")).isTrue();
assertThat(pathMatcher.match("test*aaa", "testblaaaa")).isTrue();
assertThat(pathMatcher.match("test*", "tst")).isFalse();
assertThat(pathMatcher.match("test*", "tsttest")).isFalse();
assertThat(pathMatcher.match("test*", "test/")).isFalse();
assertThat(pathMatcher.match("test*", "test/t")).isFalse();
assertThat(pathMatcher.match("test/*", "test")).isFalse();
assertThat(pathMatcher.match("*test*", "tsttst")).isFalse();
assertThat(pathMatcher.match("*test", "tsttst")).isFalse();
assertThat(pathMatcher.match("*.*", "tsttst")).isFalse();
assertThat(pathMatcher.match("test*aaa", "test")).isFalse();
assertThat(pathMatcher.match("test*aaa", "testblaaab")).isFalse();

// test matching with ?'s and /'s
assertThat(pathMatcher.match("/?", "/a")).isTrue();
assertThat(pathMatcher.match("/?/a", "/a/a")).isTrue();
assertThat(pathMatcher.match("/a/?", "/a/b")).isTrue();
assertThat(pathMatcher.match("/??/a", "/aa/a")).isTrue();
assertThat(pathMatcher.match("/a/??", "/a/bb")).isTrue();
assertThat(pathMatcher.match("/?", "/a")).isTrue();

// test matching with **'s
assertThat(pathMatcher.match("/**", "/testing/testing")).isTrue();
assertThat(pathMatcher.match("/*/**", "/testing/testing")).isTrue();
assertThat(pathMatcher.match("/**/*", "/testing/testing")).isTrue();
assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla")).isTrue();
assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")).isTrue();
assertThat(pathMatcher.match("/**/test", "/bla/bla/test")).isTrue();
assertThat(pathMatcher.match("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla")).isTrue();
assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test")).isTrue();
assertThat(pathMatcher.match("/*bla/test", "/XXXbla/test")).isTrue();
assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test")).isFalse();
assertThat(pathMatcher.match("/*bla/test", "XXXblab/test")).isFalse();
assertThat(pathMatcher.match("/*bla/test", "XXXbl/test")).isFalse();

assertThat(pathMatcher.match("/????", "/bala/bla")).isFalse();
assertThat(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb")).isFalse();

assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
assertThat(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing")).isTrue();
assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg")).isTrue();

assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing")).isTrue();
assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing")).isFalse();

assertThat(pathMatcher.match("/x/x/**/bla", "/x/x/x/")).isFalse();

assertThat(pathMatcher.match("/foo/bar/**", "/foo/bar")).isTrue();

assertThat(pathMatcher.match("", "")).isTrue();

assertThat(pathMatcher.match("/{bla}.*", "/testing.html")).isTrue();
assertThat(pathMatcher.match("/{bla}", "//x\ny")).isTrue();

matchWithNullPath

assertThat(pathMatcher.match("/test", null)).isFalse();
assertThat(pathMatcher.match("/", null)).isFalse();
assertThat(pathMatcher.match(null, null)).isFalse();

matchWithTrimTokensEnabled

pathMatcher.setTrimTokens(true);

assertThat(pathMatcher.match("/foo/bar", "/foo /bar")).isTrue();

matchStart 

// test exact matching
assertThat(pathMatcher.matchStart("test", "test")).isTrue();
assertThat(pathMatcher.matchStart("/test", "/test")).isTrue();
assertThat(pathMatcher.matchStart("/test.jpg", "test.jpg")).isFalse();
assertThat(pathMatcher.matchStart("test", "/test")).isFalse();
assertThat(pathMatcher.matchStart("/test", "test")).isFalse();

// test matching with ?'s
assertThat(pathMatcher.matchStart("t?st", "test")).isTrue();
assertThat(pathMatcher.matchStart("??st", "test")).isTrue();
assertThat(pathMatcher.matchStart("tes?", "test")).isTrue();
assertThat(pathMatcher.matchStart("te??", "test")).isTrue();
assertThat(pathMatcher.matchStart("?es?", "test")).isTrue();
assertThat(pathMatcher.matchStart("tes?", "tes")).isFalse();
assertThat(pathMatcher.matchStart("tes?", "testt")).isFalse();
assertThat(pathMatcher.matchStart("tes?", "tsst")).isFalse();

// test matching with *'s
assertThat(pathMatcher.matchStart("*", "test")).isTrue();
assertThat(pathMatcher.matchStart("test*", "test")).isTrue();
assertThat(pathMatcher.matchStart("test*", "testTest")).isTrue();
assertThat(pathMatcher.matchStart("test/*", "test/Test")).isTrue();
assertThat(pathMatcher.matchStart("test/*", "test/t")).isTrue();
assertThat(pathMatcher.matchStart("test/*", "test/")).isTrue();
assertThat(pathMatcher.matchStart("*test*", "AnothertestTest")).isTrue();
assertThat(pathMatcher.matchStart("*test", "Anothertest")).isTrue();
assertThat(pathMatcher.matchStart("*.*", "test.")).isTrue();
assertThat(pathMatcher.matchStart("*.*", "test.test")).isTrue();
assertThat(pathMatcher.matchStart("*.*", "test.test.test")).isTrue();
assertThat(pathMatcher.matchStart("test*aaa", "testblaaaa")).isTrue();
assertThat(pathMatcher.matchStart("test*", "tst")).isFalse();
assertThat(pathMatcher.matchStart("test*", "test/")).isFalse();
assertThat(pathMatcher.matchStart("test*", "tsttest")).isFalse();
assertThat(pathMatcher.matchStart("test*", "test/")).isFalse();
assertThat(pathMatcher.matchStart("test*", "test/t")).isFalse();
assertThat(pathMatcher.matchStart("test/*", "test")).isTrue();
assertThat(pathMatcher.matchStart("test/t*.txt", "test")).isTrue();
assertThat(pathMatcher.matchStart("*test*", "tsttst")).isFalse();
assertThat(pathMatcher.matchStart("*test", "tsttst")).isFalse();
assertThat(pathMatcher.matchStart("*.*", "tsttst")).isFalse();
assertThat(pathMatcher.matchStart("test*aaa", "test")).isFalse();
assertThat(pathMatcher.matchStart("test*aaa", "testblaaab")).isFalse();

// test matching with ?'s and /'s
assertThat(pathMatcher.matchStart("/?", "/a")).isTrue();
assertThat(pathMatcher.matchStart("/?/a", "/a/a")).isTrue();
assertThat(pathMatcher.matchStart("/a/?", "/a/b")).isTrue();
assertThat(pathMatcher.matchStart("/??/a", "/aa/a")).isTrue();
assertThat(pathMatcher.matchStart("/a/??", "/a/bb")).isTrue();
assertThat(pathMatcher.matchStart("/?", "/a")).isTrue();

// test matching with **'s
assertThat(pathMatcher.matchStart("/**", "/testing/testing")).isTrue();
assertThat(pathMatcher.matchStart("/*/**", "/testing/testing")).isTrue();
assertThat(pathMatcher.matchStart("/**/*", "/testing/testing")).isTrue();
assertThat(pathMatcher.matchStart("test*/**", "test/")).isTrue();
assertThat(pathMatcher.matchStart("test*/**", "test/t")).isTrue();
assertThat(pathMatcher.matchStart("/bla/**/bla", "/bla/testing/testing/bla")).isTrue();
assertThat(pathMatcher.matchStart("/bla/**/bla", "/bla/testing/testing/bla/bla")).isTrue();
assertThat(pathMatcher.matchStart("/**/test", "/bla/bla/test")).isTrue();
assertThat(pathMatcher.matchStart("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla")).isTrue();
assertThat(pathMatcher.matchStart("/bla*bla/test", "/blaXXXbla/test")).isTrue();
assertThat(pathMatcher.matchStart("/*bla/test", "/XXXbla/test")).isTrue();
assertThat(pathMatcher.matchStart("/bla*bla/test", "/blaXXXbl/test")).isFalse();
assertThat(pathMatcher.matchStart("/*bla/test", "XXXblab/test")).isFalse();
assertThat(pathMatcher.matchStart("/*bla/test", "XXXbl/test")).isFalse();

assertThat(pathMatcher.matchStart("/????", "/bala/bla")).isFalse();
assertThat(pathMatcher.matchStart("/**/*bla", "/bla/bla/bla/bbb")).isTrue();

assertThat(pathMatcher.matchStart("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
assertThat(pathMatcher.matchStart("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing")).isTrue();
assertThat(pathMatcher.matchStart("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
assertThat(pathMatcher.matchStart("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg")).isTrue();

assertThat(pathMatcher.matchStart("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
assertThat(pathMatcher.matchStart("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing")).isTrue();
assertThat(pathMatcher.matchStart("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
assertThat(pathMatcher.matchStart("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();

assertThat(pathMatcher.matchStart("/x/x/**/bla", "/x/x/x/")).isTrue();

assertThat(pathMatcher.matchStart("", "")).isTrue();

uniqueDeliminator

pathMatcher.setPathSeparator(".");

// test exact matching
assertThat(pathMatcher.match("test", "test")).isTrue();
assertThat(pathMatcher.match(".test", ".test")).isTrue();
assertThat(pathMatcher.match(".test/jpg", "test/jpg")).isFalse();
assertThat(pathMatcher.match("test", ".test")).isFalse();
assertThat(pathMatcher.match(".test", "test")).isFalse();

// test matching with ?'s
assertThat(pathMatcher.match("t?st", "test")).isTrue();
assertThat(pathMatcher.match("??st", "test")).isTrue();
assertThat(pathMatcher.match("tes?", "test")).isTrue();
assertThat(pathMatcher.match("te??", "test")).isTrue();
assertThat(pathMatcher.match("?es?", "test")).isTrue();
assertThat(pathMatcher.match("tes?", "tes")).isFalse();
assertThat(pathMatcher.match("tes?", "testt")).isFalse();
assertThat(pathMatcher.match("tes?", "tsst")).isFalse();

// test matching with *'s
assertThat(pathMatcher.match("*", "test")).isTrue();
assertThat(pathMatcher.match("test*", "test")).isTrue();
assertThat(pathMatcher.match("test*", "testTest")).isTrue();
assertThat(pathMatcher.match("*test*", "AnothertestTest")).isTrue();
assertThat(pathMatcher.match("*test", "Anothertest")).isTrue();
assertThat(pathMatcher.match("*/*", "test/")).isTrue();
assertThat(pathMatcher.match("*/*", "test/test")).isTrue();
assertThat(pathMatcher.match("*/*", "test/test/test")).isTrue();
assertThat(pathMatcher.match("test*aaa", "testblaaaa")).isTrue();
assertThat(pathMatcher.match("test*", "tst")).isFalse();
assertThat(pathMatcher.match("test*", "tsttest")).isFalse();
assertThat(pathMatcher.match("*test*", "tsttst")).isFalse();
assertThat(pathMatcher.match("*test", "tsttst")).isFalse();
assertThat(pathMatcher.match("*/*", "tsttst")).isFalse();
assertThat(pathMatcher.match("test*aaa", "test")).isFalse();
assertThat(pathMatcher.match("test*aaa", "testblaaab")).isFalse();

// test matching with ?'s and .'s
assertThat(pathMatcher.match(".?", ".a")).isTrue();
assertThat(pathMatcher.match(".?.a", ".a.a")).isTrue();
assertThat(pathMatcher.match(".a.?", ".a.b")).isTrue();
assertThat(pathMatcher.match(".??.a", ".aa.a")).isTrue();
assertThat(pathMatcher.match(".a.??", ".a.bb")).isTrue();
assertThat(pathMatcher.match(".?", ".a")).isTrue();

// test matching with **'s
assertThat(pathMatcher.match(".**", ".testing.testing")).isTrue();
assertThat(pathMatcher.match(".*.**", ".testing.testing")).isTrue();
assertThat(pathMatcher.match(".**.*", ".testing.testing")).isTrue();
assertThat(pathMatcher.match(".bla.**.bla", ".bla.testing.testing.bla")).isTrue();
assertThat(pathMatcher.match(".bla.**.bla", ".bla.testing.testing.bla.bla")).isTrue();
assertThat(pathMatcher.match(".**.test", ".bla.bla.test")).isTrue();
assertThat(pathMatcher.match(".bla.**.**.bla", ".bla.bla.bla.bla.bla.bla")).isTrue();
assertThat(pathMatcher.match(".bla*bla.test", ".blaXXXbla.test")).isTrue();
assertThat(pathMatcher.match(".*bla.test", ".XXXbla.test")).isTrue();
assertThat(pathMatcher.match(".bla*bla.test", ".blaXXXbl.test")).isFalse();
assertThat(pathMatcher.match(".*bla.test", "XXXblab.test")).isFalse();
assertThat(pathMatcher.match(".*bla.test", "XXXbl.test")).isFalse();
}

@Test
void extractPathWithinPattern() throws Exception {
assertThat(pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html")).isEqualTo("");

assertThat(pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit")).isEqualTo("cvs/commit");
assertThat(pathMatcher.extractPathWithinPattern("/docs/cvs/*.html", "/docs/cvs/commit.html")).isEqualTo("commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/docs/**", "/docs/cvs/commit")).isEqualTo("cvs/commit");
assertThat(pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/cvs/commit.html")).isEqualTo("cvs/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/commit.html")).isEqualTo("commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/*.html", "/commit.html")).isEqualTo("commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/*.html", "/docs/commit.html")).isEqualTo("docs/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("*.html", "/commit.html")).isEqualTo("/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("*.html", "/docs/commit.html")).isEqualTo("/docs/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("**/*.*", "/docs/commit.html")).isEqualTo("/docs/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("*", "/docs/commit.html")).isEqualTo("/docs/commit.html");
// SPR-10515
assertThat(pathMatcher.extractPathWithinPattern("**/commit.html", "/docs/cvs/other/commit.html")).isEqualTo("/docs/cvs/other/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/docs/**/commit.html", "/docs/cvs/other/commit.html")).isEqualTo("cvs/other/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/docs/**/**/**/**", "/docs/cvs/other/commit.html")).isEqualTo("cvs/other/commit.html");

assertThat(pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit")).isEqualTo("docs/cvs/commit");
assertThat(pathMatcher.extractPathWithinPattern("/docs/c?s/*.html", "/docs/cvs/commit.html")).isEqualTo("cvs/commit.html");
assertThat(pathMatcher.extractPathWithinPattern("/d?cs/**", "/docs/cvs/commit")).isEqualTo("docs/cvs/commit");
assertThat(pathMatcher.extractPathWithinPattern("/d?cs/**/*.html", "/docs/cvs/commit.html")).isEqualTo("docs/cvs/commit.html");

extractUriTemplateVariables

Map<String, String> result = pathMatcher.extractUriTemplateVariables("/hotels/{hotel}", "/hotels/1");
assertThat(result).isEqualTo(Collections.singletonMap("hotel", "1"));

result = pathMatcher.extractUriTemplateVariables("/h?tels/{hotel}", "/hotels/1");
assertThat(result).isEqualTo(Collections.singletonMap("hotel", "1"));

result = pathMatcher.extractUriTemplateVariables("/hotels/{hotel}/bookings/{booking}", "/hotels/1/bookings/2");
Map<String, String> expected = new LinkedHashMap<>();
expected.put("hotel", "1");
expected.put("booking", "2");
assertThat(result).isEqualTo(expected);

result = pathMatcher.extractUriTemplateVariables("/**/hotels/**/{hotel}", "/foo/hotels/bar/1");
assertThat(result).isEqualTo(Collections.singletonMap("hotel", "1"));

result = pathMatcher.extractUriTemplateVariables("/{page}.html", "/42.html");
assertThat(result).isEqualTo(Collections.singletonMap("page", "42"));

result = pathMatcher.extractUriTemplateVariables("/{page}.*", "/42.html");
assertThat(result).isEqualTo(Collections.singletonMap("page", "42"));

result = pathMatcher.extractUriTemplateVariables("/A-{B}-C", "/A-b-C");
assertThat(result).isEqualTo(Collections.singletonMap("B", "b"));

result = pathMatcher.extractUriTemplateVariables("/{name}.{extension}", "/test.html");
expected = new LinkedHashMap<>();
expected.put("name", "test");
expected.put("extension", "html");
assertThat(result).isEqualTo(expected);

extractUriTemplateVariablesRegex

Map<String, String> result = pathMatcher
        .extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar",
                "com.example-1.0.0.jar");
assertThat(result.get("symbolicName")).isEqualTo("com.example");
assertThat(result.get("version")).isEqualTo("1.0.0");

result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar",
        "com.example-sources-1.0.0.jar");
assertThat(result.get("symbolicName")).isEqualTo("com.example");
assertThat(result.get("version")).isEqualTo("1.0.0");

extractUriTemplateVarsRegexQualifiers

Map<String, String> result = pathMatcher.extractUriTemplateVariables(
        "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar",
        "com.example-sources-1.0.0.jar");
assertThat(result.get("symbolicName")).isEqualTo("com.example");
assertThat(result.get("version")).isEqualTo("1.0.0");

result = pathMatcher.extractUriTemplateVariables(
        "{symbolicName:[\\w\\.]+}-sources-{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar",
        "com.example-sources-1.0.0-20100220.jar");
assertThat(result.get("symbolicName")).isEqualTo("com.example");
assertThat(result.get("version")).isEqualTo("1.0.0");
assertThat(result.get("year")).isEqualTo("2010");
assertThat(result.get("month")).isEqualTo("02");
assertThat(result.get("day")).isEqualTo("20");

result = pathMatcher.extractUriTemplateVariables(
        "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.\\{\\}]+}.jar",
        "com.example-sources-1.0.0.{12}.jar");
assertThat(result.get("symbolicName")).isEqualTo("com.example");
assertThat(result.get("version")).isEqualTo("1.0.0.{12}");

extractUriTemplateVarsRegexCapturingGroups

assertThatIllegalArgumentException().isThrownBy(() ->
        pathMatcher.extractUriTemplateVariables("/web/{id:foo(bar)?}", "/web/foobar"))
    .withMessageContaining("The number of capturing groups in the pattern");

combine

assertThat(pathMatcher.combine(null, null)).isEqualTo("");
assertThat(pathMatcher.combine("/hotels", null)).isEqualTo("/hotels");
assertThat(pathMatcher.combine(null, "/hotels")).isEqualTo("/hotels");
assertThat(pathMatcher.combine("/hotels/*", "booking")).isEqualTo("/hotels/booking");
assertThat(pathMatcher.combine("/hotels/*", "/booking")).isEqualTo("/hotels/booking");
assertThat(pathMatcher.combine("/hotels/**", "booking")).isEqualTo("/hotels/**/booking");
assertThat(pathMatcher.combine("/hotels/**", "/booking")).isEqualTo("/hotels/**/booking");
assertThat(pathMatcher.combine("/hotels", "/booking")).isEqualTo("/hotels/booking");
assertThat(pathMatcher.combine("/hotels", "booking")).isEqualTo("/hotels/booking");
assertThat(pathMatcher.combine("/hotels/", "booking")).isEqualTo("/hotels/booking");
assertThat(pathMatcher.combine("/hotels/*", "{hotel}")).isEqualTo("/hotels/{hotel}");
assertThat(pathMatcher.combine("/hotels/**", "{hotel}")).isEqualTo("/hotels/**/{hotel}");
assertThat(pathMatcher.combine("/hotels", "{hotel}")).isEqualTo("/hotels/{hotel}");
assertThat(pathMatcher.combine("/hotels", "{hotel}.*")).isEqualTo("/hotels/{hotel}.*");
assertThat(pathMatcher.combine("/hotels/*/booking", "{booking}")).isEqualTo("/hotels/*/booking/{booking}");
assertThat(pathMatcher.combine("/*.html", "/hotel.html")).isEqualTo("/hotel.html");
assertThat(pathMatcher.combine("/*.html", "/hotel")).isEqualTo("/hotel.html");
assertThat(pathMatcher.combine("/*.html", "/hotel.*")).isEqualTo("/hotel.html");
assertThat(pathMatcher.combine("/**", "/*.html")).isEqualTo("/*.html");
assertThat(pathMatcher.combine("/*", "/*.html")).isEqualTo("/*.html");
assertThat(pathMatcher.combine("/*.*", "/*.html")).isEqualTo("/*.html");
// SPR-8858
assertThat(pathMatcher.combine("/{foo}", "/bar")).isEqualTo("/{foo}/bar");
// SPR-7970
assertThat(pathMatcher.combine("/user", "/user")).isEqualTo("/user/user");
// SPR-10062
assertThat(pathMatcher.combine("/{foo:.*[^0-9].*}", "/edit/")).isEqualTo("/{foo:.*[^0-9].*}/edit/");
// SPR-10554
assertThat(pathMatcher.combine("/1.0", "/foo/test")).isEqualTo("/1.0/foo/test");
// SPR-12975
assertThat(pathMatcher.combine("/", "/hotel")).isEqualTo("/hotel");
// SPR-12975
assertThat(pathMatcher.combine("/hotel/", "/booking")).isEqualTo("/hotel/booking");

 combineWithTwoFileExtensionPatterns

assertThatIllegalArgumentException().isThrownBy(() ->
        pathMatcher.combine("/*.html", "/*.txt"));

patternComparator

Comparator<String> comparator = pathMatcher.getPatternComparator("/hotels/new");

assertThat(comparator.compare(null, null)).isEqualTo(0);
assertThat(comparator.compare(null, "/hotels/new")).isEqualTo(1);
assertThat(comparator.compare("/hotels/new", null)).isEqualTo(-1);

assertThat(comparator.compare("/hotels/new", "/hotels/new")).isEqualTo(0);

assertThat(comparator.compare("/hotels/new", "/hotels/*")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/*", "/hotels/new")).isEqualTo(1);
assertThat(comparator.compare("/hotels/*", "/hotels/*")).isEqualTo(0);

assertThat(comparator.compare("/hotels/new", "/hotels/{hotel}")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/{hotel}", "/hotels/new")).isEqualTo(1);
assertThat(comparator.compare("/hotels/{hotel}", "/hotels/{hotel}")).isEqualTo(0);
assertThat(comparator.compare("/hotels/{hotel}/booking", "/hotels/{hotel}/bookings/{booking}")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/{hotel}/bookings/{booking}", "/hotels/{hotel}/booking")).isEqualTo(1);

// SPR-10550
assertThat(comparator.compare("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}", "/**")).isEqualTo(-1);
assertThat(comparator.compare("/**", "/hotels/{hotel}/bookings/{booking}/cutomers/{customer}")).isEqualTo(1);
assertThat(comparator.compare("/**", "/**")).isEqualTo(0);

assertThat(comparator.compare("/hotels/{hotel}", "/hotels/*")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/*", "/hotels/{hotel}")).isEqualTo(1);

assertThat(comparator.compare("/hotels/*", "/hotels/*/**")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/*/**", "/hotels/*")).isEqualTo(1);

assertThat(comparator.compare("/hotels/new", "/hotels/new.*")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/{hotel}", "/hotels/{hotel}.*")).isEqualTo(2);

// SPR-6741
assertThat(comparator.compare("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}", "/hotels/**")).isEqualTo(-1);
assertThat(comparator.compare("/hotels/**", "/hotels/{hotel}/bookings/{booking}/cutomers/{customer}")).isEqualTo(1);
assertThat(comparator.compare("/hotels/foo/bar/**", "/hotels/{hotel}")).isEqualTo(1);
assertThat(comparator.compare("/hotels/{hotel}", "/hotels/foo/bar/**")).isEqualTo(-1);

// gh-23125
assertThat(comparator.compare("/hotels/*/bookings/**", "/hotels/**")).isEqualTo(-11);

// SPR-8683
assertThat(comparator.compare("/**", "/hotels/{hotel}")).isEqualTo(1);

// longer is better
assertThat(comparator.compare("/hotels", "/hotels2")).isEqualTo(1);

// SPR-13139
assertThat(comparator.compare("*", "*/**")).isEqualTo(-1);
assertThat(comparator.compare("*/**", "*")).isEqualTo(1);

 patternComparatorSort

Comparator<String> comparator = pathMatcher.getPatternComparator("/hotels/new");
List<String> paths = new ArrayList<>(3);

paths.add(null);
paths.add("/hotels/new");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isNull();
paths.clear();

paths.add("/hotels/new");
paths.add(null);
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isNull();
paths.clear();

paths.add("/hotels/*");
paths.add("/hotels/new");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isEqualTo("/hotels/*");
paths.clear();

paths.add("/hotels/new");
paths.add("/hotels/*");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isEqualTo("/hotels/*");
paths.clear();

paths.add("/hotels/**");
paths.add("/hotels/*");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/*");
assertThat(paths.get(1)).isEqualTo("/hotels/**");
paths.clear();

paths.add("/hotels/*");
paths.add("/hotels/**");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/*");
assertThat(paths.get(1)).isEqualTo("/hotels/**");
paths.clear();

paths.add("/hotels/{hotel}");
paths.add("/hotels/new");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isEqualTo("/hotels/{hotel}");
paths.clear();

paths.add("/hotels/new");
paths.add("/hotels/{hotel}");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isEqualTo("/hotels/{hotel}");
paths.clear();

paths.add("/hotels/*");
paths.add("/hotels/{hotel}");
paths.add("/hotels/new");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new");
assertThat(paths.get(1)).isEqualTo("/hotels/{hotel}");
assertThat(paths.get(2)).isEqualTo("/hotels/*");
paths.clear();

paths.add("/hotels/ne*");
paths.add("/hotels/n*");
Collections.shuffle(paths);
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/ne*");
assertThat(paths.get(1)).isEqualTo("/hotels/n*");
paths.clear();

comparator = pathMatcher.getPatternComparator("/hotels/new.html");
paths.add("/hotels/new.*");
paths.add("/hotels/{hotel}");
Collections.shuffle(paths);
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/hotels/new.*");
assertThat(paths.get(1)).isEqualTo("/hotels/{hotel}");
paths.clear();

comparator = pathMatcher.getPatternComparator("/web/endUser/action/login.html");
paths.add("/**/login.*");
paths.add("/**/endUser/action/login.*");
paths.sort(comparator);
assertThat(paths.get(0)).isEqualTo("/**/endUser/action/login.*");
assertThat(paths.get(1)).isEqualTo("/**/login.*");
paths.clear();

trimTokensOff

pathMatcher.setTrimTokens(false);

assertThat(pathMatcher.match("/group/{groupName}/members", "/group/sales/members")).isTrue();
assertThat(pathMatcher.match("/group/{groupName}/members", "/group/  sales/members")).isTrue();
assertThat(pathMatcher.match("/group/{groupName}/members", "/Group/  Sales/Members")).isFalse();

caseInsensitive

pathMatcher.setCaseSensitive(false);

assertThat(pathMatcher.match("/group/{groupName}/members", "/group/sales/members")).isTrue();
assertThat(pathMatcher.match("/group/{groupName}/members", "/Group/Sales/Members")).isTrue();
assertThat(pathMatcher.match("/Group/{groupName}/Members", "/group/Sales/members")).isTrue();

defaultCacheSetting

assertThat(pathMatcher.stringMatcherCache.size() > 20).isTrue();

for (int i = 0; i < 65536; i++) {
    pathMatcher.match("test" + i, "test");
}
// Cache turned off because it went beyond the threshold
assertThat(pathMatcher.stringMatcherCache.isEmpty()).isTrue();

 cachePatternsSetToTrue

pathMatcher.setCachePatterns(true);
match();
assertThat(pathMatcher.stringMatcherCache.size() > 20).isTrue();

for (int i = 0; i < 65536; i++) {
    pathMatcher.match("test" + i, "test" + i);
}
// Cache keeps being alive due to the explicit cache setting
assertThat(pathMatcher.stringMatcherCache.size() > 65536).isTrue();

 cachePatternsSetToFalse

pathMatcher.setCachePatterns(false);
match();
assertThat(pathMatcher.stringMatcherCache.isEmpty()).isTrue();

CreatingStringMatchers

preventCreatingStringMatchersIfPathDoesNotStartsWithPatternPrefix

pathMatcher.setCachePatterns(true);
assertThat(pathMatcher.stringMatcherCache.size()).isEqualTo(0);

pathMatcher.match("test?", "test");
assertThat(pathMatcher.stringMatcherCache.size()).isEqualTo(1);

pathMatcher.match("test?", "best");
pathMatcher.match("test/*", "view/test.jpg");
pathMatcher.match("test/**/test.jpg", "view/test.jpg");
pathMatcher.match("test/{name}.jpg", "view/test.jpg");
assertThat(pathMatcher.stringMatcherCache.size()).isEqualTo(1);

 creatingStringMatchersIfPatternPrefixCannotDetermineIfPathMatch

pathMatcher.setCachePatterns(true);
assertThat(pathMatcher.stringMatcherCache.size()).isEqualTo(0);

pathMatcher.match("test", "testian");
pathMatcher.match("test?", "testFf");
pathMatcher.match("test/*", "test/dir/name.jpg");
pathMatcher.match("test/{name}.jpg", "test/lorem.jpg");
pathMatcher.match("bla/**/test.jpg", "bla/test.jpg");
pathMatcher.match("**/{name}.jpg", "test/lorem.jpg");
pathMatcher.match("/**/{name}.jpg", "/test/lorem.jpg");
pathMatcher.match("/*/dir/{name}.jpg", "/*/dir/lorem.jpg");

assertThat(pathMatcher.stringMatcherCache.size()).isEqualTo(7);

extensionMappingWithDotPathSeparator

pathMatcher.setPathSeparator(".");
assertThat(pathMatcher.combine("/*.html", "hotel.*")).as("Extension mapping should be disabled with \".\" as path separator").isEqualTo("/*.html.hotel.*");

isPattern

assertThat(pathMatcher.isPattern("/test/*")).isTrue();
assertThat(pathMatcher.isPattern("/test/**/name")).isTrue();
assertThat(pathMatcher.isPattern("/test?")).isTrue();
assertThat(pathMatcher.isPattern("/test/{name}")).isTrue();

assertThat(pathMatcher.isPattern("/test/name")).isFalse();
assertThat(pathMatcher.isPattern("/test/foo{bar")).isFalse();

 isPatternWithNullPath

assertThat(pathMatcher.isPattern(null)).isFalse();
1

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录