assertEquals(0, comparator.compare(parse("/hotels/new"), parse("/hotels/new"))); assertEquals(-1, comparator.compare(parse("/hotels/new"), parse("/hotels/*"))); assertEquals(1, comparator.compare(parse("/hotels/*"), parse("/hotels/new"))); assertEquals(0, comparator.compare(parse("/hotels/*"), parse("/hotels/*"))); comparator.compare(parse("/hotels/new"), parse("/hotels/{hotel}"))); assertEquals(1, comparator.compare(parse("/hotels/{hotel}"), parse("/hotels/new"))); assertEquals(0, comparator.compare(parse("/hotels/{hotel}"), parse("/hotels/{hotel}"))); assertEquals(-1, comparator.compare(parse("/hotels/{hotel}/booking"), parse("/hotels/{hotel}/bookings/{booking}"))); assertEquals(1, comparator.compare(parse("/hotels/{hotel}/bookings/{booking}"), parse("/hotels/{hotel}/booking"))); parse("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}"), parse("/**"))); assertEquals(1, comparator.compare(parse("/**"), parse("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}"))); assertEquals(0, comparator.compare(parse("/**"), parse("/**"))); comparator.compare(parse("/hotels/{hotel}"), parse("/hotels/*"))); assertEquals(1, comparator.compare(parse("/hotels/*"), parse("/hotels/{hotel}"))); assertEquals(-1, comparator.compare(parse("/hotels/*"), parse("/hotels/*/**"))); assertEquals(1, comparator.compare(parse("/hotels/*/**"), parse("/hotels/*"))); assertEquals(-1,
private PathPattern.PathMatchInfo matchAndExtract(String pattern, String path) { return parse(pattern).matchAndExtract(PathPatternTests.toPathContainer(path)); }
private PathRemainingMatchInfo getPathRemaining(String pattern, String path) { return parse(pattern).matchStartOfPath(toPathContainer(path)); }
assertNull(parse("/foo").matchStartOfPath(toPathContainer("/footastic/bar"))); assertNull(parse("/f?o").matchStartOfPath(toPathContainer("/footastic/bar"))); assertNull(parse("/f*o*p").matchStartOfPath(toPathContainer("/flooptastic/bar"))); assertNull(parse("/{abc}abc").matchStartOfPath(toPathContainer("/xyzabcbar/bar"))); assertNull(parse("/resource/**").matchStartOfPath(toPathContainer("/resourceX"))); assertEquals("",parse("/resource/**") assertNull(parse("/resource/{*foo}").matchStartOfPath(toPathContainer("/resourceX"))); assertEquals("", parse("/resource/{*foo}") PathPattern.PathRemainingMatchInfo pri = parse("/aaa/{bbb}/c?d/e*f/*/g") .matchStartOfPath(toPathContainer("/aaa/b/ccd/ef/x/g/i")); assertNotNull(pri); pri = parse("/aaa/{bbb}/c?d/e*f/*/g/").matchStartOfPath(toPathContainer("/aaa/b/ccd/ef/x/g/i")); assertNotNull(pri); assertEquals("i",pri.getPathRemaining().value()); assertEquals("b",pri.getUriVariables().get("bbb")); pri = parse("/{aaa}_{bbb}/e*f/{x}/g").matchStartOfPath(toPathContainer("/aa_bb/ef/x/g/i")); assertNotNull(pri); assertEquals("/i",pri.getPathRemaining().value()); assertNull(parse("/a/b").matchStartOfPath(toPathContainer(""))); assertEquals("/a/b",parse("").matchStartOfPath(toPathContainer("/a/b")).getPathRemaining().value()); assertEquals("",parse("").matchStartOfPath(toPathContainer("")).getPathRemaining().value());
@Test public void multipleSeparatorsInPattern() { PathPattern pp = parse("a//b//c"); assertEquals("Literal(a) Separator(/) Separator(/) Literal(b) Separator(/) Separator(/) Literal(c)", pp.toChainString()); assertMatches(pp,"a//b//c"); assertEquals("Literal(a) Separator(/) WildcardTheRest(/**)",parse("a//**").toChainString()); checkMatches("///abc", "///abc"); checkNoMatch("///abc", "/abc"); checkNoMatch("//", "/"); checkMatches("//", "//"); checkNoMatch("///abc//d/e", "/abc/d/e"); checkMatches("///abc//d/e", "///abc//d/e"); checkNoMatch("///abc//{def}//////xyz", "/abc/foo/xyz"); checkMatches("///abc//{def}//////xyz", "///abc//p//////xyz"); }
@Test public void pathRemainingEnhancements_spr15419() { PathPattern pp; PathPattern.PathRemainingMatchInfo pri; // It would be nice to partially match a path and get any bound variables in one step pp = parse("/{this}/{one}/{here}"); pri = getPathRemaining(pp, "/foo/bar/goo/boo"); assertEquals("/boo",pri.getPathRemaining().value()); assertEquals("foo",pri.getUriVariables().get("this")); assertEquals("bar",pri.getUriVariables().get("one")); assertEquals("goo",pri.getUriVariables().get("here")); pp = parse("/aaa/{foo}"); pri = getPathRemaining(pp, "/aaa/bbb"); assertEquals("",pri.getPathRemaining().value()); assertEquals("bbb",pri.getUriVariables().get("foo")); pp = parse("/aaa/bbb"); pri = getPathRemaining(pp, "/aaa/bbb"); assertEquals("",pri.getPathRemaining().value()); assertEquals(0,pri.getUriVariables().size()); pp = parse("/*/{foo}/b*"); pri = getPathRemaining(pp, "/foo"); assertNull(pri); pri = getPathRemaining(pp, "/abc/def/bhi"); assertEquals("",pri.getPathRemaining().value()); assertEquals("def",pri.getUriVariables().get("foo")); pri = getPathRemaining(pp, "/abc/def/bhi/jkl"); assertEquals("/jkl",pri.getPathRemaining().value()); assertEquals("def",pri.getUriVariables().get("foo")); }
@Test public void extractUriTemplateVariables() throws Exception { assertMatches(parse("{hotel}"),"1"); assertMatches(parse("/hotels/{hotel}"),"/hotels/1"); checkCapture("/hotels/{hotel}", "/hotels/1", "hotel", "1"); checkCapture("/h?tels/{hotel}", "/hotels/1", "hotel", "1"); checkCapture("/hotels/{hotel}/bookings/{booking}", "/hotels/1/bookings/2", "hotel", "1", "booking", "2"); checkCapture("/*/hotels/*/{hotel}", "/foo/hotels/bar/1", "hotel", "1"); checkCapture("/{page}.html", "/42.html", "page", "42"); checkNoMatch("/{var}","/"); checkCapture("/{page}.*", "/42.html", "page", "42"); checkCapture("/A-{B}-C", "/A-b-C", "B", "b"); checkCapture("/{name}.{extension}", "/test.html", "name", "test", "extension", "html"); assertNull(checkCapture("/{one}/", "//")); assertNull(checkCapture("", "/abc")); assertEquals(0, checkCapture("", "").getUriVariables().size()); checkCapture("{id}", "99", "id", "99"); checkCapture("/customer/{customerId}", "/customer/78", "customerId", "78"); checkCapture("/customer/{customerId}/banana", "/customer/42/banana", "customerId", "42"); checkCapture("{id}/{id2}", "99/98", "id", "99", "id2", "98"); checkCapture("/foo/{bar}/boo/{baz}", "/foo/plum/boo/apple", "bar", "plum", "baz", "apple"); checkCapture("/{bla}.*", "/testing.html", "bla", "testing"); PathPattern.PathMatchInfo extracted = checkCapture("/abc", "/abc"); assertEquals(0, extracted.getUriVariables().size()); checkCapture("/{bla}/foo","/a/foo"); }
pp = parse("/resource"); assertMatches(pp,"/resource"); assertMatches(pp,"/resource"); assertMatches(pp,"/resource/"); assertNoMatch(pp,"/resource//"); pp = parse("/resource/"); assertNoMatch(pp,"/resource"); assertMatches(pp,"/resource/"); assertNoMatch(pp,"/resource//"); pp = parse("res?urce"); assertNoMatch(pp,"resource//"); pp = parse("/res?urce"); assertMatches(pp,"/resource"); assertMatches(pp,"/resource/"); assertNoMatch(pp,"/resource//"); pp = parse("/res?urce/"); assertNoMatch(pp,"/resource"); assertMatches(pp,"/resource/"); pp = parse("/{var}"); assertMatches(pp,"/resource"); assertEquals("resource",pp.matchAndExtract(toPathContainer("/resource")).getUriVariables().get("var")); assertEquals("resource",pp.matchAndExtract(toPathContainer("/resource/")).getUriVariables().get("var")); assertNoMatch(pp,"/resource//"); pp = parse("/{var}/"); assertNoMatch(pp,"/resource");