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 java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.release.ReleaseExecutionException;
32  import org.apache.maven.shared.release.ReleaseFailureException;
33  import org.apache.maven.shared.release.ReleaseManager;
34  import org.apache.maven.shared.release.ReleasePrepareRequest;
35  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
36  import org.apache.maven.shared.release.env.ReleaseEnvironment;
37  import org.mockito.ArgumentCaptor;
38  import org.mockito.invocation.InvocationOnMock;
39  import org.mockito.stubbing.Answer;
40  
41  import static org.hamcrest.CoreMatchers.instanceOf;
42  import static org.hamcrest.CoreMatchers.is;
43  import static org.hamcrest.CoreMatchers.notNullValue;
44  import static org.hamcrest.MatcherAssert.assertThat;
45  import static org.mockito.ArgumentMatchers.isA;
46  import static org.mockito.Mockito.doThrow;
47  import static org.mockito.Mockito.mock;
48  import static org.mockito.Mockito.spy;
49  import static org.mockito.Mockito.times;
50  import static org.mockito.Mockito.verify;
51  import static org.mockito.Mockito.verifyNoMoreInteractions;
52  import static org.mockito.Mockito.when;
53  
54  /**
55   * Test release:prepare.
56   *
57   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
58   */
59  public class PrepareReleaseMojoTest extends AbstractMojoTestCase {
60      private void setDefaults(PrepareReleaseMojo mojo) throws IllegalAccessException {
61          setVariableValueToObject(mojo, "updateWorkingCopyVersions", Boolean.TRUE);
62      }
63  
64      public void testPrepare() throws Exception {
65          File testFile = getTestFile("target/test-classes/mojos/prepare/prepare.xml");
66          final PrepareReleaseMojo mojo = spy((PrepareReleaseMojo) lookupMojo("prepare", testFile));
67          mojo.getProject().setFile(testFile);
68          setDefaults(mojo);
69          mojo.setBasedir(testFile.getParentFile());
70          mojo.setPomFileName("pom.xml");
71          mojo.session = new MavenSession(null, null, null, null, null, null, null, null, null) {
72              public Properties getExecutionProperties() {
73                  return new Properties();
74              }
75              ;
76  
77              @Override
78              public List<MavenProject> getProjects() {
79                  return Collections.singletonList(mojo.project);
80              }
81          };
82  
83          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
84          builder.setWorkingDirectory(testFile.getParentFile().getAbsolutePath());
85          builder.setUpdateDependencies(false);
86  
87          ReleaseManager mock = mock(ReleaseManager.class);
88          mojo.setReleaseManager(mock);
89  
90          when(mojo.createReleaseDescriptor()).thenAnswer(new Answer<ReleaseDescriptorBuilder>() {
91              @Override
92              public ReleaseDescriptorBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
93                  ReleaseDescriptorBuilder original = (ReleaseDescriptorBuilder) invocationOnMock.callRealMethod();
94                  return spy(original);
95              }
96          });
97  
98          // execute
99          mojo.execute();
100 
101         ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass(ReleasePrepareRequest.class);
102 
103         // verify
104         verify(mock).prepare(prepareRequest.capture());
105 
106         assertThat(
107                 prepareRequest.getValue().getReleaseDescriptorBuilder(),
108                 is(instanceOf(ReleaseDescriptorBuilder.class)));
109         assertThat(prepareRequest.getValue().getReleaseEnvironment(), is(instanceOf(ReleaseEnvironment.class)));
110         assertThat(prepareRequest.getValue().getReactorProjects(), is(notNullValue()));
111         assertThat(prepareRequest.getValue().getResume(), is(true));
112         assertThat(prepareRequest.getValue().getDryRun(), is(false));
113 
114         verify(prepareRequest.getValue().getReleaseDescriptorBuilder()).setScmSignTags(false);
115     }
116 
117     public void testPrepareWithExecutionException() throws Exception {
118         File testFile = getTestFile("target/test-classes/mojos/prepare/prepare.xml");
119         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo("prepare", testFile);
120         mojo.getProject().setFile(testFile);
121         setDefaults(mojo);
122         mojo.setBasedir(testFile.getParentFile());
123         mojo.setPomFileName("pom.xml");
124         mojo.session = new MavenSession(null, null, null, null, null, null, null, null, null) {
125             public Properties getExecutionProperties() {
126                 return new Properties();
127             }
128             ;
129 
130             @Override
131             public List<MavenProject> getProjects() {
132                 return Collections.singletonList(mojo.project);
133             }
134         };
135 
136         ReleaseManager mock = mock(ReleaseManager.class);
137         doThrow(new ReleaseExecutionException("...")).when(mock).prepare(isA(ReleasePrepareRequest.class));
138         mojo.setReleaseManager(mock);
139 
140         // execute
141         try {
142             mojo.execute();
143 
144             fail("Should have thrown an exception");
145         } catch (MojoExecutionException e) {
146             assertEquals(
147                     "Check cause", ReleaseExecutionException.class, e.getCause().getClass());
148         }
149 
150         // verify
151         verify(mock).prepare(isA(ReleasePrepareRequest.class));
152         verifyNoMoreInteractions(mock);
153     }
154 
155     public void testPrepareWithExecutionFailure() throws Exception {
156         File testFile = getTestFile("target/test-classes/mojos/prepare/prepare.xml");
157         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo("prepare", testFile);
158         mojo.getProject().setFile(testFile);
159         setDefaults(mojo);
160         mojo.setBasedir(testFile.getParentFile());
161         mojo.setPomFileName("pom.xml");
162         mojo.session = new MavenSession(null, null, null, null, null, null, null, null, null) {
163             public Properties getExecutionProperties() {
164                 return new Properties();
165             }
166             ;
167 
168             @Override
169             public List<MavenProject> getProjects() {
170                 return Collections.singletonList(mojo.project);
171             }
172         };
173 
174         ReleaseManager mock = mock(ReleaseManager.class);
175         ReleaseFailureException cause = new ReleaseFailureException("...");
176         doThrow(cause).when(mock).prepare(isA(ReleasePrepareRequest.class));
177         mojo.setReleaseManager(mock);
178 
179         // execute
180         try {
181             mojo.execute();
182 
183             fail("Should have thrown an exception");
184         } catch (MojoFailureException e) {
185             assertEquals("Check cause exists", cause, e.getCause());
186         }
187         // verify
188         verify(mock).prepare(isA(ReleasePrepareRequest.class));
189         verifyNoMoreInteractions(mock);
190     }
191 
192     public void testLineSeparatorInPrepareWithPom() throws Exception {
193         File testFile = getTestFile("target/test-classes/mojos/prepare/prepare.xml");
194         final PrepareWithPomReleaseMojo mojo = (PrepareWithPomReleaseMojo) lookupMojo("prepare-with-pom", testFile);
195         mojo.getProject().setFile(testFile);
196         setDefaults(mojo);
197         setVariableValueToObject(mojo, "generateReleasePoms", Boolean.TRUE);
198         mojo.setBasedir(testFile.getParentFile());
199         mojo.setPomFileName("pom.xml");
200         mojo.project.setFile(testFile);
201         mojo.session = new MavenSession(null, null, null, null, null, null, null, null, null) {
202             public Properties getExecutionProperties() {
203                 return new Properties();
204             }
205             ;
206 
207             @Override
208             public List<MavenProject> getProjects() {
209                 return Collections.singletonList(mojo.project);
210             }
211         };
212 
213         ReleaseManager mock = mock(ReleaseManager.class);
214         mojo.setReleaseManager(mock);
215 
216         int times = 1;
217         testLineSeparator(null, "\n", mojo, mock, times++);
218         testLineSeparator("source", "\n", mojo, mock, times++);
219         testLineSeparator("cr", "\r", mojo, mock, times++);
220         testLineSeparator("lf", "\n", mojo, mock, times++);
221         testLineSeparator("crlf", "\r\n", mojo, mock, times++);
222         testLineSeparator("system", System.lineSeparator(), mojo, mock, times++);
223     }
224 
225     public void testLineSeparatorInPrepare() throws Exception {
226         File testFile = getTestFile("target/test-classes/mojos/prepare/prepare.xml");
227         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo("prepare", testFile);
228         mojo.getProject().setFile(testFile);
229         setDefaults(mojo);
230         mojo.setBasedir(testFile.getParentFile());
231         mojo.setPomFileName("pom.xml");
232         mojo.project.setFile(testFile);
233         mojo.session = new MavenSession(null, null, null, null, null, null, null, null, null) {
234             public Properties getExecutionProperties() {
235                 return new Properties();
236             }
237             ;
238 
239             @Override
240             public List<MavenProject> getProjects() {
241                 return Collections.singletonList(mojo.project);
242             }
243         };
244 
245         ReleaseManager mock = mock(ReleaseManager.class);
246         mojo.setReleaseManager(mock);
247 
248         int times = 1;
249         testLineSeparator(null, "\n", mojo, mock, times++);
250         testLineSeparator("source", "\n", mojo, mock, times++);
251         testLineSeparator("cr", "\r", mojo, mock, times++);
252         testLineSeparator("lf", "\n", mojo, mock, times++);
253         testLineSeparator("crlf", "\r\n", mojo, mock, times++);
254         testLineSeparator("system", System.lineSeparator(), mojo, mock, times++);
255     }
256 
257     private void testLineSeparator(
258             String lineSeparator, String expected, PrepareReleaseMojo mojo, ReleaseManager releaseManager, int times)
259             throws Exception {
260 
261         setVariableValueToObject(mojo, "lineSeparator", lineSeparator);
262 
263         mojo.execute();
264 
265         ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass(ReleasePrepareRequest.class);
266         verify(releaseManager, times(times)).prepare(prepareRequest.capture());
267 
268         assertEquals(
269                 expected,
270                 prepareRequest.getValue().getReleaseDescriptorBuilder().build().getLineSeparator());
271     }
272 
273     /*
274         public void testPerformWithScm()
275             throws Exception
276         {
277             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
278                 "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
279 
280             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
281             releaseConfiguration.setSettings( mojo.getSettings() );
282             releaseConfiguration.setUrl( "scm-url" );
283 
284             Mock mock = new Mock( ReleaseManager.class );
285             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
286                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
287                 new IsEqual( Boolean.TRUE )};
288             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
289             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
290 
291             mojo.execute();
292 
293             assertTrue( true );
294         }
295 
296         public void testPerformWithProfiles()
297             throws Exception
298         {
299             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
300                 "target/test-classes/mojos/perform/perform.xml" ) );
301 
302             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
303             releaseConfiguration.setSettings( mojo.getSettings() );
304             releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
305 
306             MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
307             Profile profile1 = new Profile();
308             profile1.setId( "prof1" );
309             Profile profile2 = new Profile();
310             profile2.setId( "2prof" );
311             project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
312 
313             Mock mock = new Mock( ReleaseManager.class );
314             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
315                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
316                 new IsEqual( Boolean.TRUE )};
317             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
318             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
319 
320             mojo.execute();
321 
322             assertTrue( true );
323         }
324 
325         public void testPerformWithProfilesAndArguments()
326             throws Exception
327         {
328             PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
329                 "target/test-classes/mojos/perform/perform-with-args.xml" ) );
330 
331             ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
332             releaseConfiguration.setSettings( mojo.getSettings() );
333             releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -P prof1,2prof" );
334 
335             MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
336             Profile profile1 = new Profile();
337             profile1.setId( "prof1" );
338             Profile profile2 = new Profile();
339             profile2.setId( "2prof" );
340             project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
341 
342             Mock mock = new Mock( ReleaseManager.class );
343             Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
344                 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
345                 new IsEqual( Boolean.TRUE )};
346             mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
347             mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
348 
349             mojo.execute();
350 
351             assertTrue( true );
352         }
353     */
354 }