Skip to content

Commit

Permalink
enforcer-rules - excludedGavRegExs should not be required (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
trentjeff authored Dec 1, 2023
1 parent 6cc827a commit 5e64f84
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 5 deletions.
10 changes: 5 additions & 5 deletions maven-enforcer-rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ Here is an example pom.xml:
<helidonDependenciesRule>
<namespace>JAKARTA | JAVAX</namespace>
<!-- list of strings - can be used to exclude a package / group name from validation -->
<excludedGavRegExs>
<!-- for example only - we suggest not including this exclusion in your usage -->
<excludedGavRegEx>javax.servlet.*</excludedGavRegEx>
</excludedGavs>
</helidonJakartaDependenciesRule>
<!-- <excludedGavRegExs>-->
<!-- &lt;!&ndash; for example only - we suggest not including this exclusion in your usage &ndash;&gt;-->
<!-- <excludedGavRegEx>javax.servlet.*</excludedGavRegEx>-->
<!-- </excludedGavRegExs>-->
</helidonDependenciesRule>
</rules>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

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

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,8 +70,13 @@ public class HelidonDependenciesRule extends AbstractEnforcerRule {

@Override
public void execute() throws ViolationException {
if (this.excludedGavRegExs == null) {
this.excludedGavRegExs = List.of();
}

String namespace = checkNamespace(this.namespace);
List<Pattern> excludedGavRegExs = this.excludedGavRegExs.stream()
.filter(s -> !s.isEmpty())
.map(Pattern::compile)
.collect(Collectors.toList());
List<Artifact> artifacts = project.getArtifacts().stream()
Expand All @@ -86,6 +93,14 @@ public String toString() {
return String.format(getClass().getSimpleName() + "[namespace=%s, excludedGavRegExs=%s]", namespace, excludedGavRegExs);
}

void setProject(MavenProject project) {
this.project = Objects.requireNonNull(project);
}

void setExcludedGavRegExs(List<String> excludedGavRegExs) {
this.excludedGavRegExs = new ArrayList<>(Objects.requireNonNull(excludedGavRegExs));
}

static String checkNamespace(String namespace) {
if (namespace == null || namespace.isBlank()) {
return JAKARTA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,26 @@

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

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.Test;

import static io.helidon.build.maven.enforcer.rules.HelidonDependenciesRule.checkNamespace;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertThrows;

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

@Test
void emptyGavExceptions() throws Exception {
MavenProject project = new MavenProject();
Set<Artifact> artifacts =
Stream.of("javax.inject:javax.inject:1",
"javax.crypto:javax.crypto:1",
"javax.servlet:javax.servlet-api:2.1.0",
"other.servlet:other.servlet:1")
.map(DependencyIsValidCheck::toArtifact)
.collect(Collectors.toSet());
project.setArtifacts(artifacts);

HelidonDependenciesRule rule =
new HelidonDependenciesRule();
rule.setLog(new TestingLogger());
rule.setProject(project);

ViolationException e = assertThrows(ViolationException.class, rule::execute);
assertThat(e.violations().size(), is(2));

List<String> allowedList = new ArrayList<>();
allowedList.add("");
rule.setExcludedGavRegExs(allowedList);
e = assertThrows(ViolationException.class, rule::execute);
assertThat(e.violations().size(), is(2));
}

static class TestingLogger implements EnforcerLogger {
static final Logger LOGGER = Logger.getLogger(TestingLogger.class.getName());

@Override
public void warnOrError(CharSequence charSequence) {
LOGGER.warning(charSequence.toString());
}

@Override
public void warnOrError(Supplier<CharSequence> supplier) {
warnOrError(supplier.get());
}

@Override
public boolean isDebugEnabled() {
return LOGGER.isLoggable(Level.FINE);
}

@Override
public void debug(CharSequence charSequence) {
LOGGER.log(Level.FINE, charSequence.toString());
}

@Override
public void debug(Supplier<CharSequence> supplier) {
debug(supplier.get());
}

@Override
public boolean isInfoEnabled() {
return LOGGER.isLoggable(Level.INFO);
}

@Override
public void info(CharSequence charSequence) {
LOGGER.log(Level.INFO, charSequence.toString());
}

@Override
public void info(Supplier<CharSequence> supplier) {
info(supplier.get());
}

@Override
public boolean isWarnEnabled() {
return LOGGER.isLoggable(Level.WARNING);
}

@Override
public void warn(CharSequence charSequence) {
LOGGER.log(Level.WARNING, charSequence.toString());
}

@Override
public void warn(Supplier<CharSequence> supplier) {
warn(supplier.get());
}

@Override
public boolean isErrorEnabled() {
return LOGGER.isLoggable(Level.SEVERE);
}

@Override
public void error(CharSequence charSequence) {
LOGGER.log(Level.SEVERE, charSequence.toString());
}

@Override
public void error(Supplier<CharSequence> supplier) {
error(supplier.get());
}
}

}

0 comments on commit 5e64f84

Please sign in to comment.