Skip to content

Commit 2ba5982

Browse files
committed
fix for issue-1002
1 parent 6cc827a commit 2ba5982

File tree

3 files changed

+134
-4
lines changed

3 files changed

+134
-4
lines changed

maven-enforcer-rules/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ Here is an example pom.xml:
3333
<helidonDependenciesRule>
3434
<namespace>JAKARTA | JAVAX</namespace>
3535
<!-- list of strings - can be used to exclude a package / group name from validation -->
36-
<excludedGavRegExs>
37-
<!-- for example only - we suggest not including this exclusion in your usage -->
38-
<excludedGavRegEx>javax.servlet.*</excludedGavRegEx>
36+
<!-- <excludedGavRegExs>-->
37+
<!-- &lt;!&ndash; for example only - we suggest not including this exclusion in your usage &ndash;&gt;-->
38+
<!-- <excludedGavRegEx>javax.servlet.*</excludedGavRegEx>-->
3939
</excludedGavs>
40-
</helidonJakartaDependenciesRule>
40+
</helidonDependenciesRule>
4141
</rules>
4242
</configuration>
4343
</execution>

maven-enforcer-rules/src/main/java/io/helidon/build/maven/enforcer/rules/HelidonDependenciesRule.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package io.helidon.build.maven.enforcer.rules;
1818

19+
import java.util.ArrayList;
1920
import java.util.List;
21+
import java.util.Objects;
2022
import java.util.regex.Pattern;
2123
import java.util.stream.Collectors;
2224

@@ -68,6 +70,10 @@ public class HelidonDependenciesRule extends AbstractEnforcerRule {
6870

6971
@Override
7072
public void execute() throws ViolationException {
73+
if (this.excludedGavRegExs == null) {
74+
this.excludedGavRegExs = List.of();
75+
}
76+
7177
String namespace = checkNamespace(this.namespace);
7278
List<Pattern> excludedGavRegExs = this.excludedGavRegExs.stream()
7379
.map(Pattern::compile)
@@ -86,6 +92,14 @@ public String toString() {
8692
return String.format(getClass().getSimpleName() + "[namespace=%s, excludedGavRegExs=%s]", namespace, excludedGavRegExs);
8793
}
8894

95+
void setProject(MavenProject project) {
96+
this.project = Objects.requireNonNull(project);
97+
}
98+
99+
void setExcludedGavRegExs(List<String> excludedGavRegExs) {
100+
this.excludedGavRegExs = new ArrayList<>(Objects.requireNonNull(excludedGavRegExs));
101+
}
102+
89103
static String checkNamespace(String namespace) {
90104
if (namespace == null || namespace.isBlank()) {
91105
return JAKARTA;

maven-enforcer-rules/src/test/java/io/helidon/build/maven/enforcer/rules/HelidonDependencyRuleTest.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,26 @@
1616

1717
package io.helidon.build.maven.enforcer.rules;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Set;
22+
import java.util.function.Supplier;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import java.util.stream.Collectors;
26+
import java.util.stream.Stream;
27+
28+
import org.apache.maven.artifact.Artifact;
29+
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
30+
import org.apache.maven.project.MavenProject;
1931
import org.junit.jupiter.api.Test;
2032

2133
import static io.helidon.build.maven.enforcer.rules.HelidonDependenciesRule.checkNamespace;
2234
import static org.hamcrest.CoreMatchers.equalTo;
35+
import static org.hamcrest.CoreMatchers.is;
2336
import static org.hamcrest.MatcherAssert.assertThat;
37+
import static org.hamcrest.Matchers.containsInAnyOrder;
38+
import static org.hamcrest.Matchers.startsWith;
2439
import static org.junit.jupiter.api.Assertions.assertThrows;
2540

2641
class HelidonDependencyRuleTest {
@@ -40,4 +55,105 @@ void testCheckNamespace() {
4055
equalTo("The namespace 'java' is invalid. Only valid namespace names are: 'jakarta' and 'javax'."));
4156
}
4257

58+
@Test
59+
void emptyGavExceptions() throws Exception {
60+
MavenProject project = new MavenProject();
61+
Set<Artifact> artifacts =
62+
Stream.of("javax.inject:javax.inject:1",
63+
"javax.crypto:javax.crypto:1",
64+
"javax.servlet:javax.servlet-api:2.1.0",
65+
"other.servlet:other.servlet:1")
66+
.map(DependencyIsValidCheck::toArtifact)
67+
.collect(Collectors.toSet());
68+
project.setArtifacts(artifacts);
69+
70+
HelidonDependenciesRule rule =
71+
new HelidonDependenciesRule();
72+
rule.setLog(new TestingLogger());
73+
rule.setProject(project);
74+
75+
ViolationException e = assertThrows(ViolationException.class, rule::execute);
76+
assertThat(e.violations().size(), is(2));
77+
78+
List<String> allowedList = new ArrayList<>();
79+
allowedList.add("");
80+
rule.setExcludedGavRegExs(allowedList);
81+
e = assertThrows(ViolationException.class, rule::execute);
82+
assertThat(e.violations().size(), is(2));
83+
}
84+
85+
static class TestingLogger implements EnforcerLogger {
86+
static final Logger LOGGER = Logger.getLogger(TestingLogger.class.getName());
87+
88+
@Override
89+
public void warnOrError(CharSequence charSequence) {
90+
LOGGER.warning(charSequence.toString());
91+
}
92+
93+
@Override
94+
public void warnOrError(Supplier<CharSequence> supplier) {
95+
warnOrError(supplier.get());
96+
}
97+
98+
@Override
99+
public boolean isDebugEnabled() {
100+
return LOGGER.isLoggable(Level.FINE);
101+
}
102+
103+
@Override
104+
public void debug(CharSequence charSequence) {
105+
LOGGER.log(Level.FINE, charSequence.toString());
106+
}
107+
108+
@Override
109+
public void debug(Supplier<CharSequence> supplier) {
110+
debug(supplier.get());
111+
}
112+
113+
@Override
114+
public boolean isInfoEnabled() {
115+
return LOGGER.isLoggable(Level.INFO);
116+
}
117+
118+
@Override
119+
public void info(CharSequence charSequence) {
120+
LOGGER.log(Level.INFO, charSequence.toString());
121+
}
122+
123+
@Override
124+
public void info(Supplier<CharSequence> supplier) {
125+
info(supplier.get());
126+
}
127+
128+
@Override
129+
public boolean isWarnEnabled() {
130+
return LOGGER.isLoggable(Level.WARNING);
131+
}
132+
133+
@Override
134+
public void warn(CharSequence charSequence) {
135+
LOGGER.log(Level.WARNING, charSequence.toString());
136+
}
137+
138+
@Override
139+
public void warn(Supplier<CharSequence> supplier) {
140+
warn(supplier.get());
141+
}
142+
143+
@Override
144+
public boolean isErrorEnabled() {
145+
return LOGGER.isLoggable(Level.SEVERE);
146+
}
147+
148+
@Override
149+
public void error(CharSequence charSequence) {
150+
LOGGER.log(Level.SEVERE, charSequence.toString());
151+
}
152+
153+
@Override
154+
public void error(Supplier<CharSequence> supplier) {
155+
error(supplier.get());
156+
}
157+
}
158+
43159
}

0 commit comments

Comments
 (0)