View Javadoc

1   package org.apache.maven.shared.release.phase;
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  
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.settings.Settings;
35  import org.apache.maven.shared.release.ReleaseExecutionException;
36  import org.apache.maven.shared.release.ReleaseFailureException;
37  import org.apache.maven.shared.release.ReleaseResult;
38  import org.apache.maven.shared.release.config.ReleaseDescriptor;
39  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
40  import org.apache.maven.shared.release.env.ReleaseEnvironment;
41  import org.apache.maven.shared.release.exec.MavenExecutor;
42  import org.apache.maven.shared.release.exec.MavenExecutorException;
43  import org.codehaus.plexus.PlexusTestCase;
44  
45  /**
46   * Test the simple test running phase.
47   *
48   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
49   */
50  public class RunPrepareGoalsPhaseTest
51      extends PlexusTestCase
52  {
53      private RunPrepareGoalsPhase phase;
54  
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          phase = (RunPrepareGoalsPhase) lookup( ReleasePhase.ROLE, "run-preparation-goals" );
61      }
62  
63      public void testExecute()
64          throws ReleaseExecutionException, ReleaseFailureException, MavenExecutorException
65      {
66          // prepare
67          File testFile = getTestFile( "target/working-directory" );
68  
69          ReleaseDescriptor config = new ReleaseDescriptor();
70          config.setPreparationGoals( "clean integration-test" );
71          config.setWorkingDirectory( testFile.getAbsolutePath() );
72  
73          MavenExecutor mock = mock( MavenExecutor.class );
74  
75          phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
76  
77          // execute
78          phase.execute( config, (Settings) null, (List<MavenProject>) null );
79  
80          // verify
81          verify( mock ).executeGoals( eq( testFile ),
82                                       eq( "clean integration-test" ), 
83                                       isA( ReleaseEnvironment.class),
84                                       eq( true), 
85                                       isNull( String.class ),
86                                       isNull( String.class ),
87                                       isA( ReleaseResult.class ) );
88          verifyNoMoreInteractions( mock );
89      }
90  
91      public void testSimulate()
92          throws ReleaseExecutionException, MavenExecutorException
93      {
94          // prepare
95          File testFile = getTestFile( "target/working-directory" );
96  
97          ReleaseDescriptor config = new ReleaseDescriptor();
98          config.setPreparationGoals( "clean integration-test" );
99          config.setWorkingDirectory( testFile.getAbsolutePath() );
100 
101         MavenExecutor mock = mock( MavenExecutor.class );
102 
103         phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, (MavenExecutor) mock );
104 
105         // execute
106         phase.simulate( config, new DefaultReleaseEnvironment(), null );
107 
108         // verify
109         verify( mock ).executeGoals( eq( testFile ),
110                                      eq( "clean integration-test" ), 
111                                      isA( ReleaseEnvironment.class),
112                                      eq( true), 
113                                      isNull( String.class ),
114                                      isNull( String.class ),
115                                      isA( ReleaseResult.class ) );
116         verifyNoMoreInteractions( mock );
117     }
118 
119     public void testExecuteException()
120         throws ReleaseFailureException, MavenExecutorException
121     {
122         // prepare
123         File testFile = getTestFile( "target/working-directory" );
124 
125         ReleaseDescriptor config = new ReleaseDescriptor();
126         config.setPreparationGoals( "clean integration-test" );
127         config.setWorkingDirectory( testFile.getAbsolutePath() );
128 
129         MavenExecutor mock = mock( MavenExecutor.class );
130         doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
131                                                                                                    eq( "clean integration-test" ),
132                                                                                                    isA( ReleaseEnvironment.class ),
133                                                                                                    eq( true ),
134                                                                                                    isNull( String.class ),
135                                                                                                    isNull( String.class ),
136                                                                                                    isA( ReleaseResult.class ) );
137 
138         phase.setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
139 
140         // execute
141         try
142         {
143             phase.execute( config, (Settings) null, (List<MavenProject>) null );
144 
145             fail( "Should have thrown an exception" );
146         }
147         catch ( ReleaseExecutionException e )
148         {
149             assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
150         }
151         
152         // verify
153         verify( mock ).executeGoals( eq( testFile ),
154                                      eq( "clean integration-test" ),
155                                      isA( ReleaseEnvironment.class ),
156                                      eq( true ),
157                                      isNull( String.class ),
158                                      isNull( String.class ),
159                                      isA( ReleaseResult.class ) );
160         verifyNoMoreInteractions( mock );
161     }
162 
163     public void testSimulateException() throws MavenExecutorException
164     {
165         // prepare
166         File testFile = getTestFile( "target/working-directory" );
167 
168         ReleaseDescriptor config = new ReleaseDescriptor();
169         config.setPreparationGoals( "clean integration-test" );
170         config.setWorkingDirectory( testFile.getAbsolutePath() );
171 
172         MavenExecutor mock = mock( MavenExecutor.class );
173         doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
174                                                                                                    eq( "clean integration-test" ),
175                                                                                                    isA( ReleaseEnvironment.class ),
176                                                                                                    eq( true ),
177                                                                                                    isNull( String.class ),
178                                                                                                    isNull( String.class ),
179                                                                                                    isA( ReleaseResult.class ) );
180 
181         phase.setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
182 
183         // execute
184         try
185         {
186             phase.simulate( config, new DefaultReleaseEnvironment(), null );
187 
188             fail( "Should have thrown an exception" );
189         }
190         catch ( ReleaseExecutionException e )
191         {
192             assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
193         }
194         
195         // verify
196         verify( mock ).executeGoals( eq( testFile ),
197                                      eq( "clean integration-test" ),
198                                      isA( ReleaseEnvironment.class ),
199                                      eq( true ),
200                                      isNull( String.class ),
201                                      isNull( String.class ),
202                                      isA( ReleaseResult.class ) );
203         verifyNoMoreInteractions( mock );
204         
205     }
206 
207     public void testEmptyGoals()
208         throws Exception
209     {
210         // prepare
211         File testFile = getTestFile( "target/working-directory" );
212 
213         ReleaseDescriptor config = new ReleaseDescriptor();
214         config.setPreparationGoals( "" );
215         config.setWorkingDirectory( testFile.getAbsolutePath() );
216 
217         MavenExecutor mock = mock( MavenExecutor.class );
218 
219         phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
220 
221         // execute
222         phase.execute( config, (Settings) null, (List<MavenProject>) null );
223 
224         // verify
225         // no invocations of mock
226         verifyNoMoreInteractions( mock );
227     }
228 }