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.dependency;
20  
21  import java.util.Collections;
22  
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
24  import org.apache.maven.enforcer.rules.utils.DependencyNodeBuilder;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.plugin.testing.ArtifactStubFactory;
27  import org.apache.maven.project.MavenProject;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.extension.ExtendWith;
30  import org.mockito.InjectMocks;
31  import org.mockito.Mock;
32  import org.mockito.junit.jupiter.MockitoExtension;
33  
34  import static org.assertj.core.api.Assertions.assertThatCode;
35  import static org.mockito.ArgumentMatchers.anyList;
36  import static org.mockito.Mockito.when;
37  
38  /**
39   * The Class BannedDependenciesTest.
40   *
41   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
42   */
43  @ExtendWith(MockitoExtension.class)
44  class BannedDependenciesTest {
45  
46      private static final ArtifactStubFactory ARTIFACT_STUB_FACTORY = new ArtifactStubFactory();
47  
48      @Mock
49      private MavenProject project;
50  
51      @Mock
52      private MavenSession session;
53  
54      @Mock
55      private ResolverUtil resolverUtil;
56  
57      @InjectMocks
58      private BannedDependencies rule;
59  
60      @Test
61      void excludesDoNotUseTransitiveDependencies() throws Exception {
62          when(session.getCurrentProject()).thenReturn(project);
63          when(project.getDependencyArtifacts()).thenReturn(ARTIFACT_STUB_FACTORY.getScopedArtifacts());
64  
65          rule.setSearchTransitive(false);
66          rule.setExcludes(Collections.singletonList("*"));
67  
68          assertThatCode(rule::execute)
69                  .isInstanceOf(EnforcerRuleException.class)
70                  .hasMessageContaining("g:runtime:jar:1.0 <--- banned via the exclude/include list")
71                  .hasMessageContaining("g:compile:jar:1.0 <--- banned via the exclude/include list")
72                  .hasMessageContaining("g:provided:jar:1.0 <--- banned via the exclude/include list")
73                  .hasMessageContaining("g:test:jar:1.0 <--- banned via the exclude/include list")
74                  .hasMessageContaining("g:system:jar:1.0 <--- banned via the exclude/include list");
75      }
76  
77      @Test
78      void excludesAndIncludesDoNotUseTransitiveDependencies() throws Exception {
79          when(session.getCurrentProject()).thenReturn(project);
80          when(project.getDependencyArtifacts()).thenReturn(ARTIFACT_STUB_FACTORY.getScopedArtifacts());
81  
82          rule.setSearchTransitive(false);
83          rule.setExcludes(Collections.singletonList("*"));
84          rule.setIncludes(Collections.singletonList("g:compile"));
85  
86          assertThatCode(rule::execute)
87                  .isInstanceOf(EnforcerRuleException.class)
88                  .hasMessageContaining("g:runtime:jar:1.0 <--- banned via the exclude/include list")
89                  .hasMessageContaining("g:provided:jar:1.0 <--- banned via the exclude/include list")
90                  .hasMessageContaining("g:test:jar:1.0 <--- banned via the exclude/include list")
91                  .hasMessageContaining("g:system:jar:1.0 <--- banned via the exclude/include list")
92                  .hasMessageNotContaining("g:compile:jar:1.0");
93      }
94  
95      @Test
96      void excludesUseTransitiveDependencies() throws Exception {
97  
98          when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
99                  .thenReturn(new DependencyNodeBuilder()
100                         .withType(DependencyNodeBuilder.Type.POM)
101                         .withChildNode(new DependencyNodeBuilder()
102                                 .withArtifactId("childA")
103                                 .withVersion("1.0.0")
104                                 .withChildNode(new DependencyNodeBuilder()
105                                         .withType(DependencyNodeBuilder.Type.WAR)
106                                         .withArtifactId("childAA")
107                                         .withVersion("1.0.0-SNAPSHOT")
108                                         .build())
109                                 .withChildNode(new DependencyNodeBuilder()
110                                         .withType(DependencyNodeBuilder.Type.WAR)
111                                         .withArtifactId("childAB")
112                                         .withVersion("1.0.0-SNAPSHOT")
113                                         .build())
114                                 .build())
115                         .build());
116 
117         rule.setSearchTransitive(true);
118         rule.setExcludes(Collections.singletonList("*:*:*:war"));
119 
120         assertThatCode(rule::execute)
121                 .isInstanceOf(EnforcerRuleException.class)
122                 .hasMessageContaining(
123                         "default-group:childAA:war:classifier:1.0.0-SNAPSHOT <--- banned via the exclude/include list")
124                 .hasMessageContaining(
125                         "default-group:childAB:war:classifier:1.0.0-SNAPSHOT <--- banned via the exclude/include list");
126     }
127 
128     @Test
129     void excludesAndIncludesUseTransitiveDependencies() throws Exception {
130 
131         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
132                 .thenReturn(new DependencyNodeBuilder()
133                         .withType(DependencyNodeBuilder.Type.POM)
134                         .withChildNode(new DependencyNodeBuilder()
135                                 .withArtifactId("childA")
136                                 .withVersion("1.0.0")
137                                 .withChildNode(new DependencyNodeBuilder()
138                                         .withType(DependencyNodeBuilder.Type.WAR)
139                                         .withArtifactId("childAA")
140                                         .withVersion("1.0.0-SNAPSHOT")
141                                         .build())
142                                 .withChildNode(new DependencyNodeBuilder()
143                                         .withType(DependencyNodeBuilder.Type.WAR)
144                                         .withArtifactId("childAB")
145                                         .withVersion("1.0.0-SNAPSHOT")
146                                         .build())
147                                 .build())
148                         .build());
149 
150         rule.setSearchTransitive(true);
151         rule.setExcludes(Collections.singletonList("*:*:*:war"));
152         rule.setIncludes(Collections.singletonList("*:childAB:*:war"));
153 
154         assertThatCode(rule::execute)
155                 .isInstanceOf(EnforcerRuleException.class)
156                 .hasMessageContaining(
157                         "default-group:childAA:war:classifier:1.0.0-SNAPSHOT <--- banned via the exclude/include list")
158                 .hasMessageNotContaining("childAB");
159     }
160 
161     @Test
162     void invalidExcludeFormat() throws Exception {
163         when(session.getCurrentProject()).thenReturn(project);
164         when(project.getDependencyArtifacts()).thenReturn(ARTIFACT_STUB_FACTORY.getScopedArtifacts());
165 
166         rule.setSearchTransitive(false);
167         rule.setExcludes(Collections.singletonList("::::::::::"));
168 
169         assertThatCode(rule::execute).isInstanceOf(IllegalArgumentException.class);
170     }
171 
172     @Test
173     void invalidIncludeFormat() throws Exception {
174         when(session.getCurrentProject()).thenReturn(project);
175         when(project.getDependencyArtifacts()).thenReturn(ARTIFACT_STUB_FACTORY.getScopedArtifacts());
176 
177         rule.setSearchTransitive(false);
178         rule.setExcludes(Collections.singletonList("*"));
179         rule.setIncludes(Collections.singletonList("*:*:x:x:x:x:x:x:x:x:"));
180 
181         assertThatCode(rule::execute).isInstanceOf(IllegalArgumentException.class);
182     }
183 }