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.junit.Assert.assertThat;
27  
28  import static org.mockito.Matchers.isA;
29  import static org.mockito.Mockito.doThrow;
30  import static org.mockito.Mockito.mock;
31  import static org.mockito.Mockito.verify;
32  import static org.mockito.Mockito.verifyNoMoreInteractions;
33  
34  import java.io.File;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Properties;
38  
39  import org.apache.maven.execution.MavenSession;
40  import org.apache.maven.plugin.MojoExecutionException;
41  import org.apache.maven.plugin.MojoFailureException;
42  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
43  import org.apache.maven.project.MavenProject;
44  import org.apache.maven.shared.release.ReleaseExecutionException;
45  import org.apache.maven.shared.release.ReleaseFailureException;
46  import org.apache.maven.shared.release.ReleaseManager;
47  import org.apache.maven.shared.release.ReleasePrepareRequest;
48  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
49  import org.apache.maven.shared.release.env.ReleaseEnvironment;
50  import org.mockito.ArgumentCaptor;
51  
52  /**
53   * Test release:prepare.
54   *
55   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
56   */
57  public class PrepareReleaseMojoTest
58      extends AbstractMojoTestCase
59  {
60      private void setDefaults( PrepareReleaseMojo mojo )
61          throws IllegalAccessException
62      {
63          setVariableValueToObject( mojo, "updateWorkingCopyVersions", Boolean.TRUE );
64      }
65      
66      public void testPrepare()
67          throws Exception
68      {
69          File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
70          final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
71          setDefaults( mojo );
72          mojo.setBasedir( testFile.getParentFile() );
73          mojo.setPomFileName( "pom.xml" );
74          mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
75          {
76              public Properties getExecutionProperties()
77              {
78                  return new Properties();
79              };
80  
81              @Override
82              public List<MavenProject> getProjects()
83              {
84                  return Collections.singletonList( mojo.project );
85              }
86          };
87          
88          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
89          builder.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
90          builder.setUpdateDependencies( false );
91          
92          ReleaseManager mock = mock( ReleaseManager.class );
93          mojo.setReleaseManager( mock );
94  
95          // execute
96          mojo.execute();
97  
98          ArgumentCaptor<ReleasePrepareRequest> prepareRequest = ArgumentCaptor.forClass( ReleasePrepareRequest.class );
99          
100         // verify
101         verify( mock ).prepare( prepareRequest.capture() );
102         
103         assertThat( prepareRequest.getValue().getReleaseDescriptorBuilder(),
104                     is( instanceOf( ReleaseDescriptorBuilder.class ) ) );
105         assertThat( prepareRequest.getValue().getReleaseEnvironment(), is( instanceOf( ReleaseEnvironment.class ) ) );
106         assertThat( prepareRequest.getValue().getReactorProjects(), is( notNullValue() ) );
107         assertThat( prepareRequest.getValue().getResume(), is( true ) );
108         assertThat( prepareRequest.getValue().getDryRun(), is( false ) );
109     }
110 
111     public void testPrepareWithExecutionException()
112         throws Exception
113     {
114         File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
115         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
116         setDefaults( mojo );
117         mojo.setBasedir( testFile.getParentFile() );
118         mojo.setPomFileName( "pom.xml" );
119         mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
120         {
121           public Properties getExecutionProperties(){
122               return new Properties();
123           };
124 
125           @Override
126           public List<MavenProject> getProjects()
127           {
128               return Collections.singletonList( mojo.project );
129           }
130         };
131 
132         ReleaseManager mock = mock( ReleaseManager.class );
133         doThrow( new ReleaseExecutionException( "..." ) ).when( mock ).prepare( isA( ReleasePrepareRequest.class ) );
134         mojo.setReleaseManager( mock );
135 
136         //execute
137         try
138         {
139             mojo.execute();
140 
141             fail( "Should have thrown an exception" );
142         }
143         catch ( MojoExecutionException e )
144         {
145             assertEquals( "Check cause", ReleaseExecutionException.class, e.getCause().getClass() );
146         }
147         
148         // verify
149         verify( mock ).prepare( isA( ReleasePrepareRequest.class ) );
150         verifyNoMoreInteractions( mock );
151     }
152 
153     public void testPrepareWithExecutionFailure()
154         throws Exception
155     {
156         File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
157         final PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
158         setDefaults( mojo );
159         mojo.setBasedir( testFile.getParentFile() );
160         mojo.setPomFileName( "pom.xml" );
161         mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
162         {
163           public Properties getExecutionProperties(){
164               return new Properties();
165           };
166           
167           @Override
168           public List<MavenProject> getProjects()
169           {
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         {
182             mojo.execute();
183 
184             fail( "Should have thrown an exception" );
185         }
186         catch ( MojoFailureException e )
187         {
188             assertEquals( "Check cause exists", cause, e.getCause() );
189         }
190         // verify
191         verify( mock ).prepare( isA( ReleasePrepareRequest.class ) );
192         verifyNoMoreInteractions( mock );
193     }
194 
195 /*
196     public void testPerformWithScm()
197         throws Exception
198     {
199         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
200             "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
201 
202         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
203         releaseConfiguration.setSettings( mojo.getSettings() );
204         releaseConfiguration.setUrl( "scm-url" );
205 
206         Mock mock = new Mock( ReleaseManager.class );
207         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
208             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
209             new IsEqual( Boolean.TRUE )};
210         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
211         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
212 
213         mojo.execute();
214 
215         assertTrue( true );
216     }
217 
218     public void testPerformWithProfiles()
219         throws Exception
220     {
221         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
222             "target/test-classes/mojos/perform/perform.xml" ) );
223 
224         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
225         releaseConfiguration.setSettings( mojo.getSettings() );
226         releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
227 
228         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
229         Profile profile1 = new Profile();
230         profile1.setId( "prof1" );
231         Profile profile2 = new Profile();
232         profile2.setId( "2prof" );
233         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
234 
235         Mock mock = new Mock( ReleaseManager.class );
236         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
237             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
238             new IsEqual( Boolean.TRUE )};
239         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
240         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
241 
242         mojo.execute();
243 
244         assertTrue( true );
245     }
246 
247     public void testPerformWithProfilesAndArguments()
248         throws Exception
249     {
250         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
251             "target/test-classes/mojos/perform/perform-with-args.xml" ) );
252 
253         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
254         releaseConfiguration.setSettings( mojo.getSettings() );
255         releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -P prof1,2prof" );
256 
257         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
258         Profile profile1 = new Profile();
259         profile1.setId( "prof1" );
260         Profile profile2 = new Profile();
261         profile2.setId( "2prof" );
262         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
263 
264         Mock mock = new Mock( ReleaseManager.class );
265         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
266             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
267             new IsEqual( Boolean.TRUE )};
268         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
269         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
270 
271         mojo.execute();
272 
273         assertTrue( true );
274     }
275 */
276 }