Author: rfscholte
Date: Thu Feb 23 22:16:15 2012
New Revision: 1292987

URL: http://svn.apache.org/viewvc?rev=1292987&view=rev
Log:
MRELEASE-741: migrate to mockito
Fixed ForkedMavenExecutorTest
No tests excluded anymore!
 

Modified:
    maven/release/trunk/maven-release-manager/pom.xml
    
maven/release/trunk/maven-release-manager/src/test/java/org/apache/maven/shared/release/exec/ForkedMavenExecutorTest.java

Modified: maven/release/trunk/maven-release-manager/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/release/trunk/maven-release-manager/pom.xml?rev=1292987&r1=1292986&r2=1292987&view=diff
==============================================================================
--- maven/release/trunk/maven-release-manager/pom.xml (original)
+++ maven/release/trunk/maven-release-manager/pom.xml Thu Feb 23 22:16:15 2012
@@ -233,10 +233,6 @@
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
           <argLine>-Xmx256m</argLine>
-          <excludes>
-            <!-- Disabled for now - we can't mock classes from plexus-utils in 
surefire -->
-            <exclude>**/ForkedMavenExecutorTest.java</exclude>
-          </excludes>
           <systemPropertyVariables>
             
<settings.security>${project.build.testOutputDirectory}/settings-security.xml</settings.security>
           </systemPropertyVariables>

Modified: 
maven/release/trunk/maven-release-manager/src/test/java/org/apache/maven/shared/release/exec/ForkedMavenExecutorTest.java
URL: 
http://svn.apache.org/viewvc/maven/release/trunk/maven-release-manager/src/test/java/org/apache/maven/shared/release/exec/ForkedMavenExecutorTest.java?rev=1292987&r1=1292986&r2=1292987&view=diff
==============================================================================
--- 
maven/release/trunk/maven-release-manager/src/test/java/org/apache/maven/shared/release/exec/ForkedMavenExecutorTest.java
 (original)
+++ 
maven/release/trunk/maven-release-manager/src/test/java/org/apache/maven/shared/release/exec/ForkedMavenExecutorTest.java
 Thu Feb 23 22:16:15 2012
@@ -19,18 +19,25 @@ package org.apache.maven.shared.release.
  * under the License.
  */
 
+import static org.mockito.Matchers.endsWith;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
 import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
 
 import org.apache.maven.shared.release.ReleaseResult;
 import org.codehaus.plexus.PlexusTestCase;
-import org.codehaus.plexus.util.StringInputStream;
+import org.codehaus.plexus.util.cli.Arg;
 import org.codehaus.plexus.util.cli.CommandLineException;
 import org.codehaus.plexus.util.cli.Commandline;
-import org.jmock.cglib.Mock;
-import org.jmock.core.constraint.IsEqual;
-import org.jmock.core.matcher.InvokeOnceMatcher;
-import org.jmock.core.stub.ReturnStub;
-import org.jmock.core.stub.ThrowStub;
 
 /**
  * Test the forked Maven executor.
@@ -51,98 +58,163 @@ public class ForkedMavenExecutorTest
     }
 
     public void testExecution()
-        throws MavenExecutorException
+        throws Exception
     {
+        // prepare
         File workingDirectory = getTestFile( "target/working-directory" );
+        Process mockProcess = mock( Process.class );
+        when( mockProcess.getInputStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getErrorStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getOutputStream() ).thenReturn( mock( 
OutputStream.class ) );
+        when( mockProcess.waitFor() ).thenReturn( 0 );
+
+        Commandline commandLineMock = mock( Commandline.class );
+        when( commandLineMock.execute() ).thenReturn( mockProcess );
+
+        Arg valueArgument = mock( Arg.class );
+        when( commandLineMock.createArg() ).thenReturn( valueArgument );
+        
+        CommandLineFactory commandLineFactoryMock = mock( 
CommandLineFactory.class );
+        when( commandLineFactoryMock.createCommandLine( isA( String.class ) 
/*"mvn"*/ ) ).thenReturn( commandLineMock );
 
-        Process process = createMockProcess( 0 );
-
-        Mock commandLineMock = createMockCommandLine( workingDirectory, 
process );
-        expectDefaultArguments( commandLineMock );
-
-        Mock mock = new Mock( CommandLineFactory.class );
-
-        mock.expects( new InvokeOnceMatcher() ).method( "createCommandLine" 
).with( new IsEqual( "mvn" ) ).will(
-            new ReturnStub( commandLineMock.proxy() ) );
-
-        executor.setCommandLineFactory( (CommandLineFactory) mock.proxy() );
+        executor.setCommandLineFactory( commandLineFactoryMock );
 
+        // execute
         executor.executeGoals( workingDirectory, "clean integration-test", 
false, null, new ReleaseResult() );
 
-        assertTrue( true );
+        // verify
+        verify( mockProcess ).getInputStream();
+        verify( mockProcess ).getErrorStream();
+        verify( mockProcess ).getOutputStream();
+        verify( mockProcess ).waitFor();
+        verify( commandLineMock ).setWorkingDirectory( 
workingDirectory.getAbsolutePath() );
+        verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" 
);
+        verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( 
String.class ) );
+        verify( commandLineMock ).execute();
+        verify( commandLineMock, times( 4 ) ).createArg();
+        verify( valueArgument ).setValue( "clean" );
+        verify( valueArgument ).setValue( "integration-test" );
+        verify( valueArgument ).setValue( "--no-plugin-updates" );
+        verify( valueArgument ).setValue( "--batch-mode" );
+        verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) 
);
+        
+        verifyNoMoreInteractions( mockProcess, commandLineFactoryMock, 
commandLineMock, valueArgument );
     }
 
     public void testExecutionWithCustomPomFile()
-        throws MavenExecutorException
+        throws Exception
     {
         File workingDirectory = getTestFile( "target/working-directory" );
+        Process mockProcess = mock( Process.class );
+        when( mockProcess.getInputStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getErrorStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getOutputStream() ).thenReturn( mock( 
OutputStream.class ) );
+        when( mockProcess.waitFor() ).thenReturn( 0 );
+        
+        Commandline commandLineMock = mock( Commandline.class );
+        when( commandLineMock.execute() ).thenReturn( mockProcess );
+        
+        Arg argMock = mock( Arg.class );
+        when( commandLineMock.createArg() ).thenReturn( argMock );
+        
+        CommandLineFactory commandLineFactoryMock = mock( 
CommandLineFactory.class );
+        when( commandLineFactoryMock.createCommandLine( isA( String.class ) /* 
"mvn" */ ) ).thenReturn( commandLineMock );
 
-        Process process = createMockProcess( 0 );
-
-        Mock commandLineMock = createMockCommandLine( workingDirectory, 
process );
-        expectDefaultArguments( commandLineMock );
-
-        String arguments = "-f my-pom.xml";
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"createArgument" ).will(
-            new ReturnStub( createArgumentLineMock( arguments ) ) );
-
-        Mock mock = new Mock( CommandLineFactory.class );
-
-        mock.expects( new InvokeOnceMatcher() ).method( "createCommandLine" 
).with( new IsEqual( "mvn" ) ).will(
-            new ReturnStub( commandLineMock.proxy() ) );
-
-        executor.setCommandLineFactory( (CommandLineFactory) mock.proxy() );
+        executor.setCommandLineFactory( commandLineFactoryMock );
 
+        // execute
         executor.executeGoals( workingDirectory, "clean integration-test", 
false, null, "my-pom.xml",
                                new ReleaseResult() );
-
-        assertTrue( true );
+        // verify
+        verify( mockProcess ).getInputStream();
+        verify( mockProcess ).getErrorStream();
+        verify( mockProcess ).getOutputStream();
+        verify( mockProcess ).waitFor();
+        verify( commandLineMock ).setWorkingDirectory( 
workingDirectory.getAbsolutePath() );
+        verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" 
);
+        verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( 
String.class ) );
+        verify( commandLineMock ).execute();
+        verify( commandLineMock, times( 6 ) ).createArg();
+        verify( argMock ).setValue( "clean" );
+        verify( argMock ).setValue( "integration-test" );
+        verify( argMock ).setValue( "-f" );
+        verify( argMock ).setValue( "my-pom.xml" );
+        verify( argMock ).setValue( "--no-plugin-updates" );
+        verify( argMock ).setValue( "--batch-mode" );
+        verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) 
);
+        
+        verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, 
commandLineFactoryMock );
     }
 
     public void testExecutionWithArguments()
-        throws MavenExecutorException
+        throws Exception
     {
         File workingDirectory = getTestFile( "target/working-directory" );
+        Process mockProcess = mock( Process.class );
+        when( mockProcess.getInputStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getErrorStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getOutputStream() ).thenReturn( mock( 
OutputStream.class ) );
+        when( mockProcess.waitFor() ).thenReturn( 0 );
+
+        Commandline commandLineMock = mock( Commandline.class );
+        when( commandLineMock.execute() ).thenReturn( mockProcess );
+        
+        Arg argMock = mock( Arg.class );
+        when( commandLineMock.createArg() ).thenReturn( argMock );
 
-        Process process = createMockProcess( 0 );
-
-        Mock commandLineMock = createMockCommandLine( workingDirectory, 
process );
-        String arguments = "-DperformRelease=true -Dmaven.test.skip=true";
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"createArgument" ).will(
-            new ReturnStub( createArgumentLineMock( arguments ) ) );
-
-        expectDefaultArguments( commandLineMock );
-
-        Mock mock = new Mock( CommandLineFactory.class );
-
-        mock.expects( new InvokeOnceMatcher() ).method( "createCommandLine" 
).with( new IsEqual( "mvn" ) ).will(
-            new ReturnStub( commandLineMock.proxy() ) );
+        CommandLineFactory commandLineFactoryMock = mock( 
CommandLineFactory.class );
+        when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) 
).thenReturn( commandLineMock );
 
-        executor.setCommandLineFactory( (CommandLineFactory) mock.proxy() );
+        executor.setCommandLineFactory( commandLineFactoryMock );
 
-        executor.executeGoals( workingDirectory, "clean integration-test", 
false, arguments, null );
+        // execute
+        String arguments = "-DperformRelease=true -Dmaven.test.skip=true";
+        executor.executeGoals( workingDirectory, "clean integration-test", 
false, arguments, new ReleaseResult() );
 
-        assertTrue( true );
+        // verify
+        verify( mockProcess ).getInputStream();
+        verify( mockProcess ).getErrorStream();
+        verify( mockProcess ).getOutputStream();
+        verify( mockProcess ).waitFor();
+        verify( commandLineMock ).setWorkingDirectory( 
workingDirectory.getAbsolutePath() );
+        verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" 
);
+        verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( 
String.class ) );
+        verify( commandLineMock ).execute();
+        verify( commandLineMock, times( 5 ) ).createArg();
+        verify( argMock ).setValue( "clean" );
+        verify( argMock ).setValue( "integration-test" );
+        verify( argMock ).setValue( "--no-plugin-updates" );
+        verify( argMock ).setValue( "--batch-mode" );
+        verify( argMock ).setLine( "-DperformRelease=true 
-Dmaven.test.skip=true" );
+        verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) 
);
+        
+        verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, 
commandLineFactoryMock );
     }
 
     public void testExecutionWithNonZeroExitCode()
-        throws MavenExecutorException
+        throws Exception
     {
+        // prepare
         File workingDirectory = getTestFile( "target/working-directory" );
+        Process mockProcess = mock( Process.class );
+        when( mockProcess.getInputStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getErrorStream() ).thenReturn( mock( 
InputStream.class ) );
+        when( mockProcess.getOutputStream() ).thenReturn( mock( 
OutputStream.class ) );
+        when( mockProcess.waitFor() ).thenReturn( 1 );
+        when( mockProcess.exitValue() ).thenReturn( 1 ); // why was this here 
in the original test?
+
+        Commandline commandLineMock = mock( Commandline.class );
+        when( commandLineMock.execute() ).thenReturn( mockProcess );
+        
+        Arg argMock = mock( Arg.class );
+        when( commandLineMock.createArg() ).thenReturn( argMock );
+        
+        CommandLineFactory commandLineFactoryMock = mock( 
CommandLineFactory.class );
+        when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) 
).thenReturn( commandLineMock );
 
-        Process process = createMockProcess( 1 );
-
-        Mock commandLineMock = createMockCommandLine( workingDirectory, 
process );
-
-        expectDefaultArguments( commandLineMock );
-
-        Mock mock = new Mock( CommandLineFactory.class );
-
-        mock.expects( new InvokeOnceMatcher() ).method( "createCommandLine" 
).with( new IsEqual( "mvn" ) ).will(
-            new ReturnStub( commandLineMock.proxy() ) );
-
-        executor.setCommandLineFactory( (CommandLineFactory) mock.proxy() );
+        executor.setCommandLineFactory( commandLineFactoryMock );
 
+        // execute
         try
         {
             executor.executeGoals( workingDirectory, "clean integration-test", 
false, null, new ReleaseResult() );
@@ -153,30 +225,45 @@ public class ForkedMavenExecutorTest
         {
             assertEquals( "Check exit code", 1, e.getExitCode() );
         }
+        
+        // verify
+        verify( mockProcess ).getInputStream();
+        verify( mockProcess ).getErrorStream();
+        verify( mockProcess ).getOutputStream();
+        verify( mockProcess ).waitFor();
+//        verify( mockProcess ).exitValue();
+        verify( commandLineMock ).setWorkingDirectory( 
workingDirectory.getAbsolutePath() );
+        verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" 
);
+        verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( 
String.class ) );
+        verify( commandLineMock ).execute();
+        verify( commandLineMock, times( 4 ) ).createArg();
+        verify( argMock ).setValue( "clean" );
+        verify( argMock ).setValue( "integration-test" );
+        verify( argMock ).setValue( "--no-plugin-updates" );
+        verify( argMock ).setValue( "--batch-mode" );
+        verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) 
);
+        
+        verifyNoMoreInteractions( mockProcess, commandLineMock, argMock, 
commandLineFactoryMock );
     }
 
     public void testExecutionWithCommandLineException()
-        throws MavenExecutorException
+        throws Exception
     {
+        // prepare
         File workingDirectory = getTestFile( "target/working-directory" );
 
-        Mock commandLineMock = new Mock( Commandline.class );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"setWorkingDirectory" ).with(
-            new IsEqual( workingDirectory.getAbsolutePath() ) );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"addEnvironment" ).with(
-            new IsEqual( "MAVEN_TERMINATE_CMD" ), new IsEqual( "on" ) );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( "execute" 
).will(
-            new ThrowStub( new CommandLineException( "..." ) ) );
-
-        expectDefaultArguments( commandLineMock );
+        Commandline commandLineMock = mock( Commandline.class );
+        when( commandLineMock.execute() ).thenThrow( new CommandLineException( 
"..." ) );
 
-        Mock mock = new Mock( CommandLineFactory.class );
-
-        mock.expects( new InvokeOnceMatcher() ).method( "createCommandLine" 
).with( new IsEqual( "mvn" ) ).will(
-            new ReturnStub( commandLineMock.proxy() ) );
-
-        executor.setCommandLineFactory( (CommandLineFactory) mock.proxy() );
+        Arg argMock = mock( Arg.class );
+        when ( commandLineMock.createArg() ).thenReturn( argMock );
+        
+        CommandLineFactory commandLineFactoryMock = mock( 
CommandLineFactory.class );
+        when( commandLineFactoryMock.createCommandLine( endsWith( "mvn" ) ) 
).thenReturn( commandLineMock );
+        
+        executor.setCommandLineFactory( commandLineFactoryMock );
 
+        // execute
         try
         {
             executor.executeGoals( workingDirectory, "clean integration-test", 
false, null, new ReleaseResult() );
@@ -187,58 +274,19 @@ public class ForkedMavenExecutorTest
         {
             assertEquals( "Check cause", CommandLineException.class, 
e.getCause().getClass() );
         }
-    }
-
-    private static void expectDefaultArguments( Mock commandLineMock )
-    {
-        String[] args = new String[]{"clean", "integration-test", 
"--no-plugin-updates", "--batch-mode"};
-        for ( int i = args.length - 1; i >= 0; i-- )
-        {
-            commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"createArgument" ).will(
-                new ReturnStub( createArgumentValueMock( args[i] ) ) );
-        }
-    }
-
-    private static Mock createMockCommandLine( File workingDirectory, Process 
process )
-    {
-        Mock commandLineMock = new Mock( Commandline.class );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"setWorkingDirectory" ).with(
-            new IsEqual( workingDirectory.getAbsolutePath() ) );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( 
"addEnvironment" ).with(
-            new IsEqual( "MAVEN_TERMINATE_CMD" ), new IsEqual( "on" ) );
-        commandLineMock.expects( new InvokeOnceMatcher() ).method( "execute" 
).will( new ReturnStub( process ) );
-
-        return commandLineMock;
-    }
-
-    private static Commandline.Argument createArgumentValueMock( String value )
-    {
-        Mock mock = new Mock( Commandline.Argument.class );
-        mock.expects( new InvokeOnceMatcher() ).method( "setValue" ).with( new 
IsEqual( value ) );
-        return (Commandline.Argument) mock.proxy();
-    }
 
-    private static Commandline.Argument createArgumentLineMock( String value )
-    {
-        Mock mock = new Mock( Commandline.Argument.class );
-        mock.expects( new InvokeOnceMatcher() ).method( "setLine" ).with( new 
IsEqual( value ) );
-        return (Commandline.Argument) mock.proxy();
-    }
-
-    private static Process createMockProcess( int exitCode )
-    {
-        Mock mockProcess = new Mock( Process.class );
-        mockProcess.expects( new InvokeOnceMatcher() ).method( 
"getInputStream" ).will(
-            new ReturnStub( new StringInputStream( "" ) ) );
-        mockProcess.expects( new InvokeOnceMatcher() ).method( 
"getErrorStream" ).will(
-            new ReturnStub( new StringInputStream( "" ) ) );
-        mockProcess.expects( new InvokeOnceMatcher() ).method( "waitFor" 
).will(
-            new ReturnStub( new Integer( exitCode ) ) );
-        if ( exitCode != 0 )
-        {
-            mockProcess.expects( new InvokeOnceMatcher() ).method( "exitValue" 
).will(
-                new ReturnStub( new Integer( exitCode ) ) );
-        }
-        return (Process) mockProcess.proxy();
+        // verify
+        verify( commandLineMock ).setWorkingDirectory( 
workingDirectory.getAbsolutePath() );
+        verify( commandLineMock ).addEnvironment( "MAVEN_TERMINATE_CMD", "on" 
);
+        verify( commandLineMock ).addEnvironment( eq( "M2_HOME" ), isNull( 
String.class ) );
+        verify( commandLineMock ).execute();
+        verify( commandLineMock, times( 4 ) ).createArg();
+        verify( argMock ).setValue( "clean" );
+        verify( argMock ).setValue( "integration-test" );
+        verify( argMock ).setValue( "--no-plugin-updates" );
+        verify( argMock ).setValue( "--batch-mode" );
+        verify( commandLineFactoryMock ).createCommandLine( endsWith( "mvn" ) 
);
+        
+        verifyNoMoreInteractions( commandLineMock, argMock, 
commandLineFactoryMock );
     }
-}
+}
\ No newline at end of file


Reply via email to