|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2016 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.util.Arrays;
|
20 | 20 | import java.util.Collections;
|
21 |
| -import java.util.HashSet; |
| 21 | +import java.util.LinkedHashSet; |
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Set;
|
24 | 24 |
|
25 | 25 | import org.apache.maven.artifact.Artifact;
|
26 | 26 | import org.apache.maven.plugin.MojoExecutionException;
|
27 | 27 | import org.apache.maven.plugin.MojoFailureException;
|
| 28 | +import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; |
| 29 | +import org.apache.maven.shared.artifact.filter.collection.ScopeFilter; |
28 | 30 | import org.junit.Test;
|
29 | 31 |
|
30 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -64,27 +66,89 @@ public void filterGroupIdExactMatch() throws MojoExecutionException {
|
64 | 66 | assertThat(artifacts.iterator().next()).isSameAs(artifact);
|
65 | 67 | }
|
66 | 68 |
|
67 |
| - private Artifact createArtifact(String groupId, String artifactId) { |
| 69 | + @Test |
| 70 | + public void filterScopeKeepOrder() throws MojoExecutionException { |
| 71 | + TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo( |
| 72 | + Collections.<Exclude>emptyList(), "", "", |
| 73 | + new ScopeFilter(null, Artifact.SCOPE_SYSTEM)); |
| 74 | + Artifact one = createArtifact("com.foo", "one"); |
| 75 | + Artifact two = createArtifact("com.foo", "two", Artifact.SCOPE_SYSTEM); |
| 76 | + Artifact three = createArtifact("com.foo", "three", Artifact.SCOPE_RUNTIME); |
| 77 | + Set<Artifact> artifacts = mojo.filterDependencies(one, two, three); |
| 78 | + assertThat(artifacts).containsExactly(one, three); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void filterArtifactIdKeepOrder() throws MojoExecutionException { |
| 83 | + TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo( |
| 84 | + Collections.<Exclude>emptyList(), "", "one,three"); |
| 85 | + Artifact one = createArtifact("com.foo", "one"); |
| 86 | + Artifact two = createArtifact("com.foo", "two"); |
| 87 | + Artifact three = createArtifact("com.foo", "three"); |
| 88 | + Artifact four = createArtifact("com.foo", "four"); |
| 89 | + Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four); |
| 90 | + assertThat(artifacts).containsExactly(two, four); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void filterGroupIdKeepOrder() throws MojoExecutionException { |
| 95 | + TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo( |
| 96 | + Collections.<Exclude>emptyList(), "com.foo", ""); |
| 97 | + Artifact one = createArtifact("com.foo", "one"); |
| 98 | + Artifact two = createArtifact("com.bar", "two"); |
| 99 | + Artifact three = createArtifact("com.bar", "three"); |
| 100 | + Artifact four = createArtifact("com.foo", "four"); |
| 101 | + Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four); |
| 102 | + assertThat(artifacts).containsExactly(two, three); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void filterExcludeKeepOrder() throws MojoExecutionException { |
| 107 | + Exclude exclude = new Exclude(); |
| 108 | + exclude.setGroupId("com.bar"); |
| 109 | + exclude.setArtifactId("two"); |
| 110 | + TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo( |
| 111 | + Collections.singletonList(exclude), "", ""); |
| 112 | + Artifact one = createArtifact("com.foo", "one"); |
| 113 | + Artifact two = createArtifact("com.bar", "two"); |
| 114 | + Artifact three = createArtifact("com.bar", "three"); |
| 115 | + Artifact four = createArtifact("com.foo", "four"); |
| 116 | + Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four); |
| 117 | + assertThat(artifacts).containsExactly(one, three, four); |
| 118 | + } |
| 119 | + |
| 120 | + private static Artifact createArtifact(String groupId, String artifactId) { |
| 121 | + return createArtifact(groupId, artifactId, null); |
| 122 | + } |
| 123 | + |
| 124 | + private static Artifact createArtifact(String groupId, String artifactId, String scope) { |
68 | 125 | Artifact a = mock(Artifact.class);
|
69 | 126 | given(a.getGroupId()).willReturn(groupId);
|
70 | 127 | given(a.getArtifactId()).willReturn(artifactId);
|
| 128 | + if (scope != null) { |
| 129 | + given(a.getScope()).willReturn(scope); |
| 130 | + } |
71 | 131 | return a;
|
72 | 132 | }
|
73 | 133 |
|
74 | 134 | private static final class TestableDependencyFilterMojo
|
75 | 135 | extends AbstractDependencyFilterMojo {
|
76 | 136 |
|
| 137 | + private final ArtifactsFilter[] additionalFilters; |
| 138 | + |
77 | 139 | private TestableDependencyFilterMojo(List<Exclude> excludes,
|
78 |
| - String excludeGroupIds, String excludeArtifactIds) { |
| 140 | + String excludeGroupIds, String excludeArtifactIds, |
| 141 | + ArtifactsFilter... additionalFilters) { |
79 | 142 | setExcludes(excludes);
|
80 | 143 | setExcludeGroupIds(excludeGroupIds);
|
81 | 144 | setExcludeArtifactIds(excludeArtifactIds);
|
| 145 | + this.additionalFilters = additionalFilters; |
82 | 146 | }
|
83 | 147 |
|
84 | 148 | public Set<Artifact> filterDependencies(Artifact... artifacts)
|
85 | 149 | throws MojoExecutionException {
|
86 |
| - Set<Artifact> input = new HashSet<Artifact>(Arrays.asList(artifacts)); |
87 |
| - return filterDependencies(input, getFilters()); |
| 150 | + Set<Artifact> input = new LinkedHashSet<Artifact>(Arrays.asList(artifacts)); |
| 151 | + return filterDependencies(input, getFilters(this.additionalFilters)); |
88 | 152 | }
|
89 | 153 |
|
90 | 154 | @Override
|
|
0 commit comments