Spring的AntPathMatcher与linux路径的匹配行为

时间:2011-02-22 15:29:33

标签: java regex spring ant

我从AntPathMatcher发现了这种奇怪的行为,我在下面的单元测试中将其分离出来:

@Test
    public void testAntPathMacherPotentiallyBrokenForNix() throws IOException {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        Resource resource = new ClassPathResource("properties/RESOURCE_TEST.properties"); // there's a RESOURCE_TEST.properties under a directory 'properties'
        String localPath = resource.getFile().getAbsolutePath();

        Assert.assertThat(antPathMatcher.match("*.properties", localPath), is(true));

        String nixPath = "/local/app/instances/properties/RESOURCE_TEST.properties";

        Assert.assertThat(antPathMatcher.match("*.properties", nixPath), is(true));
}

第二个断言失败了,但上述两个断言都不应该是真的吗?

我错过了什么? (不是我真的必须使用AntPathMatcher,我只是好奇)

2 个答案:

答案 0 :(得分:0)

我试图重现这一点,但是在Spring 3中,两个版本都失败了(我再也没有触及2.5: - ))。

原因:您无法使用通配符启动路径,需要使用根来锚定。

使用此模式:/**/*.properties,至少在Spring 3中,两个测试都会成功。

答案 1 :(得分:0)

试试这个:

antPathMatcher.setPathSeparator(System.getProperty("file.separator")) ;

相关问题