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.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
45
46
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
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 }