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.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
46
47
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
68
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
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 }