View Javadoc
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.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  }