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      public void testPrepare()
51          throws Exception
52      {
53          File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
54          PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
55          mojo.setBasedir( testFile.getParentFile() );
56          mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
57          {
58            public Properties getExecutionProperties(){
59                return new Properties();
60            };
61          };
62          
63          ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
64          releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
65          releaseDescriptor.setUpdateDependencies( false );
66          
67          Mock mock = new Mock( ReleaseManager.class );
68  
69          Constraint[] constraints = new Constraint[] {
70              new IsEqual( releaseDescriptor ),
71              new IsInstanceOf( ReleaseEnvironment.class ),
72              new IsNull(),
73              new IsEqual( Boolean.TRUE ),
74              new IsEqual( Boolean.FALSE )
75          };
76  
77          mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints );
78          mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
79  
80          mojo.execute();
81  
82          assertTrue( true );
83      }
84  
85      public void testPrepareWithExecutionException()
86          throws Exception
87      {
88          File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
89          PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
90          mojo.setBasedir( testFile.getParentFile() );
91          mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
92          {
93            public Properties getExecutionProperties(){
94                return new Properties();
95            };
96          };
97          ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
98          releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
99          releaseDescriptor.setUpdateDependencies( false );
100         
101         Mock mock = new Mock( ReleaseManager.class );
102 
103         Constraint[] constraints = new Constraint[] {
104             new IsEqual( releaseDescriptor ),
105             new IsInstanceOf( ReleaseEnvironment.class ),
106             new IsNull(),
107             new IsEqual( Boolean.TRUE ),
108             new IsEqual( Boolean.FALSE )
109         };
110 
111         mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
112             new ThrowStub( new ReleaseExecutionException( "..." ) ) );
113         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
114 
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 
127     public void testPrepareWithExecutionFailure()
128         throws Exception
129     {
130         File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
131         PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
132         mojo.setBasedir( testFile.getParentFile() );
133         mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
134         {
135           public Properties getExecutionProperties(){
136               return new Properties();
137           };
138         };
139         ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
140         releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
141         releaseDescriptor.setUpdateDependencies( false );
142         
143         Mock mock = new Mock( ReleaseManager.class );
144 
145         Constraint[] constraints = new Constraint[] {
146             new IsEqual( releaseDescriptor ),
147             new IsInstanceOf( ReleaseEnvironment.class ),
148             new IsNull(),
149             new IsEqual( Boolean.TRUE ),
150             new IsEqual( Boolean.FALSE )
151         };
152 
153         ReleaseFailureException cause = new ReleaseFailureException( "..." );
154         mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
155             new ThrowStub( cause ) );
156         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
157 
158         try
159         {
160             mojo.execute();
161 
162             fail( "Should have thrown an exception" );
163         }
164         catch ( MojoFailureException e )
165         {
166             assertEquals( "Check cause exists", cause, e.getCause() );
167         }
168     }
169 
170 /*
171     public void testPerformWithScm()
172         throws Exception
173     {
174         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
175             "target/test-classes/mojos/perform/perform-with-scm.xml" ) );
176 
177         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
178         releaseConfiguration.setSettings( mojo.getSettings() );
179         releaseConfiguration.setUrl( "scm-url" );
180 
181         Mock mock = new Mock( ReleaseManager.class );
182         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
183             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
184             new IsEqual( Boolean.TRUE )};
185         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
186         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
187 
188         mojo.execute();
189 
190         assertTrue( true );
191     }
192 
193     public void testPerformWithProfiles()
194         throws Exception
195     {
196         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
197             "target/test-classes/mojos/perform/perform.xml" ) );
198 
199         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
200         releaseConfiguration.setSettings( mojo.getSettings() );
201         releaseConfiguration.setAdditionalArguments( "-P prof1,2prof" );
202 
203         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
204         Profile profile1 = new Profile();
205         profile1.setId( "prof1" );
206         Profile profile2 = new Profile();
207         profile2.setId( "2prof" );
208         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
209 
210         Mock mock = new Mock( ReleaseManager.class );
211         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
212             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
213             new IsEqual( Boolean.TRUE )};
214         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
215         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
216 
217         mojo.execute();
218 
219         assertTrue( true );
220     }
221 
222     public void testPerformWithProfilesAndArguments()
223         throws Exception
224     {
225         PerformReleaseMojo mojo = (PerformReleaseMojo) lookupMojo( "perform", getTestFile(
226             "target/test-classes/mojos/perform/perform-with-args.xml" ) );
227 
228         ReleaseDescriptor releaseConfiguration = new ReleaseDescriptor();
229         releaseConfiguration.setSettings( mojo.getSettings() );
230         releaseConfiguration.setAdditionalArguments( "-Dmaven.test.skip=true -P prof1,2prof" );
231 
232         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
233         Profile profile1 = new Profile();
234         profile1.setId( "prof1" );
235         Profile profile2 = new Profile();
236         profile2.setId( "2prof" );
237         project.setActiveProfiles( Arrays.asList( new Profile[]{profile1, profile2} ) );
238 
239         Mock mock = new Mock( ReleaseManager.class );
240         Constraint[] constraints = new Constraint[]{new IsEqual( releaseConfiguration ),
241             new IsEqual( new File( getBasedir(), "target/checkout" ) ), new IsEqual( "deploy site-deploy" ),
242             new IsEqual( Boolean.TRUE )};
243         mock.expects( new InvokeOnceMatcher() ).method( "perform" ).with( constraints );
244         mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
245 
246         mojo.execute();
247 
248         assertTrue( true );
249     }
250 */
251 }