Skip to content

Commit

Permalink
WW-5309 Supports patterns starting with variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed May 16, 2023
1 parent 92705b9 commit 1df2f0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public CompiledPattern compilePattern(String data) {
int s = 0;
while (s < len) {
int e = data.indexOf('{', s);
if (e < 0 && data.indexOf('}') > -1) {
throw new IllegalArgumentException("Missing openning '{' in [" + data + "]!");
if (e < 0 && data.indexOf('}', s) > -1) {
throw new IllegalArgumentException("Missing opening '{' in [" + data + "]!");
}
if (e < 0) {
regex.append(Pattern.quote(data.substring(s)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public void testCompile() {
assertEquals("bob", pattern.getVariableNames().get(1));
assertTrue(pattern.getPattern().matcher("foostar/jie").matches());
assertFalse(pattern.getPattern().matcher("foo/star/jie").matches());

pattern = matcher.compilePattern("{urlLocale}/eula_cz");
assertEquals("([^/]+)\\Q/eula_cz\\E", pattern.getPattern().pattern());
assertEquals("urlLocale", pattern.getVariableNames().get(0));
assertTrue(pattern.getPattern().matcher("foostar/eula_cz").matches());
assertFalse(pattern.getPattern().matcher("foo/star/eula_cz").matches());

pattern = matcher.compilePattern("{test1}/path/{test2}");
assertEquals("([^/]+)\\Q/path/\\E([^/]+)", pattern.getPattern().pattern());
assertEquals("test1", pattern.getVariableNames().get(0));
assertEquals("test2", pattern.getVariableNames().get(1));
assertTrue(pattern.getPattern().matcher("test1/path/test2").matches());
assertFalse(pattern.getPattern().matcher("test/1/path/test2").matches());

pattern = matcher.compilePattern("path1/{test1}/path2/{test2}");
assertEquals("\\Qpath1/\\E([^/]+)\\Q/path2/\\E([^/]+)", pattern.getPattern().pattern());
assertEquals("test1", pattern.getVariableNames().get(0));
assertEquals("test2", pattern.getVariableNames().get(1));
assertTrue(pattern.getPattern().matcher("path1/test1/path2/test2").matches());
assertFalse(pattern.getPattern().matcher("path1/test/1/path2/test2").matches());
}

@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit 1df2f0a

Please sign in to comment.