Skip to content

Commit ffba5a6

Browse files
committed
extra comments and tests based on review
1 parent 8b27e36 commit ffba5a6

File tree

6 files changed

+147
-23
lines changed

6 files changed

+147
-23
lines changed

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj
239239

240240
/**
241241
* By default, this goal uses the project itself as the root of the dependency tree.
242-
* With graphRoots, you can select a subtree of dependencies.
242+
* With graphRoots, you can select a subtree of dependencies based on groupId and artifactId.
243243
* After that, the general include/exclude filters can be applied.
244244
*
245245
* @since 3.10.0
@@ -504,8 +504,12 @@ private Set<Artifact> resolve(Set<org.eclipse.aether.artifact.Artifact> artifact
504504

505505
private Set<Artifact> collectArtifacts(MavenProject project) throws DependencyResolutionException {
506506
if (graphRoots == null || graphRoots.isEmpty()) {
507+
// artifact have already been resolved here due to
508+
// @Mojo(requiresDependencyResolution = ResolutionScope.TEST) on final Mojo
507509
return project.getArtifacts();
508510
} else {
511+
// MavenProject doesn't provide access to the graph of dependencies(only the direct dependencies)
512+
// Hence we need to re-resolve artifacts, but only for the matching graphnodes
509513
List<DependencyMatcher> filterMatchers =
510514
graphRoots.stream().map(GraphRootMatcher::new).collect(Collectors.toList());
511515

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRoot.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@
1818
*/
1919
package org.apache.maven.plugins.dependency.fromDependencies;
2020

21-
/**
22-
* All values are static, no expression, so matcher can use equals
23-
*/
2421
public class GraphRoot {
2522

2623
private String groupId;
2724

2825
private String artifactId;
2926

30-
private String type;
31-
3227
public String getGroupId() {
3328
return groupId;
3429
}
@@ -44,12 +39,4 @@ public String getArtifactId() {
4439
public void setArtifactId(String artifactId) {
4540
this.artifactId = artifactId;
4641
}
47-
48-
public String getType() {
49-
return type;
50-
}
51-
52-
public void setType(String type) {
53-
this.type = type;
54-
}
5542
}

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcher.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,21 @@
1818
*/
1919
package org.apache.maven.plugins.dependency.fromDependencies;
2020

21+
import java.util.Objects;
22+
2123
import org.apache.maven.model.Dependency;
2224

2325
class GraphRootMatcher implements DependencyMatcher {
2426

2527
private final GraphRoot graphRoot;
2628

2729
GraphRootMatcher(GraphRoot graphRoot) {
28-
this.graphRoot = graphRoot;
30+
this.graphRoot = Objects.requireNonNull(graphRoot);
2931
}
3032

3133
@Override
3234
public boolean matches(Dependency dependency) {
33-
return matches(graphRoot.getGroupId(), dependency.getGroupId())
34-
&& matches(graphRoot.getArtifactId(), dependency.getArtifactId())
35-
&& matches(graphRoot.getType(), dependency.getType());
36-
}
37-
38-
private static boolean matches(String filterValue, String nodeValue) {
39-
return filterValue == null || filterValue.equals(nodeValue);
35+
return Objects.equals(graphRoot.getGroupId(), dependency.getGroupId())
36+
&& Objects.equals(graphRoot.getArtifactId(), dependency.getArtifactId());
4037
}
4138
}

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ class OrDependencyMatcher implements DependencyMatcher {
2828
private Collection<DependencyMatcher> matchers;
2929

3030
OrDependencyMatcher(Collection<DependencyMatcher> matchers) {
31-
this.matchers = new ArrayList<>(matchers);
31+
this.matchers = new ArrayList<>(validate(matchers));
32+
}
33+
34+
private Collection<DependencyMatcher> validate(Collection<DependencyMatcher> matchers) {
35+
if (matchers == null || matchers.isEmpty()) {
36+
throw new IllegalArgumentException("There must be at least 1 dependencyMatcher");
37+
}
38+
return matchers;
3239
}
3340

3441
@Override
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.dependency.fromDependencies;
20+
21+
import java.util.stream.Stream;
22+
23+
import org.apache.maven.model.Dependency;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
import org.junit.jupiter.params.provider.NullSource;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
32+
class GraphRootMatcherTest {
33+
34+
@ParameterizedTest
35+
@NullSource
36+
void illegalArgument(GraphRoot root) {
37+
assertThatThrownBy(() -> new GraphRootMatcher(root)).isInstanceOf(NullPointerException.class);
38+
}
39+
40+
@ParameterizedTest
41+
@MethodSource
42+
void match(GraphRoot graphRoot, Dependency dependency, boolean result) {
43+
assertThat(new GraphRootMatcher(graphRoot).matches(dependency)).isEqualTo(result);
44+
}
45+
46+
static Stream<Arguments> match() {
47+
return Stream.of(
48+
Arguments.arguments(newGraphRoot("g", "a"), newDependency("g", "a"), true),
49+
Arguments.arguments(newGraphRoot("x", "a"), newDependency("g", "a"), false),
50+
Arguments.arguments(newGraphRoot("g", "x"), newDependency("g", "a"), false),
51+
Arguments.arguments(newGraphRoot("g", "a"), newDependency("x", "a"), false),
52+
Arguments.arguments(newGraphRoot("g", "a"), newDependency("g", "x"), false));
53+
}
54+
55+
private static GraphRoot newGraphRoot(String groupId, String artifactId) {
56+
GraphRoot graphRoot = new GraphRoot();
57+
graphRoot.setGroupId(groupId);
58+
graphRoot.setArtifactId(artifactId);
59+
return graphRoot;
60+
}
61+
62+
private static Dependency newDependency(String groupId, String artifactId) {
63+
Dependency dependency = new Dependency();
64+
dependency.setGroupId(groupId);
65+
dependency.setArtifactId(artifactId);
66+
return dependency;
67+
}
68+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.dependency.fromDependencies;
20+
21+
import java.util.Arrays;
22+
import java.util.Collection;
23+
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.CsvSource;
26+
import org.junit.jupiter.params.provider.NullAndEmptySource;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
30+
31+
class OrDependencyMatcherTest {
32+
33+
@ParameterizedTest
34+
@NullAndEmptySource
35+
void illegalArgument(Collection<DependencyMatcher> source) {
36+
assertThatThrownBy(() -> new OrDependencyMatcher(source))
37+
.hasMessage("There must be at least 1 dependencyMatcher");
38+
}
39+
40+
@ParameterizedTest
41+
@CsvSource({
42+
"true, true",
43+
"false, false",
44+
})
45+
void singleMatcher(boolean input, boolean result) {
46+
Collection<DependencyMatcher> matchers = Arrays.asList(d -> input);
47+
assertThat(new OrDependencyMatcher(matchers).matches(null)).isEqualTo(result);
48+
}
49+
50+
@ParameterizedTest
51+
@CsvSource({
52+
"true, true, true",
53+
"true, false, true",
54+
"false, true, true",
55+
"false, false, false",
56+
})
57+
void doubleMatcher(boolean input1, boolean input2, boolean result) {
58+
Collection<DependencyMatcher> matchers = Arrays.asList(d -> input1, d -> input2);
59+
assertThat(new OrDependencyMatcher(matchers).matches(null)).isEqualTo(result);
60+
}
61+
}

0 commit comments

Comments
 (0)