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