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 RunCompleteGoalsPhaseTest
51      extends PlexusTestCase
52  {
53      private RunCompleteGoalsPhase phase;
54  
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          phase = (RunCompleteGoalsPhase) lookup( ReleasePhase.ROLE, "run-completion-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.setCompletionGoals( "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.setCompletionGoals( "clean integration-test" );
99          config.setWorkingDirectory( testFile.getAbsolutePath() );
100 
101         MavenExecutor mock = mock( MavenExecutor.class );
102 
103         phase.setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, 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 ), isA( ReleaseResult.class ) );
115         verifyNoMoreInteractions( mock );
116     }
117 
118     public void testExecuteException()
119         throws ReleaseFailureException, MavenExecutorException
120     {
121         // prepare
122         File testFile = getTestFile( "target/working-directory" );
123 
124         ReleaseDescriptor config = new ReleaseDescriptor();
125         config.setCompletionGoals( "clean integration-test" );
126         config.setWorkingDirectory( testFile.getAbsolutePath() );
127 
128         MavenExecutor mock = mock( MavenExecutor.class );
129         doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
130                                  eq( "clean integration-test" ),
131                                  isA( ReleaseEnvironment.class ),
132                                  eq( true ),
133                                  isNull( String.class ),
134                                  isNull( String.class ),
135                                  isA( ReleaseResult.class ) );
136 
137         phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
138 
139         // execute
140         try
141         {
142             phase.execute( config, (Settings) null, (List<MavenProject>) null );
143 
144             fail( "Should have thrown an exception" );
145         }
146         catch ( ReleaseExecutionException e )
147         {
148             assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
149         }
150         
151         // verify
152         verify( mock ).executeGoals( eq( testFile ),
153                                      eq( "clean integration-test" ),
154                                      isA( ReleaseEnvironment.class ),
155                                      eq( true ),
156                                      isNull( String.class ),
157                                      isNull( String.class ),
158                                      isA( ReleaseResult.class ) );
159         verifyNoMoreInteractions( mock );
160     }
161 
162     public void testSimulateException() throws MavenExecutorException
163     {
164         // prepare
165         File testFile = getTestFile( "target/working-directory" );
166 
167         ReleaseDescriptor config = new ReleaseDescriptor();
168         config.setCompletionGoals( "clean integration-test" );
169         config.setWorkingDirectory( testFile.getAbsolutePath() );
170 
171         MavenExecutor mock = mock( MavenExecutor.class );
172         doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
173                                                                                                    eq( "clean integration-test" ),
174                                                                                                    isA( ReleaseEnvironment.class ),
175                                                                                                    eq( true ),
176                                                                                                    isNull( String.class ),
177                                                                                                    isNull( String.class ),
178                                                                                                    isA( ReleaseResult.class ) );
179 
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     public void testEmptyGoals()
207         throws Exception
208     {
209         // prepare
210         File testFile = getTestFile( "target/working-directory" );
211 
212         ReleaseDescriptor config = new ReleaseDescriptor();
213         config.setCompletionGoals( "" );
214         config.setWorkingDirectory( testFile.getAbsolutePath() );
215 
216         MavenExecutor mock = mock( MavenExecutor.class );
217 
218         phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
219 
220         // execute
221         phase.execute( config, (Settings) null, (List<MavenProject>) null );
222 
223         // verify
224         // never invoke mock
225         verifyNoMoreInteractions( mock );
226     }
227 }