| 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.shared.dependency.analyzer; |
| 20 |
|
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.HashSet; |
| 23 |
import java.util.Set; |
| 24 |
|
| 25 |
import org.apache.maven.artifact.Artifact; |
| 26 |
import org.apache.maven.artifact.DefaultArtifact; |
| 27 |
import org.apache.maven.artifact.versioning.VersionRange; |
| 28 |
import org.junit.jupiter.api.Test; |
| 29 |
|
| 30 |
import static org.assertj.core.api.Assertions.assertThat; |
| 31 |
|
| 32 |
/** |
| 33 |
* Tests <code>ProjectDependencyAnalysis</code>. |
| 34 |
* |
| 35 |
* @author <a href="mailto:[email protected]">Mark Hobson</a> |
| 36 |
* @see ProjectDependencyAnalysis |
| 37 |
*/ |
| 38 |
class ProjectDependencyAnalysisTest { |
| 39 |
@Test |
| 40 |
void testConstructor() { |
| 41 |
Set<Artifact> usedDeclaredArtifacts = new HashSet<>(); |
| 42 |
Set<Artifact> usedUndeclaredArtifacts = new HashSet<>(); |
| 43 |
Set<Artifact> unusedDeclaredArtifacts = new HashSet<>(); |
| 44 |
Set<Artifact> testArtifactsWithNonTestScope = new HashSet<>(); |
| 45 |
|
| 46 |
ProjectDependencyAnalysis analysis = new ProjectDependencyAnalysis( |
| 47 |
usedDeclaredArtifacts, usedUndeclaredArtifacts, unusedDeclaredArtifacts, testArtifactsWithNonTestScope); |
| 48 |
|
| 49 |
assertThat(analysis.getUsedDeclaredArtifacts()).isEqualTo(usedDeclaredArtifacts); |
| 50 |
assertThat(analysis.getUsedUndeclaredArtifacts()).isEqualTo(usedUndeclaredArtifacts); |
| 51 |
assertThat(analysis.getUnusedDeclaredArtifacts()).isEqualTo(unusedDeclaredArtifacts); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
void ignoreNonCompileShouldFilterOnlyUnusedDeclare() { |
| 56 |
Artifact artifactCompile = aTestArtifact("test1", Artifact.SCOPE_COMPILE); |
| 57 |
Artifact artifactProvided = aTestArtifact("test2", Artifact.SCOPE_PROVIDED); |
| 58 |
Artifact artifactTest = aTestArtifact("test3", Artifact.SCOPE_TEST); |
| 59 |
|
| 60 |
ProjectDependencyAnalysis analysis = new ProjectDependencyAnalysis( |
| 61 |
asSet(artifactCompile, artifactProvided, artifactTest), |
| 62 |
asSet(artifactCompile, artifactProvided, artifactTest), |
| 63 |
asSet(artifactCompile, artifactProvided, artifactTest), |
| 64 |
asSet(artifactCompile, artifactProvided, artifactTest)); |
| 65 |
|
| 66 |
ProjectDependencyAnalysis compileOnlyAnalysis = analysis.ignoreNonCompile(); |
| 67 |
|
| 68 |
assertThat(compileOnlyAnalysis.getUsedDeclaredArtifacts()).hasSize(3); |
| 69 |
assertThat(compileOnlyAnalysis.getUsedUndeclaredArtifacts()).hasSize(3); |
| 70 |
|
| 71 |
assertThat(compileOnlyAnalysis.getUnusedDeclaredArtifacts()).hasSize(1).allSatisfy(a -> assertThat(a.getScope()) |
| 72 |
.isEqualTo(Artifact.SCOPE_COMPILE)); |
| 73 |
|
| 74 |
assertThat(compileOnlyAnalysis.getTestArtifactsWithNonTestScope()).hasSize(3); |
| 75 |
} |
| 76 |
|
| 77 |
private <T> Set<T> asSet(T... items) { |
| 78 |
return new HashSet<>(Arrays.asList(items)); |
| 79 |
} |
| 80 |
|
| 81 |
private Artifact aTestArtifact(String artifactId, String scope) { |
| 82 |
return new DefaultArtifact( |
| 83 |
"groupId", artifactId, VersionRange.createFromVersion("1.0"), scope, "jar", "", null); |
| 84 |
} |
| 85 |
} |