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