View Javadoc

1   package org.apache.maven.shared.release.exec;
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.endsWith;
23  import static org.mockito.Matchers.eq;
24  import static org.mockito.Matchers.isA;
25  import static org.mockito.Matchers.isNull;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.times;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.verifyNoMoreInteractions;
30  import static org.mockito.Mockito.when;
31  
32  import java.io.File;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import org.apache.maven.shared.release.ReleaseResult;
37  import org.codehaus.plexus.PlexusTestCase;
38  import org.codehaus.plexus.util.cli.Arg;
39  import org.codehaus.plexus.util.cli.CommandLineException;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  /**
43   * Test the forked Maven executor.
44   *
45   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
46   */
47  public class ForkedMavenExecutorTest
48      extends PlexusTestCase
49  {
50      private ForkedMavenExecutor executor;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          executor = (ForkedMavenExecutor) lookup( MavenExecutor.ROLE, "forked-path" );
58      }
59  
60      public void testExecution()
61          throws Exception
62      {
63          // prepare
64          File workingDirectory = getTestFile( "target/working-directory" );
65          Process mockProcess = mock( Process.class );
66          when( mockProcess.getInputStream() ).thenReturn( mock( InputStream.class ) );
67          when( mockProcess.getErrorStream() ).thenReturn( mock( InputStream.class ) );
68          when( mockProcess.getOutputStream() ).thenReturn( mock( OutputStream.class ) );
69          when( mockProcess.waitFor() ).thenReturn( 0 );
70  
71          Commandline commandLineMock = mock( Commandline.class );
72          when( commandLineMock.execute() ).thenReturn( mockProcess );
73  
74          Arg valueArgument = mock( Arg.class );
75          when( commandLineMock.createArg() ).thenReturn( valueArgument );
76          
77          CommandLineFactory commandLineFactoryMock = mock( CommandLineFactory.class );
78          when( commandLineFactoryMock.createCommandLine( isA( String.class ) /*"mvn"*/ ) ).thenReturn( commandLineMock );
79  
80          executor.setCommandLineFactory( commandLineFactoryMock );
81  
82          // execute
83          executor.executeGoals( workingDirectory, "clean integration-test", false, null, new ReleaseResult() );
84  
85          // verify
86          verify( mockProcess ).getInputStream();
87          verify( mockProcess ).getErrorStream();
88          verify( mockProcess ).getOutputStream();
89          verify( mockProcess ).waitFor();
90          verify( commandLineMock ).setWorkingDirectory( workingDirectory.getAbsolutePath() );
91          verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
92          verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( String.class ) );
93          verify( commandLineMock ).execute();
94          verify( commandLineMock, times( 4 ) ).createArg();
95          verify( valueArgument ).setValue( "clean" );
96          verify( valueArgument ).setValue( "integration-test" );
97          verify( valueArgument ).setValue( "--no-plugin-updates" );
98          verify( valueArgument ).setValue( "--batch-mode" );
99          verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) );
100         
101         verifyNoMoreInteractions( mockProcess, commandLineFactoryMock, commandLineMock, valueArgument );
102     }
103 
104     public void testExecutionWithCustomPomFile()
105         throws Exception
106     {
107         File workingDirectory = getTestFile( "target/working-directory" );
108         Process mockProcess = mock( Process.class );
109         when( mockProcess.getInputStream() ).thenReturn( mock( InputStream.class ) );
110         when( mockProcess.getErrorStream() ).thenReturn( mock( InputStream.class ) );
111         when( mockProcess.getOutputStream() ).thenReturn( mock( OutputStream.class ) );
112         when( mockProcess.waitFor() ).thenReturn( 0 );
113         
114         Commandline commandLineMock = mock( Commandline.class );
115         when( commandLineMock.execute() ).thenReturn( mockProcess );
116         
117         Arg argMock = mock( Arg.class );
118         when( commandLineMock.createArg() ).thenReturn( argMock );
119         
120         CommandLineFactory commandLineFactoryMock = mock( CommandLineFactory.class );
121         when( commandLineFactoryMock.createCommandLine( isA( String.class ) /* "mvn" */ ) ).thenReturn( commandLineMock );
122 
123         executor.setCommandLineFactory( commandLineFactoryMock );
124 
125         // execute
126         executor.executeGoals( workingDirectory, "clean integration-test", false, null, "my-pom.xml",
127                                new ReleaseResult() );
128         // verify
129         verify( mockProcess ).getInputStream();
130         verify( mockProcess ).getErrorStream();
131         verify( mockProcess ).getOutputStream();
132         verify( mockProcess ).waitFor();
133         verify( commandLineMock ).setWorkingDirectory( workingDirectory.getAbsolutePath() );
134         verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
135         verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( String.class ) );
136         verify( commandLineMock ).execute();
137         verify( commandLineMock, times( 6 ) ).createArg();
138         verify( argMock ).setValue( "clean" );
139         verify( argMock ).setValue( "integration-test" );
140         verify( argMock ).setValue( "-f" );
141         verify( argMock ).setValue( "my-pom.xml" );
142         verify( argMock ).setValue( "--no-plugin-updates" );
143         verify( argMock ).setValue( "--batch-mode" );
144         verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) );
145         
146         verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, commandLineFactoryMock );
147     }
148 
149     public void testExecutionWithArguments()
150         throws Exception
151     {
152         File workingDirectory = getTestFile( "target/working-directory" );
153         Process mockProcess = mock( Process.class );
154         when( mockProcess.getInputStream() ).thenReturn( mock( InputStream.class ) );
155         when( mockProcess.getErrorStream() ).thenReturn( mock( InputStream.class ) );
156         when( mockProcess.getOutputStream() ).thenReturn( mock( OutputStream.class ) );
157         when( mockProcess.waitFor() ).thenReturn( 0 );
158 
159         Commandline commandLineMock = mock( Commandline.class );
160         when( commandLineMock.execute() ).thenReturn( mockProcess );
161         
162         Arg argMock = mock( Arg.class );
163         when( commandLineMock.createArg() ).thenReturn( argMock );
164 
165         CommandLineFactory commandLineFactoryMock = mock( CommandLineFactory.class );
166         when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) ).thenReturn( commandLineMock );
167 
168         executor.setCommandLineFactory( commandLineFactoryMock );
169 
170         // execute
171         String arguments = "-DperformRelease=true -Dmaven.test.skip=true";
172         executor.executeGoals( workingDirectory, "clean integration-test", false, arguments, new ReleaseResult() );
173 
174         // verify
175         verify( mockProcess ).getInputStream();
176         verify( mockProcess ).getErrorStream();
177         verify( mockProcess ).getOutputStream();
178         verify( mockProcess ).waitFor();
179         verify( commandLineMock ).setWorkingDirectory( workingDirectory.getAbsolutePath() );
180         verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
181         verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( String.class ) );
182         verify( commandLineMock ).execute();
183         verify( commandLineMock, times( 5 ) ).createArg();
184         verify( argMock ).setValue( "clean" );
185         verify( argMock ).setValue( "integration-test" );
186         verify( argMock ).setValue( "--no-plugin-updates" );
187         verify( argMock ).setValue( "--batch-mode" );
188         verify( argMock ).setLine( "-DperformRelease=true -Dmaven.test.skip=true" );
189         verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) );
190         
191         verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, commandLineFactoryMock );
192     }
193 
194     public void testExecutionWithNonZeroExitCode()
195         throws Exception
196     {
197         // prepare
198         File workingDirectory = getTestFile( "target/working-directory" );
199         Process mockProcess = mock( Process.class );
200         when( mockProcess.getInputStream() ).thenReturn( mock( InputStream.class ) );
201         when( mockProcess.getErrorStream() ).thenReturn( mock( InputStream.class ) );
202         when( mockProcess.getOutputStream() ).thenReturn( mock( OutputStream.class ) );
203         when( mockProcess.waitFor() ).thenReturn( 1 );
204         when( mockProcess.exitValue() ).thenReturn( 1 ); // why was this here in the original test?
205 
206         Commandline commandLineMock = mock( Commandline.class );
207         when( commandLineMock.execute() ).thenReturn( mockProcess );
208         
209         Arg argMock = mock( Arg.class );
210         when( commandLineMock.createArg() ).thenReturn( argMock );
211         
212         CommandLineFactory commandLineFactoryMock = mock( CommandLineFactory.class );
213         when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) ).thenReturn( commandLineMock );
214 
215         executor.setCommandLineFactory( commandLineFactoryMock );
216 
217         // execute
218         try
219         {
220             executor.executeGoals( workingDirectory, "clean integration-test", false, null, new ReleaseResult() );
221 
222             fail( "Should have thrown an exception" );
223         }
224         catch ( MavenExecutorException e )
225         {
226             assertEquals( "Check exit code", 1, e.getExitCode() );
227         }
228         
229         // verify
230         verify( mockProcess ).getInputStream();
231         verify( mockProcess ).getErrorStream();
232         verify( mockProcess ).getOutputStream();
233         verify( mockProcess ).waitFor();
234 //        verify( mockProcess ).exitValue();
235         verify( commandLineMock ).setWorkingDirectory( workingDirectory.getAbsolutePath() );
236         verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
237         verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( String.class ) );
238         verify( commandLineMock ).execute();
239         verify( commandLineMock, times( 4 ) ).createArg();
240         verify( argMock ).setValue( "clean" );
241         verify( argMock ).setValue( "integration-test" );
242         verify( argMock ).setValue( "--no-plugin-updates" );
243         verify( argMock ).setValue( "--batch-mode" );
244         verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) );
245         
246         verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, commandLineFactoryMock );
247     }
248 
249     public void testExecutionWithCommandLineException()
250         throws Exception
251     {
252         // prepare
253         File workingDirectory = getTestFile( "target/working-directory" );
254 
255         Commandline commandLineMock = mock( Commandline.class );
256         when( commandLineMock.execute() ).thenThrow( new CommandLineException( "..." ) );
257 
258         Arg argMock = mock( Arg.class );
259         when ( commandLineMock.createArg() ).thenReturn( argMock );
260         
261         CommandLineFactory commandLineFactoryMock = mock( CommandLineFactory.class );
262         when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) ).thenReturn( commandLineMock );
263         
264         executor.setCommandLineFactory( commandLineFactoryMock );
265 
266         // execute
267         try
268         {
269             executor.executeGoals( workingDirectory, "clean integration-test", false, null, new ReleaseResult() );
270 
271             fail( "Should have thrown an exception" );
272         }
273         catch ( MavenExecutorException e )
274         {
275             assertEquals( "Check cause", CommandLineException.class, e.getCause().getClass() );
276         }
277 
278         // verify
279         verify( commandLineMock ).setWorkingDirectory( workingDirectory.getAbsolutePath() );
280         verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
281         verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( String.class ) );
282         verify( commandLineMock ).execute();
283         verify( commandLineMock, times( 4 ) ).createArg();
284         verify( argMock ).setValue( "clean" );
285         verify( argMock ).setValue( "integration-test" );
286         verify( argMock ).setValue( "--no-plugin-updates" );
287         verify( argMock ).setValue( "--batch-mode" );
288         verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) );
289         
290         verifyNoMoreInteractions( commandLineMock, argMock, commandLineFactoryMock );
291     }
292 }