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.io.IOException;
22  import java.util.Collections;
23  
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.enforcer.rules.utils.DependencyNodeBuilder;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.testing.ArtifactStubFactory;
28  import org.apache.maven.project.MavenProject;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  import org.mockito.InjectMocks;
33  import org.mockito.Mock;
34  import org.mockito.junit.jupiter.MockitoExtension;
35  
36  import static org.apache.maven.enforcer.rules.EnforcerTestUtils.getDependencyNodeWithMultipleSnapshots;
37  import static org.apache.maven.enforcer.rules.EnforcerTestUtils.getDependencyNodeWithMultipleTestSnapshots;
38  import static org.assertj.core.api.Assertions.assertThat;
39  import static org.assertj.core.api.Assertions.assertThatCode;
40  import static org.mockito.ArgumentMatchers.anyList;
41  import static org.mockito.Mockito.verifyNoInteractions;
42  import static org.mockito.Mockito.when;
43  
44  /**
45   * Unit tests for {@link RequireReleaseDeps}
46   *
47   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>, Andrzej Jarmoniuk
48   */
49  @ExtendWith(MockitoExtension.class)
50  class RequireReleaseDepsTest {
51      private static final ArtifactStubFactory ARTIFACT_STUB_FACTORY = new ArtifactStubFactory();
52  
53      @Mock
54      private MavenProject project;
55  
56      @Mock
57      private MavenSession session;
58  
59      @Mock
60      private ResolverUtil resolverUtil;
61  
62      @InjectMocks
63      private RequireReleaseDeps rule;
64  
65      @BeforeEach
66      void setUp() {
67          // ruleHelper = EnforcerTestUtils.getHelper(project);
68          // rule = new RequireReleaseDeps();
69      }
70  
71      @Test
72      void testSearchNonTransitive() throws IOException {
73          when(session.getCurrentProject()).thenReturn(project);
74          when(project.getDependencyArtifacts()).thenReturn(ARTIFACT_STUB_FACTORY.getScopedArtifacts());
75          rule.setSearchTransitive(false);
76  
77          assertThatCode(rule::execute).doesNotThrowAnyException();
78  
79          verifyNoInteractions(resolverUtil);
80      }
81  
82      @Test
83      void testSearchTransitiveMultipleFailures() throws Exception {
84          when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
85                  .thenReturn(getDependencyNodeWithMultipleSnapshots());
86          rule.setSearchTransitive(true);
87  
88          assertThatCode(rule::execute)
89                  .isInstanceOf(EnforcerRuleException.class)
90                  .hasMessageContaining(
91                          "default-group:childAA:jar:classifier:1.0.0-SNAPSHOT <--- is not a release dependency")
92                  .hasMessageContaining(
93                          "default-group:childB:jar:classifier:2.0.0-SNAPSHOT <--- is not a release dependency");
94      }
95  
96      @Test
97      void testSearchTransitiveNoFailures() throws Exception {
98          when(session.getCurrentProject()).thenReturn(project);
99          when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
100                 .thenReturn(new DependencyNodeBuilder().build());
101 
102         rule.setSearchTransitive(true);
103         assertThatCode(rule::execute).doesNotThrowAnyException();
104     }
105 
106     @Test
107     void testShouldFailOnlyWhenRelease() throws Exception {
108         when(session.getCurrentProject()).thenReturn(project);
109         when(project.getArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
110         rule.setOnlyWhenRelease(true);
111 
112         assertThatCode(rule::execute).doesNotThrowAnyException();
113 
114         verifyNoInteractions(resolverUtil);
115     }
116 
117     @Test
118     void testWildcardExcludeTests() throws Exception {
119         when(session.getCurrentProject()).thenReturn(project);
120         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
121                 .thenReturn(getDependencyNodeWithMultipleTestSnapshots());
122 
123         rule.setExcludes(Collections.singletonList("*:*:*:*:test"));
124         rule.setSearchTransitive(true);
125 
126         assertThatCode(rule::execute).doesNotThrowAnyException();
127     }
128 
129     @Test
130     void testWildcardExcludeAll() throws Exception {
131         when(session.getCurrentProject()).thenReturn(project);
132         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
133                 .thenReturn(getDependencyNodeWithMultipleTestSnapshots());
134 
135         rule.setExcludes(Collections.singletonList("*"));
136         rule.setSearchTransitive(true);
137 
138         assertThatCode(rule::execute).doesNotThrowAnyException();
139     }
140 
141     @Test
142     void testExcludesAndIncludes() throws Exception {
143         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
144                 .thenReturn(getDependencyNodeWithMultipleTestSnapshots());
145 
146         rule.setExcludes(Collections.singletonList("*"));
147         rule.setIncludes(Collections.singletonList("*:*:*:*:test"));
148         rule.setSearchTransitive(true);
149 
150         assertThatCode(rule::execute)
151                 .isInstanceOf(EnforcerRuleException.class)
152                 .hasMessageContaining(
153                         "default-group:childAA:jar:classifier:1.0.0-SNAPSHOT <--- is not a release dependency")
154                 .hasMessageContaining(
155                         "default-group:childB:jar:classifier:2.0.0-SNAPSHOT <--- is not a release dependency");
156     }
157 
158     /**
159      * Test id.
160      */
161     @Test
162     void testId() {
163         assertThat(rule.getCacheId()).isNull();
164     }
165 
166     @Test
167     void testFailWhenParentIsSnapshot() throws Exception {
168         when(session.getCurrentProject()).thenReturn(project);
169         when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
170         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
171                 .thenReturn(new DependencyNodeBuilder().build());
172 
173         rule.setFailWhenParentIsSnapshot(true);
174 
175         assertThatCode(rule::execute)
176                 .isInstanceOf(EnforcerRuleException.class)
177                 .hasMessageContaining("Parent Cannot be a snapshot: testGroupId:snapshot:jar:2.0-SNAPSHOT");
178     }
179 
180     @Test
181     void parentShouldBeExcluded() throws Exception {
182         when(session.getCurrentProject()).thenReturn(project);
183         when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
184         when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
185                 .thenReturn(new DependencyNodeBuilder().build());
186 
187         rule.setFailWhenParentIsSnapshot(true);
188         rule.setExcludes(Collections.singletonList("testGroupId:*"));
189 
190         assertThatCode(rule::execute).doesNotThrowAnyException();
191     }
192 }