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.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   * Test release:prepare.
66   *
67   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
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         // execute
105         mojo.execute();
106 
107         ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass(ReleasePrepareRequest.class);
108 
109         // verify
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         // execute
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         // verify
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         // execute
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         // verify
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         public void testPerformWithScm()
213             throws Exception
214         {
215             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
216                 "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
217 
218             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
219             releaseConfiguration.setSettings( mojo.getSettings() );
220             releaseConfiguration.setUrl( "scm-url" );
221 
222             Mock mock = new Mock( ReleaseManager.class );
223             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
224                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
225                 new IsEqual( Boolean.TRUE )};
226             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
227             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
228 
229             mojo.execute();
230 
231             assertTrue( true );
232         }
233 
234         public void testPerformWithProfiles()
235             throws Exception
236         {
237             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
238                 "target/test-classes/mojos/perform/perform.xml" ) );
239 
240             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
241             releaseConfiguration.setSettings( mojo.getSettings() );
242             releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
243 
244             MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
245             Profile profile1 = new Profile();
246             profile1.setId( "prof1" );
247             Profile profile2 = new Profile();
248             profile2.setId( "2prof" );
249             project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
250 
251             Mock mock = new Mock( ReleaseManager.class );
252             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
253                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
254                 new IsEqual( Boolean.TRUE )};
255             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
256             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
257 
258             mojo.execute();
259 
260             assertTrue( true );
261         }
262 
263         public void testPerformWithProfilesAndArguments()
264             throws Exception
265         {
266             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
267                 "target/test-classes/mojos/perform/perform-with-args.xml" ) );
268 
269             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
270             releaseConfiguration.setSettings( mojo.getSettings() );
271             releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -P prof1,2prof" );
272 
273             MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
274             Profile profile1 = new Profile();
275             profile1.setId( "prof1" );
276             Profile profile2 = new Profile();
277             profile2.setId( "2prof" );
278             project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
279 
280             Mock mock = new Mock( ReleaseManager.class );
281             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
282                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
283                 new IsEqual( Boolean.TRUE )};
284             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
285             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
286 
287             mojo.execute();
288 
289             assertTrue( true );
290         }
291     */
292 }