1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.release;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.util.Collections;
25
26 import org.apache.maven.api.di.Provides;
27 import org.apache.maven.api.plugin.testing.Basedir;
28 import org.apache.maven.api.plugin.testing.InjectMojo;
29 import org.apache.maven.api.plugin.testing.MojoParameter;
30 import org.apache.maven.api.plugin.testing.MojoTest;
31 import org.apache.maven.execution.DefaultMavenExecutionRequest;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.shared.release.ReleaseExecutionException;
37 import org.apache.maven.shared.release.ReleaseFailureException;
38 import org.apache.maven.shared.release.ReleaseManager;
39 import org.apache.maven.shared.release.ReleasePrepareRequest;
40 import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
41 import org.apache.maven.shared.release.env.ReleaseEnvironment;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.api.extension.ExtendWith;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Mock;
47 import org.mockito.junit.jupiter.MockitoExtension;
48
49 import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
50 import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject;
51 import static org.hamcrest.CoreMatchers.instanceOf;
52 import static org.hamcrest.CoreMatchers.is;
53 import static org.hamcrest.CoreMatchers.notNullValue;
54 import static org.hamcrest.MatcherAssert.assertThat;
55 import static org.junit.jupiter.api.Assertions.assertEquals;
56 import static org.junit.jupiter.api.Assertions.fail;
57 import static org.mockito.ArgumentMatchers.isA;
58 import static org.mockito.Mockito.doThrow;
59 import static org.mockito.Mockito.times;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.Mockito.verifyNoMoreInteractions;
62 import static org.mockito.Mockito.when;
63
64
65
66
67
68
69 @ExtendWith(MockitoExtension.class)
70 @MojoTest
71 class PrepareReleaseMojoTest {
72
73 @Mock
74 private ReleaseManager releaseManagerMock;
75
76 @Inject
77 private MavenProject mavenProject;
78
79 @Inject
80 private MavenSession mavenSession;
81
82 @Provides
83 private ReleaseManager releaseManager() {
84 return releaseManagerMock;
85 }
86
87 @BeforeEach
88 void setup() {
89 when(mavenProject.getFile()).thenReturn(new File(getBasedir(), "prepare.xml"));
90
91 when(mavenProject.getGroupId()).thenReturn("groupId");
92 when(mavenProject.getArtifactId()).thenReturn("artifactId");
93 when(mavenProject.getVersion()).thenReturn("1.0.0-SNAPSHOT");
94
95 when(mavenSession.getProjects()).thenReturn(Collections.singletonList(mavenProject));
96 when(mavenSession.getRequest()).thenReturn(new DefaultMavenExecutionRequest());
97 }
98
99 @Test
100 @Basedir("/mojos/prepare")
101 @InjectMojo(goal = "prepare", pom = "prepare.xml")
102 @MojoParameter(name = "updateDependencies", value = "false")
103 void testPrepare(PrepareReleaseMojo mojo) throws Exception {
104
105 mojo.execute();
106
107 ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass(ReleasePrepareRequest.class);
108
109
110 verify(releaseManagerMock).prepare(prepareRequest.capture());
111
112 assertThat(
113 prepareRequest.getValue().getReleaseDescriptorBuilder(),
114 is(instanceOf(ReleaseDescriptorBuilder.class)));
115 assertThat(prepareRequest.getValue().getReleaseEnvironment(), is(instanceOf(ReleaseEnvironment.class)));
116 assertThat(prepareRequest.getValue().getReactorProjects(), is(notNullValue()));
117 assertThat(prepareRequest.getValue().getResume(), is(true));
118 assertThat(prepareRequest.getValue().getDryRun(), is(false));
119
120 ReleaseDescriptorBuilder.BuilderReleaseDescriptor releaseDescriptor =
121 prepareRequest.getValue().getReleaseDescriptorBuilder().build();
122 assertThat(releaseDescriptor.isScmSignTags(), is(false));
123 assertThat(releaseDescriptor.isUpdateDependencies(), is(false));
124 }
125
126 @Test
127 @Basedir("/mojos/prepare")
128 @InjectMojo(goal = "prepare", pom = "prepare.xml")
129 void testPrepareWithExecutionException(PrepareReleaseMojo mojo) throws Exception {
130 doThrow(new ReleaseExecutionException("..."))
131 .when(releaseManagerMock)
132 .prepare(isA(ReleasePrepareRequest.class));
133
134
135 try {
136 mojo.execute();
137
138 fail("Should have thrown an exception");
139 } catch (MojoExecutionException e) {
140 assertEquals(ReleaseExecutionException.class, e.getCause().getClass(), "Check cause");
141 }
142
143
144 verify(releaseManagerMock).prepare(isA(ReleasePrepareRequest.class));
145 verifyNoMoreInteractions(releaseManagerMock);
146 }
147
148 @Test
149 @Basedir("/mojos/prepare")
150 @InjectMojo(goal = "prepare", pom = "prepare.xml")
151 void testPrepareWithExecutionFailure(PrepareReleaseMojo mojo) throws Exception {
152
153 ReleaseFailureException cause = new ReleaseFailureException("...");
154 doThrow(cause).when(releaseManagerMock).prepare(isA(ReleasePrepareRequest.class));
155
156
157 try {
158 mojo.execute();
159
160 fail("Should have thrown an exception");
161 } catch (MojoFailureException e) {
162 assertEquals(cause, e.getCause(), "Check cause exists");
163 }
164
165 verify(releaseManagerMock).prepare(isA(ReleasePrepareRequest.class));
166 verifyNoMoreInteractions(releaseManagerMock);
167 }
168
169 @Test
170 @Basedir("/mojos/prepare")
171 @InjectMojo(goal = "prepare-with-pom", pom = "prepare.xml")
172 void testLineSeparatorInPrepareWithPom(PrepareReleaseMojo mojo) throws Exception {
173 int times = 1;
174 testLineSeparator(null, "\n", mojo, releaseManagerMock, times++);
175 testLineSeparator("source", "\n", mojo, releaseManagerMock, times++);
176 testLineSeparator("cr", "\r", mojo, releaseManagerMock, times++);
177 testLineSeparator("lf", "\n", mojo, releaseManagerMock, times++);
178 testLineSeparator("crlf", "\r\n", mojo, releaseManagerMock, times++);
179 testLineSeparator("system", System.lineSeparator(), mojo, releaseManagerMock, times++);
180 }
181
182 @Test
183 @Basedir("/mojos/prepare")
184 @InjectMojo(goal = "prepare", pom = "prepare.xml")
185 void testLineSeparatorInPrepare(PrepareReleaseMojo mojo) throws Exception {
186 int times = 1;
187 testLineSeparator(null, "\n", mojo, releaseManagerMock, times++);
188 testLineSeparator("source", "\n", mojo, releaseManagerMock, times++);
189 testLineSeparator("cr", "\r", mojo, releaseManagerMock, times++);
190 testLineSeparator("lf", "\n", mojo, releaseManagerMock, times++);
191 testLineSeparator("crlf", "\r\n", mojo, releaseManagerMock, times++);
192 testLineSeparator("system", System.lineSeparator(), mojo, releaseManagerMock, times++);
193 }
194
195 private void testLineSeparator(
196 String lineSeparator, String expected, PrepareReleaseMojo mojo, ReleaseManager releaseManager, int times)
197 throws Exception {
198
199 setVariableValueToObject(mojo, "lineSeparator", lineSeparator);
200
201 mojo.execute();
202
203 ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass(ReleasePrepareRequest.class);
204 verify(releaseManager, times(times)).prepare(prepareRequest.capture());
205
206 assertEquals(
207 expected,
208 prepareRequest.getValue().getReleaseDescriptorBuilder().build().getLineSeparator());
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 }