1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.enforcer.rules;
20
21 import java.util.Collections;
22
23 import org.apache.maven.enforcer.rule.api.EnforcerLogger;
24 import org.apache.maven.model.Dependency;
25 import org.apache.maven.model.DependencyManagement;
26 import org.apache.maven.project.MavenProject;
27 import org.hamcrest.MatcherAssert;
28 import org.hamcrest.Matchers;
29 import org.junit.jupiter.api.Test;
30
31 import static org.mockito.Mockito.mock;
32
33 class BanDependencyManagementScopeTest {
34 @Test
35 void testGetViolatingDependencies() {
36 BanDependencyManagementScope rule = new BanDependencyManagementScope(new MavenProject());
37 DependencyManagement depMgmt = new DependencyManagement();
38 Dependency depWithoutScope = createDependency("myGroup", "artifact1", null);
39 Dependency depWithScope = createDependency("myGroup", "artifact2", "1.1.0", "provided");
40 Dependency depWithImportScope = createDependency("myGroup", "artifact3", "1.1.0", "import");
41 Dependency excludedDepWithScope = createDependency("myGroup", "artifact4", "1.1.0", "provided");
42 depMgmt.addDependency(depWithoutScope);
43 depMgmt.addDependency(depWithScope);
44 depMgmt.addDependency(depWithImportScope);
45 depMgmt.addDependency(excludedDepWithScope);
46 rule.setExcludes(Collections.singletonList("*:artifact4"));
47 rule.setLog(mock(EnforcerLogger.class));
48 MatcherAssert.assertThat(rule.getViolatingDependencies(depMgmt), Matchers.contains(depWithScope));
49 }
50
51 static Dependency createDependency(String groupId, String artifactId, String version) {
52 return createDependency(groupId, artifactId, version, null);
53 }
54
55 static Dependency createDependency(String groupId, String artifactId, String version, String scope) {
56 Dependency dependency = new Dependency();
57 dependency.setGroupId(groupId);
58 dependency.setArtifactId(artifactId);
59 if (version != null) {
60 dependency.setVersion(version);
61 }
62 if (scope != null) {
63 dependency.setScope(scope);
64 }
65 return dependency;
66 }
67 }