Skip to content

Commit

Permalink
add alternatives to package matcher
Browse files Browse the repository at this point in the history
Issue: TNG#662
Signed-off-by: e.solutions <[email protected]>
on-behalf-of: @e-solutions-GmbH <[email protected]>
  • Loading branch information
Pfoerd committed Jun 22, 2022
1 parent ea7133f commit 35cc0e6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
* <li><b>{@code '*.*.pack*..'}</b> matches <b>{@code 'a.b.packfix.c.d'}</b>,
* but neither <b>{@code 'a.packfix.b'}</b> nor <b>{@code 'a.b.prepack.d'}</b></li>
* </ul>
* You can also use alternations with the '|' operator. For example
* <ul>
* <li><b>{@code 'pack.[a.c|b*].d'}</b> matches <b>{@code 'pack.a.c.d'} or <b>{@code 'pack.bar.d'}</b>, but neither
* <b>{@code 'pack.a.d'}</b> nor <b>{@code 'pack.b.c.d'}</b></li>
* </ul>
* <p>
* Furthermore, the use of capturing groups is supported. In this case '(*)' matches any sequence of characters,
* but not the dot '.', while '(**)' matches any sequence including the dot. <br>
* For example
Expand All @@ -49,6 +55,7 @@
* <li><b>{@code '..service.(**)'}</b> matches <b>{@code 'a.service.hello.more'}</b> and group 1 would be <b>{@code 'hello.more'}</b></li>
* <li><b>{@code 'my.(*)..service.(**)'}</b> matches <b>{@code 'my.company.some.service.hello.more'}</b>
* and group 1 would be <b>{@code 'company'}</b>, while group 2 would be <b>{@code 'hello.more'}</b></li>
* <li><b>{@code '..service.(a|b*)..'}</b> matches <b>{@code 'a.service.bar.more'}</b> and group 1 would be <b>{@code 'bar'}</b></li>
* </ul>
* Create via {@link PackageMatcher#of(String) PackageMatcher.of(packageIdentifier)}
*/
Expand All @@ -62,7 +69,7 @@ public final class PackageMatcher {
private static final String TWO_STAR_CAPTURE_REGEX = "(\\w+(?:\\.\\w+)*)";
static final String TWO_STAR_REGEX_MARKER = "#%#%#";

private static final Set<Character> PACKAGE_CONTROL_SYMBOLS = ImmutableSet.of('*', '(', ')', '.');
private static final Set<Character> PACKAGE_CONTROL_SYMBOLS = ImmutableSet.of('*', '(', ')', '.', '|', '[', ']');

private final String packageIdentifier;
private final Pattern packagePattern;
Expand All @@ -84,6 +91,10 @@ private void validate(String packageIdentifier) {
if (packageIdentifier.contains("(..)")) {
throw new IllegalArgumentException("Package Identifier does not support capturing via (..), use (**) instead");
}
if (Pattern.compile("([\\)\\]]\\|)|(\\|[\\(\\[])").matcher(packageIdentifier).find()) {
throw new IllegalArgumentException("Package Identifier does neither support nested alternations nor " +
"alternation of capturing and non capturing alternatives");
}
validateCharacters(packageIdentifier);
}

Expand All @@ -100,6 +111,7 @@ private void validateCharacters(String packageIdentifier) {

private String convertToRegex(String packageIdentifier) {
return packageIdentifier.
replaceAll("\\[([\\w.*]*)((?:\\|[\\w.*]*)+)]", "(?:$1$2)"). // replacing all ' [..|..]' with '(?:..|..)'
replace(TWO_STAR_CAPTURE_LITERAL, TWO_STAR_REGEX_MARKER).
replace("*", "\\w+").
replace(".", "\\.").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,40 @@ public class PackageMatcherTest {

@Test
@DataProvider(value = {
"some.arbitrary.pkg | some.arbitrary.pkg | true",
"some.arbitrary.pkg | some.thing.different | false",
"some..pkg | some.arbitrary.pkg | true",
"some..middle..pkg | some.arbitrary.middle.more.pkg | true",
"*..pkg | some.arbitrary.pkg | true",
"some..* | some.arbitrary.pkg | true",
"*..pkg | some.arbitrary.pkg.toomuch | false",
"toomuch.some..* | some.arbitrary.pkg | false",
"*..wrong | some.arbitrary.pkg | false",
"some..* | wrong.arbitrary.pkg | false",
"..some | some | true",
"some.. | some | true",
"*..some | some | false",
"some..* | some | false",
"..some | asome | false",
"some.. | somea | false",
"*.*.* | wrong.arbitrary.pkg | true",
"*.*.* | wrong.arbitrary.pkg.toomuch | false",
"some.arbi*.pk*.. | some.arbitrary.pkg.whatever | true",
"some.arbi*.. | some.brbitrary.pkg | false",
"some.*rary.*kg.. | some.arbitrary.pkg.whatever | true",
"some.*rary.. | some.arbitrarz.pkg | false",
"some.pkg | someepkg | false",
"..pkg.. | some.random.pkg.maybe.anywhere | true",
"..p.. | s.r.p.m.a | true",
"*..pkg..* | some.random.pkg.maybe.anywhere | true",
"*..p..* | s.r.p.m.a | true"
}, splitBy = "\\|")
"some.arbitrary.pkg , some.arbitrary.pkg , true",
"some.arbitrary.pkg , some.thing.different , false",
"some..pkg , some.arbitrary.pkg , true",
"some..middle..pkg , some.arbitrary.middle.more.pkg , true",
"*..pkg , some.arbitrary.pkg , true",
"some..* , some.arbitrary.pkg , true",
"*..pkg , some.arbitrary.pkg.toomuch , false",
"toomuch.some..* , some.arbitrary.pkg , false",
"*..wrong , some.arbitrary.pkg , false",
"some..* , wrong.arbitrary.pkg , false",
"..some , some , true",
"some.. , some , true",
"*..some , some , false",
"some..* , some , false",
"..some , asome , false",
"some.. , somea , false",
"*.*.* , wrong.arbitrary.pkg , true",
"*.*.* , wrong.arbitrary.pkg.toomuch , false",
"some.arbi*.pk*.. , some.arbitrary.pkg.whatever , true",
"some.arbi*.. , some.brbitrary.pkg , false",
"some.*rary.*kg.. , some.arbitrary.pkg.whatever , true",
"some.*rary.. , some.arbitrarz.pkg , false",
"some.pkg , someepkg , false",
"..pkg.. , some.random.pkg.maybe.anywhere , true",
"..p.. , s.r.p.m.a , true",
"*..pkg..* , some.random.pkg.maybe.anywhere , true",
"*..p..* , s.r.p.m.a , true",
"..[a|b|c].pk*.. , some.a.pkg.whatever , true",
"..[c|b].pk*.. , some.a.pkg.whatever , false",
"..[a|b*].pk*.. , some.bitrary.pkg.whatever , true",
"..[a|b*].pk*.. , some.a.pkg.whatever , true",
"..[a|b*].pk*.. , some.arbitrary.pkg.whatever , false",
"..[*c|d*].pk*.. , some.arbitrary.pkg.whatever , false",
})
public void match(String matcher, String target, boolean matches) {
assertThat(PackageMatcher.of(matcher).matches(target))
.as("package matches")
Expand All @@ -56,26 +62,30 @@ public void match(String matcher, String target, boolean matches) {

@Test
@DataProvider(value = {
"some.(*).pkg | some.arbitrary.pkg | arbitrary",
"some.arb(*)ry.pkg | some.arbitrary.pkg | itra",
"some.arb(*)ry.pkg | some.arbit.rary.pkg | null",
"some.(*).matches.(*).pkg | some.first.matches.second.pkg | first:second",
"(*).matches.(*) | start.matches.end | start:end",
"(*).(*).(*).(*) | a.b.c.d | a:b:c:d",
"(*) | some | some",
"some.(*).pkg | some.in.between.pkg | null",
"some.(**).pkg | some.in.between.pkg | in.between",
"some.(**).pkg.(*) | some.in.between.pkg.addon | in.between:addon",
"some(**)pkg | somerandom.in.between.longpkg | random.in.between.long",
"some.(**).pkg | somer.in.between.pkg | null",
"some.(**).pkg | some.in.between.gpkg | null",
"so(*)me.(**)pkg.an(*).more | soinfme.in.between.gpkg.and.more | inf:in.between.g:d",
"so(*)me.(**)pkg.an(*).more | soinfme.in.between.gpkg.an.more | null",
"so(**)me | some | null",
"so(*)me | some | null",
"(**)so | awe.some.aso | awe.some.a",
"so(**) | soan.some.we | an.some.we",
}, splitBy = "\\|")
"some.(*).pkg , some.arbitrary.pkg , arbitrary",
"some.arb(*)ry.pkg , some.arbitrary.pkg , itra",
"some.arb(*)ry.pkg , some.arbit.rary.pkg , null",
"some.(*).matches.(*).pkg , some.first.matches.second.pkg , first:second",
"(*).matches.(*) , start.matches.end , start:end",
"(*).(*).(*).(*) , a.b.c.d , a:b:c:d",
"(*) , some , some",
"some.(*).pkg , some.in.between.pkg , null",
"some.(**).pkg , some.in.between.pkg , in.between",
"some.(**).pkg.(*) , some.in.between.pkg.addon , in.between:addon",
"some(**)pkg , somerandom.in.between.longpkg , random.in.between.long",
"some.(**).pkg , somer.in.between.pkg , null",
"some.(**).pkg , some.in.between.gpkg , null",
"so(*)me.(**)pkg.an(*).more , soinfme.in.between.gpkg.and.more , inf:in.between.g:d",
"so(*)me.(**)pkg.an(*).more , soinfme.in.between.gpkg.an.more , null",
"so(**)me , some , null",
"so(*)me , some , null",
"(**)so , awe.some.aso , awe.some.a",
"so(**) , soan.some.we , an.some.we",
"..(a|b).pk*.(c|d).. , some.a.pkg.d.whatever , a:d",
"..[a|b|c].pk*.(c|d).. , some.c.pkg.d.whatever , d",
"..(a|b*|cd).pk*.(**).end , some.bitrary.pkg.in.between.end, bitrary:in.between",
"..(a.b|c.d).pk* , some.a.b.pkg , a.b",
}, splitBy = ",")
public void capture_groups(String matcher, String target, String groupString) {
assertThat(PackageMatcher.of(matcher).match(target).isPresent())
.as("'%s' matching '%s'", matcher, target)
Expand Down Expand Up @@ -113,6 +123,24 @@ public void should_reject_capturing_with_two_dots() {
PackageMatcher.of("some.(..).package");
}

@Test
public void should_reject_nested_alternations() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Package Identifier does neither support nested alternations nor alternation of " +
"capturing and non capturing alternatives");

PackageMatcher.of("some.(a|b)|(c|d).package");
}

@Test
public void should_reject_alternation_of_capturing_and_non_capturing_alternatives() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Package Identifier does neither support nested alternations nor alternation of " +
"capturing and non capturing alternatives");

PackageMatcher.of("some.[a|(c)].package");
}

@Test
public void should_reject_illegal_characters() {
String illegalPackageIdentifier = "some" + PackageMatcher.TWO_STAR_REGEX_MARKER + "package";
Expand Down

0 comments on commit 35cc0e6

Please sign in to comment.