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 import static org.mockito.Matchers.eq;
23 import static org.mockito.Matchers.isA;
24 import static org.mockito.Matchers.isNull;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29
30 import java.io.File;
31 import java.util.List;
32 import java.util.Properties;
33
34 import org.apache.maven.execution.MavenSession;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
38 import org.apache.maven.shared.release.ReleaseExecutionException;
39 import org.apache.maven.shared.release.ReleaseFailureException;
40 import org.apache.maven.shared.release.ReleaseManager;
41 import org.apache.maven.shared.release.config.ReleaseDescriptor;
42 import org.apache.maven.shared.release.env.ReleaseEnvironment;
43
44 /**
45 * Test release:prepare.
46 *
47 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
48 */
49 public class PrepareReleaseMojoTest
50 extends AbstractMojoTestCase
51 {
52 private void setDefaults( PrepareReleaseMojo mojo )
53 throws IllegalAccessException
54 {
55 setVariableValueToObject( mojo, "updateWorkingCopyVersions", Boolean.TRUE );
56 }
57
58 @SuppressWarnings( "unchecked" )
59 public void testPrepare()
60 throws Exception
61 {
62 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
63 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
64 setDefaults( mojo );
65 mojo.setBasedir( testFile.getParentFile() );
66 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
67 {
68 public Properties getExecutionProperties(){
69 return new Properties();
70 };
71 };
72
73 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
74 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
75 releaseDescriptor.setUpdateDependencies( false );
76
77 ReleaseManager mock = mock( ReleaseManager.class );
78 mojo.setReleaseManager( mock );
79
80 // execute
81 mojo.execute();
82
83 // verify
84 verify( mock ).prepare( eq( releaseDescriptor ), isA( ReleaseEnvironment.class ), isNull( List.class), eq( true ), eq( false ) );
85 assertTrue( true );
86 }
87
88 @SuppressWarnings( "unchecked" )
89 public void testPrepareWithExecutionException()
90 throws Exception
91 {
92 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
93 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
94 setDefaults( mojo );
95 mojo.setBasedir( testFile.getParentFile() );
96 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
97 {
98 public Properties getExecutionProperties(){
99 return new Properties();
100 };
101 };
102 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
103 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
104 releaseDescriptor.setUpdateDependencies( false );
105
106 ReleaseManager mock = mock( ReleaseManager.class );
107 doThrow( new ReleaseExecutionException( "..." ) ).when( mock ).prepare( eq( releaseDescriptor ),
108 isA( ReleaseEnvironment.class ),
109 isNull( List.class),
110 eq( true ),
111 eq( false ) );
112 mojo.setReleaseManager( mock );
113
114 //execute
115 try
116 {
117 mojo.execute();
118
119 fail( "Should have thrown an exception" );
120 }
121 catch ( MojoExecutionException e )
122 {
123 assertEquals( "Check cause", ReleaseExecutionException.class, e.getCause().getClass() );
124 }
125
126 // verify
127 verify( mock ).prepare( eq( releaseDescriptor ),
128 isA( ReleaseEnvironment.class ),
129 isNull( List.class),
130 eq( true ),
131 eq( false ) );
132 verifyNoMoreInteractions( mock );
133 }
134
135 @SuppressWarnings( "unchecked" )
136 public void testPrepareWithExecutionFailure()
137 throws Exception
138 {
139 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
140 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
141 setDefaults( mojo );
142 mojo.setBasedir( testFile.getParentFile() );
143 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
144 {
145 public Properties getExecutionProperties(){
146 return new Properties();
147 };
148 };
149 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
150 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
151 releaseDescriptor.setUpdateDependencies( false );
152
153 ReleaseManager mock = mock( ReleaseManager.class );
154 ReleaseFailureException cause = new ReleaseFailureException( "..." );
155 doThrow( cause ).when( mock ).prepare( eq( releaseDescriptor ),
156 isA( ReleaseEnvironment.class ),
157 isNull( List.class),
158 eq( true ),
159 eq( false ) );
160 mojo.setReleaseManager( mock );
161
162 // execute
163 try
164 {
165 mojo.execute();
166
167 fail( "Should have thrown an exception" );
168 }
169 catch ( MojoFailureException e )
170 {
171 assertEquals( "Check cause exists", cause, e.getCause() );
172 }
173 // verify
174 verify( mock ).prepare( eq( releaseDescriptor ),
175 isA( ReleaseEnvironment.class ),
176 isNull( List.class),
177 eq( true ),
178 eq( false ) );
179 verifyNoMoreInteractions( mock );
180 }
181
182 /*
183 public void testPerformWithScm()
184 throws Exception
185 {
186 PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
187 "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
188
189 ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
190 releaseConfiguration.setSettings( mojo.getSettings() );
191 releaseConfiguration.setUrl( "scm-url" );
192
193 Mock mock = new Mock( ReleaseManager.class );
194 Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
195 new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
196 new IsEqual( Boolean.TRUE )};
197 mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
198 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
199
200 mojo.execute();
201
202 assertTrue( true );
203 }
204
205 public void testPerformWithProfiles()
206 throws Exception
207 {
208 PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
209 "target/test-classes/mojos/perform/perform.xml" ) );
210
211 ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
212 releaseConfiguration.setSettings( mojo.getSettings() );
213 releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
214
215 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
216 Profile profile1 = new Profile();
217 profile1.setId( "prof1" );
218 Profile profile2 = new Profile();
219 profile2.setId( "2prof" );
220 project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
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 testPerformWithProfilesAndArguments()
235 throws Exception
236 {
237 PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
238 "target/test-classes/mojos/perform/perform-with-args.xml" ) );
239
240 ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
241 releaseConfiguration.setSettings( mojo.getSettings() );
242 releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -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 }