View Javadoc
1   package org.apache.maven.plugins.release;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import static org.hamcrest.CoreMatchers.instanceOf;
24  import static org.hamcrest.CoreMatchers.is;
25  import static org.hamcrest.CoreMatchers.notNullValue;
26  import static org.hamcrest.MatcherAssert.assertThat;
27  import static org.mockito.ArgumentMatchers.isA;
28  import static org.mockito.Mockito.doThrow;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.spy;
31  import static org.mockito.Mockito.verify;
32  import static org.mockito.Mockito.verifyNoMoreInteractions;
33  import static org.mockito.Mockito.when;
34  
35  import java.io.File;
36  import java.util.Collections;
37  import java.util.List;
38  import java.util.Properties;
39  
40  import org.apache.maven.execution.MavenSession;
41  import org.apache.maven.plugin.MojoExecutionException;
42  import org.apache.maven.plugin.MojoFailureException;
43  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
44  import org.apache.maven.project.MavenProject;
45  import org.apache.maven.shared.release.ReleaseExecutionException;
46  import org.apache.maven.shared.release.ReleaseFailureException;
47  import org.apache.maven.shared.release.ReleaseManager;
48  import org.apache.maven.shared.release.ReleasePrepareRequest;
49  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
50  import org.apache.maven.shared.release.env.ReleaseEnvironment;
51  import org.mockito.ArgumentCaptor;
52  import org.mockito.invocation.InvocationOnMock;
53  import org.mockito.stubbing.Answer;
54  
55  /**
56   * Test release:prepare.
57   *
58   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
59   */
60  public class PrepareReleaseMojoTest
61      extends AbstractMojoTestCase
62  {
63      private void setDefaults( PrepareReleaseMojo mojo )
64          throws IllegalAccessException
65      {
66          setVariableValueToObject( mojo, "updateWorkingCopyVersions", Boolean.TRUE );
67      }
68      
69      public void testPrepare()
70          throws Exception
71      {
72          File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
73          final PrepareReleaseMojo mojo = spy((PrepareReleaseMojo) lookupMojo( "prepare", testFile ));
74          setDefaults( mojo );
75          mojo.setBasedir( testFile.getParentFile() );
76          mojo.setPomFileName( "pom.xml" );
77          mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
78          {
79              public Properties getExecutionProperties()
80              {
81                  return new Properties();
82              };
83  
84              @Override
85              public List<MavenProject> getProjects()
86              {
87                  return Collections.singletonList( mojo.project );
88              }
89          };
90          
91          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
92          builder.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
93          builder.setUpdateDependencies( false );
94          
95          ReleaseManager mock = mock( ReleaseManager.class );
96          mojo.setReleaseManager( mock );
97  
98          when(mojo.createReleaseDescriptor()).thenAnswer(new Answer<ReleaseDescriptorBuilder>() {
99              @Override
100             public ReleaseDescriptorBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
101                 ReleaseDescriptorBuilder original = (ReleaseDescriptorBuilder) invocationOnMock.callRealMethod();
102                 return spy(original);
103             }
104         });
105 
106         // execute
107         mojo.execute();
108 
109         ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass( ReleasePrepareRequest.class );
110         
111         // verify
112         verify( mock ).prepare( prepareRequest.capture() );
113         
114         assertThat( prepareRequest.getValue().getReleaseDescriptorBuilder(),
115                     is( instanceOf( ReleaseDescriptorBuilder.class ) ) );
116         assertThat( prepareRequest.getValue().getReleaseEnvironment(), is( instanceOf( ReleaseEnvironment.class ) ) );
117         assertThat( prepareRequest.getValue().getReactorProjects(), is( notNullValue() ) );
118         assertThat( prepareRequest.getValue().getResume(), is( true ) );
119         assertThat( prepareRequest.getValue().getDryRun(), is( false ) );
120 
121         verify(prepareRequest.getValue().getReleaseDescriptorBuilder()).setScmSignTags(false);
122     }
123 
124     public void testPrepareWithExecutionException()
125         throws Exception
126     {
127         File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
128         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
129         setDefaults( mojo );
130         mojo.setBasedir( testFile.getParentFile() );
131         mojo.setPomFileName( "pom.xml" );
132         mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
133         {
134           public Properties getExecutionProperties(){
135               return new Properties();
136           };
137 
138           @Override
139           public List<MavenProject> getProjects()
140           {
141               return Collections.singletonList( mojo.project );
142           }
143         };
144 
145         ReleaseManager mock = mock( ReleaseManager.class );
146         doThrow( new ReleaseExecutionException( "..." ) ).when( mock ).prepare( isA( ReleasePrepareRequest.class ) );
147         mojo.setReleaseManager( mock );
148 
149         //execute
150         try
151         {
152             mojo.execute();
153 
154             fail( "Should have thrown an exception" );
155         }
156         catch ( MojoExecutionException e )
157         {
158             assertEquals( "Check cause", ReleaseExecutionException.class, e.getCause().getClass() );
159         }
160         
161         // verify
162         verify( mock ).prepare( isA( ReleasePrepareRequest.class ) );
163         verifyNoMoreInteractions( mock );
164     }
165 
166     public void testPrepareWithExecutionFailure()
167         throws Exception
168     {
169         File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
170         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
171         setDefaults( mojo );
172         mojo.setBasedir( testFile.getParentFile() );
173         mojo.setPomFileName( "pom.xml" );
174         mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
175         {
176           public Properties getExecutionProperties(){
177               return new Properties();
178           };
179           
180           @Override
181           public List<MavenProject> getProjects()
182           {
183               return Collections.singletonList( mojo.project );
184           }
185         };
186         
187         ReleaseManager mock = mock( ReleaseManager.class );
188         ReleaseFailureException cause = new ReleaseFailureException( "..." );
189         doThrow( cause ).when( mock ).prepare( isA( ReleasePrepareRequest.class ) );
190         mojo.setReleaseManager( mock );
191 
192         // execute
193         try
194         {
195             mojo.execute();
196 
197             fail( "Should have thrown an exception" );
198         }
199         catch ( MojoFailureException e )
200         {
201             assertEquals( "Check cause exists", cause, e.getCause() );
202         }
203         // verify
204         verify( mock ).prepare( isA( ReleasePrepareRequest.class ) );
205         verifyNoMoreInteractions( mock );
206     }
207 
208 /*
209     public void testPerformWithScm()
210         throws Exception
211     {
212         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
213             "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
214 
215         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
216         releaseConfiguration.setSettings( mojo.getSettings() );
217         releaseConfiguration.setUrl( "scm-url" );
218 
219         Mock mock = new Mock( ReleaseManager.class );
220         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
221             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
222             new IsEqual( Boolean.TRUE )};
223         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
224         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
225 
226         mojo.execute();
227 
228         assertTrue( true );
229     }
230 
231     public void testPerformWithProfiles()
232         throws Exception
233     {
234         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
235             "target/test-classes/mojos/perform/perform.xml" ) );
236 
237         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
238         releaseConfiguration.setSettings( mojo.getSettings() );
239         releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
240 
241         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
242         Profile profile1 = new Profile();
243         profile1.setId( "prof1" );
244         Profile profile2 = new Profile();
245         profile2.setId( "2prof" );
246         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
247 
248         Mock mock = new Mock( ReleaseManager.class );
249         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
250             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
251             new IsEqual( Boolean.TRUE )};
252         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
253         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
254 
255         mojo.execute();
256 
257         assertTrue( true );
258     }
259 
260     public void testPerformWithProfilesAndArguments()
261         throws Exception
262     {
263         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
264             "target/test-classes/mojos/perform/perform-with-args.xml" ) );
265 
266         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
267         releaseConfiguration.setSettings( mojo.getSettings() );
268         releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -P prof1,2prof" );
269 
270         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
271         Profile profile1 = new Profile();
272         profile1.setId( "prof1" );
273         Profile profile2 = new Profile();
274         profile2.setId( "2prof" );
275         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
276 
277         Mock mock = new Mock( ReleaseManager.class );
278         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
279             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
280             new IsEqual( Boolean.TRUE )};
281         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
282         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
283 
284         mojo.execute();
285 
286         assertTrue( true );
287     }
288 */
289 }