diff --git a/pom.xml b/pom.xml index 0b27d7c83..1bf018ec3 100644 --- a/pom.xml +++ b/pom.xml @@ -204,18 +204,6 @@ under the License. 2.28.2 test - - org.easymock - easymockclassextension - 2.5.2 - test - - - org.easymock - easymock - 2.5.2 - test - jdom jdom diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/DefaultAssemblyArchiverTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/DefaultAssemblyArchiverTest.java index 4294b66e5..dd41811c0 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/DefaultAssemblyArchiverTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/DefaultAssemblyArchiverTest.java @@ -19,14 +19,28 @@ * under the License. */ -import org.apache.maven.artifact.repository.ArtifactRepository; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import org.apache.maven.model.Model; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; import org.apache.maven.plugins.assembly.archive.phase.AssemblyArchiverPhase; -import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException; -import org.apache.maven.plugins.assembly.artifact.DependencyResolver; -import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.plugins.assembly.mojos.AbstractAssemblyMojo; import org.apache.maven.project.MavenProject; @@ -37,7 +51,6 @@ import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.diags.NoOpArchiver; import org.codehaus.plexus.archiver.manager.ArchiverManager; -import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; import org.codehaus.plexus.archiver.tar.TarArchiver; import org.codehaus.plexus.archiver.tar.TarLongFileMode; import org.codehaus.plexus.archiver.war.WarArchiver; @@ -45,30 +58,14 @@ import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.EasyMock; -import org.easymock.classextension.EasyMockSupport; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.easymock.EasyMock.anyBoolean; -import static org.easymock.EasyMock.anyInt; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.expectLastCall; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - +@RunWith( MockitoJUnitRunner.class ) public class DefaultAssemblyArchiverTest { @Rule @@ -78,462 +75,419 @@ public class DefaultAssemblyArchiverTest public static void setupInterpolators( AssemblerConfigurationSource configSource ) { - expect( configSource.getRepositoryInterpolator() ).andReturn( - FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getCommandLinePropsInterpolator() ).andReturn( - FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getMainProjectInterpolator() ).andReturn( - FixedStringSearchInterpolator.create() ).anyTimes(); - + when( configSource.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); } public static void setupInterpolators( AssemblerConfigurationSource configSource, MavenProject mavenProject ) { - expect( configSource.getRepositoryInterpolator() ).andReturn( - FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getCommandLinePropsInterpolator() ).andReturn( - FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes(); - expect( configSource.getMainProjectInterpolator() ).andReturn( - AbstractAssemblyMojo.mainProjectInterpolator( mavenProject ) ).anyTimes(); - + when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( configSource.getMainProjectInterpolator() ).thenReturn( AbstractAssemblyMojo.mainProjectInterpolator( mavenProject ) ); } @Before public void setup() throws PlexusContainerException { - container = new DefaultPlexusContainer(); + this.container = new DefaultPlexusContainer(); } @Test( expected = InvalidAssemblerConfigurationException.class ) public void failWhenAssemblyIdIsNull() - throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAssemblyArchiver macMgr = new MockAndControlForAssemblyArchiver( mm ); - - final AssemblerConfigurationSource configSource = - mm.createControl().createMock( AssemblerConfigurationSource.class ); - - mm.replayAll(); - - final DefaultAssemblyArchiver archiver = createSubject( macMgr, null, null ); - archiver.createArchive( new Assembly(), "full-name", "zip", configSource, false, null, null ); - - mm.verifyAll(); + final DefaultAssemblyArchiver archiver = createSubject( null, null, null ); + archiver.createArchive( new Assembly(), "full-name", "zip", null, false, null, null ); } @Test public void testCreateArchive() - throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException, - IOException, DependencyResolutionException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAssemblyArchiver macMgr = new MockAndControlForAssemblyArchiver( mm ); - - macMgr.expectGetArchiver( "zip", Archiver.class ); - macMgr.expectGetDestFile( new File( "test" ) ); - - final AssemblyArchiverPhase phase = mm.createControl().createMock( AssemblyArchiverPhase.class ); - - phase.execute( (Assembly) anyObject(), (Archiver) anyObject(), (AssemblerConfigurationSource) anyObject() ); + Archiver archiver = mock( Archiver.class ); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "zip" ) ).thenReturn( archiver ); - final AssemblerConfigurationSource configSource = - mm.createControl().createMock( AssemblerConfigurationSource.class ); - - final File tempDir = new File ( temporaryFolder.getRoot(), "temp" ); - - expect( configSource.getTemporaryRootDirectory() ).andReturn( tempDir ).anyTimes(); - expect( configSource.isDryRun() ).andReturn( false ).anyTimes(); - expect( configSource.isIgnoreDirFormatExtensions() ).andReturn( false ).anyTimes(); + final AssemblyArchiverPhase phase = mock( AssemblyArchiverPhase.class ); final File outDir = temporaryFolder.newFolder( "out" ); - macMgr.archiver.setDestFile( new File( outDir, "full-name.zip" ) ); - - try - { - macMgr.archiver.createArchive(); - } - catch ( final ArchiverException | IOException e ) - { - fail( "Should never happen" ); - } - - expect( configSource.getOverrideUid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideUserName() ).andReturn( "root" ).atLeastOnce(); - expect( configSource.getOverrideGid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideGroupName() ).andReturn( "root" ).atLeastOnce(); - expect( configSource.getOutputDirectory() ).andReturn( outDir ); - expect( configSource.getFinalName() ).andReturn( "finalName" ); - expect( configSource.getArchiverConfig() ).andReturn( null ).anyTimes(); - expect( configSource.getWorkingDirectory() ).andReturn( new File( "." ) ).anyTimes(); - expect( configSource.isUpdateOnly() ).andReturn( false ).anyTimes(); - expect( configSource.isIgnorePermissions() ).andReturn( false ).anyTimes(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getTemporaryRootDirectory() ).thenReturn( new File ( temporaryFolder.getRoot(), "temp" ) ); + when( configSource.getOverrideUid() ).thenReturn( 0 ); + when( configSource.getOverrideUserName() ).thenReturn( "root" ); + when( configSource.getOverrideGid() ).thenReturn( 0 ); + when( configSource.getOverrideGroupName() ).thenReturn( "root" ); + when( configSource.getOutputDirectory() ).thenReturn( outDir ); + when( configSource.getFinalName() ).thenReturn( "finalName" ); + when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) ); final Assembly assembly = new Assembly(); assembly.setId( "id" ); - // try - // { - // expect( macMgr.dependencyResolver.resolve( (Assembly) anyObject(), (AssemblerConfigurationSource) - // anyObject() )).andReturn( new HashSet( ) ); -// macMgr.dependencyResolverControl.setMatcher( MockControl.ALWAYS_MATCHER ); - // } - // catch ( final DependencyResolutionException e ) - // { - // fail( "Should never happen" ); - // } - - mm.replayAll(); - - final DefaultAssemblyArchiver subject = createSubject( macMgr, Collections.singletonList( phase ), null ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, Collections.singletonList( phase ), null ); subject.createArchive( assembly, "full-name", "zip", configSource, false, null, null ); - - mm.verifyAll(); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiverConfig(); + verify( configSource ).getFinalName(); + verify( configSource ).getOutputDirectory(); + verify( configSource, atLeastOnce() ).getOverrideUid(); + verify( configSource, atLeastOnce() ).getOverrideUserName(); + verify( configSource, atLeastOnce() ).getOverrideGid(); + verify( configSource, atLeastOnce() ).getOverrideGroupName(); + verify( configSource ).getTemporaryRootDirectory(); + verify( configSource ).getWorkingDirectory(); + verify( configSource ).isDryRun(); + verify( configSource ).isIgnoreDirFormatExtensions(); + verify( configSource ).isIgnorePermissions(); + verify( configSource, times( 2 ) ).isUpdateOnly(); + + verify( phase ).execute( eq( assembly ), any( Archiver.class ), eq( configSource ) ); + + verify( archiver ).createArchive(); + verify( archiver ).setDestFile( new File( outDir, "full-name.zip" ) ); + verify( archiver, times( 2 ) ).setForced( true ); + verify( archiver ).setIgnorePermissions( false ); + verify( archiver ).setOverrideUid( 0 ); + verify( archiver ).setOverrideUserName( "root" ); + verify( archiver ).setOverrideGid( 0 ); + verify( archiver ).setOverrideGroupName( "root" ); + + verify( archiverManager ).getArchiver( "zip" ); } @Test public void testCreateArchiver_ShouldConfigureArchiver() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); - final TestArchiverWithConfig archiver = new TestArchiverWithConfig(); - macArchiverManager.expectGetArchiver( "dummy", archiver ); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "dummy" ) ).thenReturn( archiver ); - final AssemblerConfigurationSource configSource = mm.createMock( AssemblerConfigurationSource.class ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); final String simpleConfig = "value"; - expect( configSource.getArchiverConfig() ).andReturn( - "" + simpleConfig + "" ).anyTimes(); + when( configSource.getArchiverConfig() ).thenReturn( + "" + simpleConfig + "" ); final MavenProject project = new MavenProject( new Model() ); - expect( configSource.getProject() ).andReturn( project ).anyTimes(); - - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); - - expect( configSource.isDryRun() ).andReturn( false ).anyTimes(); - - expect( configSource.getWorkingDirectory() ).andReturn( new File( "." ) ).anyTimes(); - - expect( configSource.isUpdateOnly() ).andReturn( false ).anyTimes(); + when( configSource.getProject() ).thenReturn( project ); + when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) ); - final ArtifactRepository lr = mm.createMock( ArtifactRepository.class ); + when( configSource.isIgnorePermissions() ).thenReturn( true ); + setupInterpolators( configSource ); - expect( lr.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes(); - - expect( configSource.getLocalRepository() ).andReturn( lr ).anyTimes(); - expect( configSource.isIgnorePermissions() ).andReturn( true ); - setupInterpolators( configSource, project ); - - expect( configSource.getOverrideUid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideUserName() ).andReturn( "root" ).atLeastOnce(); - expect( configSource.getOverrideGid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideGroupName() ).andReturn( "root" ).atLeastOnce(); - - mm.replayAll(); + when( configSource.getOverrideUid() ).thenReturn( 0 ); + when( configSource.getOverrideUserName() ).thenReturn( "root" ); + when( configSource.getOverrideGid() ).thenReturn( 0 ); + when( configSource.getOverrideGroupName() ).thenReturn( "root" ); final DefaultAssemblyArchiver subject = - createSubject( macArchiverManager, new ArrayList(), null ); + createSubject( archiverManager, new ArrayList(), null ); subject.createArchiver( "dummy", false, "finalName", configSource, null, false, null, null ); assertEquals( simpleConfig, archiver.getSimpleConfig() ); - - mm.verifyAll(); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "dummy" ); } @Test public void testCreateArchiver_ShouldCreateTarArchiverWithNoCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final TestTarArchiver ttArchiver = new TestTarArchiver(); - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); - - macArchiverManager.expectGetArchiver( "tar", ttArchiver ); - - final AssemblerConfigurationSource configSource = mm.createMock( AssemblerConfigurationSource.class ); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( ttArchiver ); - expect( configSource.getTarLongFileMode() ).andReturn( TarLongFileMode.fail.toString() ).anyTimes(); - expect( configSource.isDryRun() ).andReturn( false ).anyTimes(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getTarLongFileMode() ).thenReturn( TarLongFileMode.fail.toString() ); + when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) ); + when( configSource.isIgnorePermissions() ).thenReturn( true ); + when( configSource.getOverrideUid() ).thenReturn( 0 ); + when( configSource.getOverrideUserName() ).thenReturn( "root" ); + when( configSource.getOverrideGid() ).thenReturn( 0 ); + when( configSource.getOverrideGroupName() ).thenReturn( "root" ); - expect( configSource.getArchiverConfig() ).andReturn( null ).anyTimes(); - - final DefaultAssemblyArchiver subject = setupStdExpectations( mm, macArchiverManager, configSource ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createArchiver( "tar", false, "finalName", configSource, null, false, null, null ); assertNull( ttArchiver.compressionMethod ); assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); - mm.verifyAll(); - } - - private DefaultAssemblyArchiver setupStdExpectations( EasyMockSupport mm, - MockAndControlForAssemblyArchiver macArchiverManager, - AssemblerConfigurationSource configSource ) - { - expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes(); - - expect( configSource.getJarArchiveConfiguration() ).andReturn( null ).anyTimes(); - - expect( configSource.getWorkingDirectory() ).andReturn( new File( "." ) ).anyTimes(); - - expect( configSource.isUpdateOnly() ).andReturn( false ).anyTimes(); - - expect( configSource.isIgnorePermissions() ).andReturn( true ).anyTimes(); - - expect( configSource.getOverrideUid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideUserName() ).andReturn( "root" ).atLeastOnce(); - expect( configSource.getOverrideGid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideGroupName() ).andReturn( "root" ).atLeastOnce(); - - mm.replayAll(); - - return createSubject( macArchiverManager, new ArrayList(), null ); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiverConfig(); + verify( configSource, times( 2 ) ).getOverrideGid(); + verify( configSource, times( 2 ) ).getOverrideGroupName(); + verify( configSource, times( 2 ) ).getOverrideUid(); + verify( configSource, times( 2 ) ).getOverrideUserName(); + verify( configSource ).getTarLongFileMode(); + verify( configSource ).getWorkingDirectory(); + verify( configSource ).isDryRun(); + verify( configSource ).isIgnorePermissions(); + verify( configSource, times( 2 ) ).isUpdateOnly(); + + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateArchiver_ShouldCreateWarArchiverWithIgnoreWebxmlSetToFalse() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final TestWarArchiver twArchiver = new TestWarArchiver(); - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); - - macArchiverManager.expectGetArchiver( "war", twArchiver ); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "war" ) ).thenReturn( twArchiver ); - final AssemblerConfigurationSource configSource = mm.createMock( AssemblerConfigurationSource.class ); - - expect( configSource.isDryRun() ).andReturn( false ).anyTimes(); - expect( configSource.getArchiverConfig() ).andReturn( null ).anyTimes(); - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); - final DefaultAssemblyArchiver subject = setupStdExpectations( mm, macArchiverManager, configSource ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getOverrideGid() ).thenReturn( 0 ); + when( configSource.getOverrideGroupName() ).thenReturn( "root" ); + when( configSource.getOverrideUid() ).thenReturn( 0 ); + when( configSource.getOverrideUserName() ).thenReturn( "root" ); + when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) ); + when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) ); + when( configSource.isIgnorePermissions() ).thenReturn( true ); + + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createArchiver( "war", false, null, configSource, null, false, null, null ); assertFalse( twArchiver.ignoreWebxml ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiverConfig(); + verify( configSource ).getJarArchiveConfiguration(); + verify( configSource ).getMavenSession(); + verify( configSource, times( 2 ) ).getOverrideGid(); + verify( configSource, times( 2 ) ).getOverrideGroupName(); + verify( configSource, times( 2 ) ).getOverrideUid(); + verify( configSource, times( 2 ) ).getOverrideUserName(); + verify( configSource ).getProject(); + verify( configSource ).getWorkingDirectory(); + verify( configSource ).isDryRun(); + verify( configSource ).isIgnorePermissions(); + verify( configSource, times( 2 ) ).isUpdateOnly(); + + verify( archiverManager ).getArchiver( "war" ); } @Test public void testCreateArchiver_ShouldCreateZipArchiver() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final ZipArchiver archiver = new ZipArchiver(); - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "zip" ) ).thenReturn( archiver ); - macArchiverManager.expectGetArchiver( "zip", archiver ); - - final AssemblerConfigurationSource configSource = mm.createMock( AssemblerConfigurationSource.class ); - - expect( configSource.isDryRun() ).andReturn( false ).anyTimes(); - expect( configSource.getArchiverConfig() ).andReturn( null ).anyTimes(); - expect( configSource.getWorkingDirectory() ).andReturn( new File( "." ) ).anyTimes(); - expect( configSource.isUpdateOnly() ).andReturn( false ).anyTimes(); - expect( configSource.getJarArchiveConfiguration() ).andReturn( null ).anyTimes(); - expect( configSource.isIgnorePermissions() ).andReturn( true ).anyTimes(); - - expect( configSource.getOverrideUid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideUserName() ).andReturn( "root" ).atLeastOnce(); - expect( configSource.getOverrideGid() ).andReturn( 0 ).atLeastOnce(); - expect( configSource.getOverrideGroupName() ).andReturn( "root" ).atLeastOnce(); - - mm.replayAll(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getOverrideGid() ).thenReturn( 0 ); + when( configSource.getOverrideGroupName() ).thenReturn( "root" ); + when( configSource.getOverrideUid() ).thenReturn( 0 ); + when( configSource.getOverrideUserName() ).thenReturn( "root" ); + when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) ); + when( configSource.isIgnorePermissions() ).thenReturn( true ); final DefaultAssemblyArchiver subject = - createSubject( macArchiverManager, new ArrayList(), null ); + createSubject( archiverManager, new ArrayList(), null ); subject.createArchiver( "zip", false, null, configSource, null, false, null, null ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiverConfig(); + verify( configSource, times( 2 ) ).getOverrideGid(); + verify( configSource, times( 2 ) ).getOverrideGroupName(); + verify( configSource, times( 2 ) ).getOverrideUid(); + verify( configSource, times( 2 ) ).getOverrideUserName(); + verify( configSource ).getWorkingDirectory(); + verify( configSource ).isDryRun(); + verify( configSource ).isIgnorePermissions(); + verify( configSource, times( 2 ) ).isUpdateOnly(); + + verify( archiverManager ).getArchiver( "zip" ); } @Test public void testCreateWarArchiver_ShouldDisableIgnoreWebxmlOption() - throws NoSuchArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final TestWarArchiver twArchiver = new TestWarArchiver(); - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); - - macArchiverManager.expectGetArchiver( "war", twArchiver ); - - mm.replayAll(); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "war" ) ).thenReturn( twArchiver ); final DefaultAssemblyArchiver subject = - createSubject( macArchiverManager, new ArrayList(), null ); + createSubject( archiverManager, new ArrayList(), null ); subject.createWarArchiver(); assertFalse( twArchiver.ignoreWebxml ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "war" ); } @Test public void testCreateTarArchiver_ShouldNotInitializeCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); - final TestTarArchiver ttArchiver = new TestTarArchiver(); - - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tar", TarLongFileMode.fail ); - assertNull( ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); - - mm.verifyAll(); - } - - private DefaultAssemblyArchiver createSubject( EasyMockSupport mm, TestTarArchiver ttArchiver ) - throws NoSuchArchiverException - { - final MockAndControlForAssemblyArchiver macArchiverManager = new MockAndControlForAssemblyArchiver( mm ); - - macArchiverManager.expectGetArchiver( "tar", ttArchiver ); + assertNull( new TestTarArchiver().compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.replayAll(); - - return createSubject( macArchiverManager, new ArrayList(), null ); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_TarGzFormat_ShouldInitializeGZipCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final TestTarArchiver ttArchiver = new TestTarArchiver(); - - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); + + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tar.gz", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.gzip, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.gzip, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_TgzFormat_ShouldInitializeGZipCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final TestTarArchiver ttArchiver = new TestTarArchiver(); - - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); + + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tgz", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.gzip, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.gzip, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_TarBz2Format_ShouldInitializeBZipCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); - final TestTarArchiver ttArchiver = new TestTarArchiver(); - - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tar.bz2", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.bzip2, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.bzip2, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_Tbz2Format_ShouldInitializeBZipCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final TestTarArchiver ttArchiver = new TestTarArchiver(); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tbz2", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.bzip2, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.bzip2, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_TarXzFormat_ShouldInitializeXzCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final TestTarArchiver ttArchiver = new TestTarArchiver(); + final TestTarArchiver archiver = new TestTarArchiver(); - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); + + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "tar.xz", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.xz, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.xz, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_TXzFormat_ShouldInitializeXzCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); + final TestTarArchiver archiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver ); - final TestTarArchiver ttArchiver = new TestTarArchiver(); - - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); subject.createTarArchiver( "txz", TarLongFileMode.fail ); - assertEquals( TarArchiver.TarCompressionMethod.xz, ttArchiver.compressionMethod ); - assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode ); + assertEquals( TarArchiver.TarCompressionMethod.xz, archiver.compressionMethod ); + assertEquals( TarLongFileMode.fail, archiver.longFileMode ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } @Test public void testCreateTarArchiver_InvalidFormat_ShouldFailWithInvalidCompression() - throws NoSuchArchiverException, ArchiverException + throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final TestTarArchiver ttArchiver = new TestTarArchiver(); + + final ArchiverManager archiverManager = mock( ArchiverManager.class ); + when( archiverManager.getArchiver( "tar" ) ).thenReturn( ttArchiver ); - final DefaultAssemblyArchiver subject = createSubject( mm, ttArchiver ); + final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList(), null ); try { @@ -545,14 +499,15 @@ public void testCreateTarArchiver_InvalidFormat_ShouldFailWithInvalidCompression { // expected. } - - mm.verifyAll(); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiverManager ).getArchiver( "tar" ); } - private DefaultAssemblyArchiver createSubject( final MockAndControlForAssemblyArchiver macMgr, + private DefaultAssemblyArchiver createSubject( final ArchiverManager archiverManager, final List phases, Logger logger ) { - final DefaultAssemblyArchiver subject = new DefaultAssemblyArchiver( macMgr.archiverManager, phases ); + final DefaultAssemblyArchiver subject = new DefaultAssemblyArchiver( archiverManager, phases ); subject.setContainer( container ); @@ -630,73 +585,4 @@ public String getDuplicateBehavior() } } - private final class MockAndControlForAssemblyArchiver - { - final ArchiverManager archiverManager; - - final DependencyResolver dependencyResolver; - - private final EasyMockSupport mm; - - Archiver archiver; - - public MockAndControlForAssemblyArchiver( final EasyMockSupport mm ) - { - this.mm = mm; - archiverManager = mm.createControl().createMock( ArchiverManager.class ); - - dependencyResolver = mm.createControl().createMock( DependencyResolver.class ); - - } - - void expectGetDestFile( final File file ) - { - expect( archiver.getDestFile() ).andReturn( file ).anyTimes(); - } - - void createArchiver( final Class archiverClass ) - { - archiver = mm.createControl().createMock( archiverClass ); - - archiver.setForced( anyBoolean() ); - expectLastCall().anyTimes(); - - archiver.setIgnorePermissions( false ); - expectLastCall().anyTimes(); - - archiver.setOverrideUid( anyInt() ); - expectLastCall().anyTimes(); - - archiver.setOverrideUserName( EasyMock.anyObject() ); - expectLastCall().anyTimes(); - - archiver.setOverrideGid( anyInt() ); - expectLastCall().anyTimes(); - - archiver.setOverrideGroupName( EasyMock.anyObject() ); - expectLastCall().anyTimes(); - } - - void expectGetArchiver( final String format, final Class archiverClass ) - { - createArchiver( archiverClass ); - - try - { - expect( archiverManager.getArchiver( format ) ).andReturn( archiver ); - } - catch ( final NoSuchArchiverException e ) - { - Assert.fail( "should never happen" ); - } - - } - - void expectGetArchiver( final String format, final Archiver archiver ) - throws NoSuchArchiverException - { - expect( archiverManager.getArchiver( format ) ).andReturn( archiver ); - } - } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizerTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizerTest.java index 6e7e9e35d..6d332d4f4 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizerTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/ManifestCreationFinalizerTest.java @@ -37,14 +37,15 @@ import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.ArchiveFinalizer; -import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.util.IOUtil; -import org.easymock.classextension.EasyMockSupport; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class ManifestCreationFinalizerTest { @@ -62,18 +63,10 @@ public void testShouldDoNothingWhenArchiveConfigIsNull() public void testShouldDoNothingWhenArchiverIsNotJarArchiver() throws Exception { - EasyMockSupport mm = new EasyMockSupport(); - - MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm ); - MavenProject project = new MavenProject( new Model() ); MavenArchiveConfiguration config = new MavenArchiveConfiguration(); - mm.replayAll(); - - new ManifestCreationFinalizer( null, project, config ).finalizeArchiveCreation( macArchiver.archiver ); - - mm.verifyAll(); + new ManifestCreationFinalizer( null, project, config ).finalizeArchiveCreation( null ); } @Test @@ -147,24 +140,9 @@ public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver() IOUtil.copy( reader, writer ); - System.out.println( "Test Manifest:\n\n" + writer ); - assertTrue( writer.toString().contains( testKey + ": " + testValue ) ); // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678 ( (JarURLConnection) resource.openConnection() ).getJarFile().close(); } - - private final class MockAndControlForArchiver - { - final Archiver archiver; - - - MockAndControlForArchiver( EasyMockSupport mm ) - { - - archiver = mm.createMock( Archiver.class ); - } - } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/archiver/AssemblyProxyArchiverTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/archiver/AssemblyProxyArchiverTest.java index b823c93f3..6e2d7a3d2 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/archiver/AssemblyProxyArchiverTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/archiver/AssemblyProxyArchiverTest.java @@ -22,12 +22,22 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.annotation.Nonnull; + import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.FileSet; @@ -42,17 +52,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.mockito.junit.MockitoJUnitRunner; -import javax.annotation.Nonnull; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - +@RunWith( MockitoJUnitRunner.class ) public class AssemblyProxyArchiverTest { @Rule diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/DependencySetAssemblyPhaseTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/DependencySetAssemblyPhaseTest.java index c37b404a1..24d755b0e 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/DependencySetAssemblyPhaseTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/DependencySetAssemblyPhaseTest.java @@ -19,32 +19,48 @@ * under the License. */ +import static org.mockito.ArgumentMatchers.anyListOf; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import java.io.File; import java.io.IOException; import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Model; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddDependencySetsTask; import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException; import org.apache.maven.plugins.assembly.artifact.DependencyResolver; import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.plugins.assembly.model.DependencySet; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class DependencySetAssemblyPhaseTest { - - final EasyMockSupport mm = new EasyMockSupport(); + private DependencySetAssemblyPhase phase; + + private DependencyResolver dependencyResolver; + + @Before + public void setUp() + { + this.dependencyResolver = mock( DependencyResolver.class ); + + this.phase = new DependencySetAssemblyPhase( null, dependencyResolver, null ); + } @Test public void testExecute_ShouldAddOneDependencyFromProject() @@ -72,75 +88,47 @@ public void testExecute_ShouldAddOneDependencyFromProject() assembly.setIncludeBaseDirectory( false ); assembly.addDependencySet( ds ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - - macTask.expectCSGetRepositories( null, null ); - - macTask.expectGetDestFile( new File( "junk" ) ); -// macTask.expectAddFile( artifactFile, "out/dep", 10 ); - project.setArtifacts( Collections.singleton( artifact ) ); - macTask.expectCSGetFinalName( "final-name" ); - - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - - final MavenProject depProject = newMavenProject( "group", "dep", "1" ); - - macTask.expectBuildFromRepository( depProject ); - - macTask.expectResolveDependencySets(); - - mm.replayAll(); - - createPhase( macTask, logger, macTask.dependencyResolver ).execute( assembly, macTask.archiver, - macTask.configSource ); - - mm.verifyAll(); - } - - private MavenProject newMavenProject( final String groupId, final String artifactId, final String version ) - { - final Model model = new Model(); - model.setGroupId( groupId ); - model.setArtifactId( artifactId ); - model.setVersion( version ); + when( dependencyResolver.resolveDependencySets( eq( assembly ), + isNull(AssemblerConfigurationSource.class), + anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap>() ); + + this.phase.execute( assembly, null, null ); - return new MavenProject( model ); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( dependencyResolver ).resolveDependencySets( eq( assembly ), + isNull(AssemblerConfigurationSource.class), + anyListOf( DependencySet.class ) ); } @Test public void testExecute_ShouldNotAddDependenciesWhenProjectHasNone() - throws AssemblyFormattingException, ArchiveCreationException, IOException, - InvalidAssemblerConfigurationException, DependencyResolutionException + throws Exception { final Assembly assembly = new Assembly(); - assembly.setId( "test" ); assembly.setIncludeBaseDirectory( false ); + + when( dependencyResolver.resolveDependencySets( eq( assembly ), + isNull( AssemblerConfigurationSource.class ), + anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap>() ); - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - - final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mm, null ); - - macTask.expectResolveDependencySets(); - - mm.replayAll(); - - createPhase( macTask, logger, macTask.dependencyResolver ).execute( assembly, null, macTask.configSource ); + this.phase.execute( assembly, null, null ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( dependencyResolver ).resolveDependencySets( eq( assembly ), + isNull( AssemblerConfigurationSource.class ), + anyListOf( DependencySet.class ) ); } - - private DependencySetAssemblyPhase createPhase( final MockAndControlForAddDependencySetsTask macTask, - final Logger logger, DependencyResolver dr ) + + private MavenProject newMavenProject( final String groupId, final String artifactId, final String version ) { - final DependencySetAssemblyPhase phase = new DependencySetAssemblyPhase( null, dr, null ); - - phase.enableLogging( logger ); + final Model model = new Model(); + model.setGroupId( groupId ); + model.setArtifactId( artifactId ); + model.setVersion( version ); - return phase; + return new MavenProject( model ); } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileItemAssemblyPhaseTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileItemAssemblyPhaseTest.java index 3be332bf8..62d4ac568 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileItemAssemblyPhaseTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileItemAssemblyPhaseTest.java @@ -19,8 +19,8 @@ * under the License. */ -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -44,7 +44,10 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class FileItemAssemblyPhaseTest { @Rule diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileSetAssemblyPhaseTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileSetAssemblyPhaseTest.java index eb43fb5e2..05f53e54d 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileSetAssemblyPhaseTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/FileSetAssemblyPhaseTest.java @@ -19,46 +19,52 @@ * under the License. */ +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.apache.maven.model.Model; -import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask; -import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.plugins.assembly.model.FileSet; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.logging.Logger; -import org.easymock.EasyMock; -import org.easymock.classextension.EasyMockSupport; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - +@RunWith( MockitoJUnitRunner.class ) public class FileSetAssemblyPhaseTest { - final EasyMockSupport mm = new EasyMockSupport(); + private FileSetAssemblyPhase phase; + + @Before + public void setUp() + { + Logger logger = mock( Logger.class ); + when( logger.isDebugEnabled() ).thenReturn( true ); + + this.phase = new FileSetAssemblyPhase(); + phase.enableLogging( logger ); + } @Test public void testShouldNotFailWhenNoFileSetsSpecified() throws Exception { final Assembly assembly = new Assembly(); - assembly.setId( "test" ); - final MockAndControlForLogger macLogger = new MockAndControlForLogger(); - final MockAndControlForAddFileSetsTask macTask = new MockAndControlForAddFileSetsTask( mm ); - - mm.replayAll(); - - createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource ); - - mm.verifyAll(); + this.phase.execute( assembly, null, null ); } - @Test public void testShouldAddOneFileSet() - throws ArchiveCreationException, AssemblyFormattingException + @Test + public void testShouldAddOneFileSet() + throws Exception { final Assembly assembly = new Assembly(); @@ -73,64 +79,33 @@ public void testShouldNotFailWhenNoFileSetsSpecified() assembly.addFileSet( fs ); - final MockAndControlForLogger macLogger = new MockAndControlForLogger(); - final MockAndControlForAddFileSetsTask macTask = new MockAndControlForAddFileSetsTask( mm ); - - macTask.expectGetArchiveBaseDirectory(); - final MavenProject project = new MavenProject( new Model() ); - macLogger.expectError( true, true ); - final int dirMode = Integer.parseInt( "777", 8 ); final int fileMode = Integer.parseInt( "777", 8 ); final int[] modes = { -1, -1, dirMode, fileMode }; - macTask.expectAdditionOfSingleFileSet( project, "final-name", false, modes, 1, true ); - - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mm.replayAll(); - - createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource ); - - mm.verifyAll(); - } - - private FileSetAssemblyPhase createPhase( final MockAndControlForLogger macLogger ) - { - final FileSetAssemblyPhase phase = new FileSetAssemblyPhase(); - - phase.enableLogging( macLogger.logger ); - - return phase; + // the logger sends a debug message with this info inside the addFileSet(..) method.. + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( modes[0] ); + when( archiver.getOverrideFileMode() ).thenReturn( modes[1] ); + + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getProject() ).thenReturn( project ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); + + this.phase.execute( assembly, archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiveBaseDirectory(); + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } - - private final class MockAndControlForLogger - { - final Logger logger; - - MockAndControlForLogger() - { - logger = mm.createMock( Logger.class ); - } - - public void expectError( final boolean debugCheck, final boolean debugEnabled ) - { - if ( debugCheck ) - { - expect( logger.isDebugEnabled() ).andReturn( debugEnabled ).anyTimes(); - } - - logger.debug( (String) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - logger.warn( (String) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - logger.error( (String) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - } - - } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/ModuleSetAssemblyPhaseTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/ModuleSetAssemblyPhaseTest.java index 297ac95e0..b2156993e 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/ModuleSetAssemblyPhaseTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/ModuleSetAssemblyPhaseTest.java @@ -24,49 +24,72 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyListOf; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Model; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddArtifactTask; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddDependencySetsTask; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask; import org.apache.maven.plugins.assembly.artifact.DependencyResolver; import org.apache.maven.plugins.assembly.model.Assembly; +import org.apache.maven.plugins.assembly.model.DependencySet; import org.apache.maven.plugins.assembly.model.FileSet; import org.apache.maven.plugins.assembly.model.ModuleBinaries; import org.apache.maven.plugins.assembly.model.ModuleSet; import org.apache.maven.plugins.assembly.model.ModuleSources; -import org.apache.maven.plugins.assembly.utils.TypeConversionUtils; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; +import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMock; -import org.easymock.classextension.EasyMockSupport; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class ModuleSetAssemblyPhaseTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - private final Logger logger = new ConsoleLogger( Logger.LEVEL_INFO, "test" ); + + private ModuleSetAssemblyPhase phase; + + private DependencyResolver dependencyResolver; + + private ProjectBuilder projectBuilder; + + private Logger logger; + + @Before + public void setUp() + { + this.dependencyResolver = mock( DependencyResolver.class ); + + this.logger = mock( Logger.class ); + + this.phase = new ModuleSetAssemblyPhase( projectBuilder, dependencyResolver, logger ); + } @Test public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchOutputDir() @@ -74,9 +97,7 @@ public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchOutputDir() final ModuleSources sources = new ModuleSources(); sources.setOutputDirectory( "outdir" ); - final ModuleSetAssemblyPhase phase = createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); - - assertTrue( phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); + assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); } @Test @@ -85,9 +106,7 @@ public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchInclude() final ModuleSources sources = new ModuleSources(); sources.addInclude( "**/included.txt" ); - final ModuleSetAssemblyPhase phase = createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); - - assertTrue( phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); + assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); } @Test @@ -96,9 +115,7 @@ public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchExclude() final ModuleSources sources = new ModuleSources(); sources.addExclude( "**/excluded.txt" ); - final ModuleSetAssemblyPhase phase = createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); - - assertTrue( phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); + assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); } @Test @@ -107,9 +124,7 @@ public void testIsDeprecatedModuleSourcesConfigPresent_ShouldNotCatchFileMode() final ModuleSources sources = new ModuleSources(); sources.setFileMode( "777" ); - final ModuleSetAssemblyPhase phase = createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); - - assertFalse( phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); + assertFalse( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); } @Test @@ -118,25 +133,20 @@ public void testIsDeprecatedModuleSourcesConfigPresent_ShouldNotCatchDirMode() final ModuleSources sources = new ModuleSources(); sources.setDirectoryMode( "777" ); - final ModuleSetAssemblyPhase phase = createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); - - assertFalse( phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); + assertFalse( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) ); } @Test public void testCreateFileSet_ShouldUseModuleDirOnlyWhenOutDirIsNull() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final Model model = new Model(); model.setArtifactId( "artifact" ); final MavenProject project = new MavenProject( model ); - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm, project ); - - macTask.expectGetFinalName( null ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getProject() ).thenReturn( project ); final FileSet fs = new FileSet(); @@ -153,34 +163,29 @@ public void testCreateFileSet_ShouldUseModuleDirOnlyWhenOutDirIsNull() artifactProject.setArtifact( artifact ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - mm.replayAll(); - - final FileSet result = - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ).createFileSet( fs, sources, - artifactProject, - macTask.configSource ); + final FileSet result = this.phase.createFileSet( fs, sources, artifactProject, configSource ); assertEquals( "artifact/", result.getOutputDirectory() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testCreateFileSet_ShouldPrependModuleDirWhenOutDirIsProvided() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final Model model = new Model(); model.setArtifactId( "artifact" ); final MavenProject project = new MavenProject( model ); - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm, project ); - - macTask.expectGetFinalName( null ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getProject() ).thenReturn( project ); final FileSet fs = new FileSet(); fs.setOutputDirectory( "out" ); @@ -198,29 +203,23 @@ public void testCreateFileSet_ShouldPrependModuleDirWhenOutDirIsProvided() when( artifact.getArtifactId() ).thenReturn( "artifact" ); artifactProject.setArtifact( artifact ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mm.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project /* or artifactProject */ ); - final FileSet result = - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ).createFileSet( fs, sources, - artifactProject, - macTask.configSource ); + final FileSet result = this.phase.createFileSet( fs, sources, artifactProject, configSource ); assertEquals( "artifact/out/", result.getOutputDirectory() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testCreateFileSet_ShouldAddExcludesForSubModulesWhenExcludeSubModDirsIsTrue() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm, null ); - - macTask.expectGetFinalName( null ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); final FileSet fs = new FileSet(); @@ -241,18 +240,17 @@ public void testCreateFileSet_ShouldAddExcludesForSubModulesWhenExcludeSubModDir Artifact artifact = mock( Artifact.class ); project.setArtifact( artifact ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mm.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - final FileSet result = - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ).createFileSet( fs, sources, project, - macTask.configSource ); + final FileSet result = this.phase.createFileSet( fs, sources, project, configSource ); assertEquals( 1, result.getExcludes().size() ); assertEquals( "submodule/**", result.getExcludes().get( 0 ) ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); } @Test @@ -262,19 +260,15 @@ public void testExecute_ShouldSkipIfNoModuleSetsFound() final Assembly assembly = new Assembly(); assembly.setIncludeBaseDirectory( false ); - createPhase( null, null ).execute( assembly, null, null ); + this.phase.execute( assembly, null, null ); } @Test public void testExecute_ShouldAddOneModuleSetWithOneModuleInIt() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm, project ); - final MavenProject module = createProject( "group", "module", "version", project ); Artifact artifact = mock( Artifact.class ); @@ -283,63 +277,69 @@ public void testExecute_ShouldAddOneModuleSetWithOneModuleInIt() module.setArtifact( artifact ); final List projects = new ArrayList<>(); - projects.add( module ); - macTask.expectGetReactorProjects( projects ); - macTask.expectGetFinalName( "final-name" ); - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectGetMode( 0777, 0777 ); - - final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - - macTask.expectAddFile( moduleArtifactFile, "out/artifact", mode ); - - final Assembly assembly = new Assembly(); - assembly.setIncludeBaseDirectory( false ); - - final ModuleSet ms = new ModuleSet(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getReactorProjects() ).thenReturn( projects ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + when( configSource.getProject() ).thenReturn( project ); + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0777 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0777 ); final ModuleBinaries bin = new ModuleBinaries(); - bin.setOutputFileNameMapping( "artifact" ); bin.setOutputDirectory( "out" ); bin.setFileMode( "777" ); bin.setUnpack( false ); bin.setIncludeDependencies( false ); + final ModuleSet ms = new ModuleSet(); ms.setBinaries( bin ); + final Assembly assembly = new Assembly(); + assembly.setIncludeBaseDirectory( false ); assembly.addModuleSet( ms ); - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - - macTask.expectResolveDependencySets(); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mm.replayAll(); - - final ModuleSetAssemblyPhase phase = createPhase( logger, macTask.dependencyResolver, null ); - phase.execute( assembly, macTask.archiver, macTask.configSource ); - - mm.verifyAll(); + when( dependencyResolver.resolveDependencySets( eq( assembly ), + eq( ms ), + eq( configSource ), + anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap>() ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, module ); + + this.phase.execute( assembly, archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + verify( configSource, atLeastOnce() ).getReactorProjects(); + + verify( archiver ).addFile( moduleArtifactFile, "out/artifact", 511 ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver, times( 2 ) ).setFileMode( 511 ); + + verify( dependencyResolver ).resolveDependencySets( eq( assembly ), + eq( ms ), + eq( configSource ), + anyListOf( DependencySet.class ) ); } @Test public void testAddModuleBinaries_ShouldReturnImmediatelyWhenBinariesIsNull() throws Exception { - createPhase( null, null ).addModuleBinaries( null, null, null, null, null, null ); + this.phase.addModuleBinaries( null, null, null, null, null, null ); } @Test public void testAddModuleBinaries_ShouldFilterPomModule() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm ); - final ModuleBinaries binaries = new ModuleBinaries(); binaries.setUnpack( false ); @@ -354,36 +354,26 @@ public void testAddModuleBinaries_ShouldFilterPomModule() project.setArtifact( artifact ); final Set projects = singleton( project ); - - mm.replayAll(); - - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ).addModuleBinaries( null, null, binaries, - projects, - macTask.archiver, - macTask.configSource ); - - mm.verifyAll(); + + this.phase.addModuleBinaries( null, null, binaries, projects, null, null ); } @Test public void testAddModuleBinaries_ShouldAddOneModuleAttachmentArtifactAndNoDeps() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm, null ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); Artifact artifact = mock( Artifact.class ); when( artifact.getClassifier() ).thenReturn( "test" ); final File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); - macTask.expectGetFinalName( "final-name" ); - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectGetMode( 0222, 0222 ); - macTask.expectAddFile( artifactFile, "out/artifact", - TypeConversionUtils.modeToInt( "777", - new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ) ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); final ModuleBinaries binaries = new ModuleBinaries(); @@ -399,30 +389,37 @@ public void testAddModuleBinaries_ShouldAddOneModuleAttachmentArtifactAndNoDeps( final Set projects = singleton( project ); - macTask.expectResolveDependencySets(); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mm.replayAll(); - - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - - createPhase( logger, macTask.dependencyResolver, null ).addModuleBinaries( null, null, binaries, projects, - macTask.archiver, - macTask.configSource ); - - mm.verifyAll(); + when( dependencyResolver.resolveDependencySets( isNull( Assembly.class ), + isNull( ModuleSet.class ), + eq( configSource ), + anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap>() ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); + + this.phase.addModuleBinaries( null, null, binaries, projects, archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + + verify( archiver ).addFile( artifactFile, "out/artifact", 511 ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setFileMode( 511 ); + verify( archiver ).setFileMode( 146 ); + + verify( dependencyResolver ).resolveDependencySets( isNull( Assembly.class ), + isNull( ModuleSet.class ), + eq( configSource ), + anyListOf( DependencySet.class ) ); } @Test public void testAddModuleBinaries_ShouldFailWhenOneModuleDoesntHaveAttachmentWithMatchingClassifier() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm ); - Artifact artifact = mock( Artifact.class ); - when( artifact.getClassifier() ).thenReturn( "test" ); final ModuleBinaries binaries = new ModuleBinaries(); @@ -437,14 +434,10 @@ public void testAddModuleBinaries_ShouldFailWhenOneModuleDoesntHaveAttachmentWit final Set projects = singleton( project ); - mm.replayAll(); - - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - try { - createPhase( logger, null ).addModuleBinaries( null, null, binaries, projects, macTask.archiver, - macTask.configSource ); + + this.phase.addModuleBinaries( null, null, binaries, projects, null, null ); fail( "Should throw an invalid configuration exception because of module with missing attachment." ); } @@ -454,28 +447,23 @@ public void testAddModuleBinaries_ShouldFailWhenOneModuleDoesntHaveAttachmentWit + "Please exclude this module from the module-set.", e.getMessage()); // should throw this because of missing attachment. } - - mm.verifyAll(); } @Test public void testAddModuleBinaries_ShouldAddOneModuleArtifactAndNoDeps() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm ); - Artifact artifact = mock( Artifact.class ); final File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); - macTask.expectGetFinalName( "final-name" ); - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectAddFile( artifactFile, "out/artifact", - TypeConversionUtils.modeToInt( "777", - new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ) ); - macTask.expectGetMode( 0222, 0222 ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); final ModuleBinaries binaries = new ModuleBinaries(); @@ -490,18 +478,30 @@ public void testAddModuleBinaries_ShouldAddOneModuleArtifactAndNoDeps() final Set projects = singleton( project ); - macTask.expectResolveDependencySets(); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); + when( dependencyResolver.resolveDependencySets( isNull( Assembly.class ), + isNull( ModuleSet.class ), + any( AssemblerConfigurationSource.class ), + anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap>() ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - mm.replayAll(); + this.phase.addModuleBinaries( null, null, binaries, projects, archiver, configSource ); - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - - createPhase( logger, macTask.dependencyResolver, null ).addModuleBinaries( null, null, binaries, projects, - macTask.archiver, - macTask.configSource ); - - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + + verify( dependencyResolver ).resolveDependencySets( isNull( Assembly.class ), + isNull( ModuleSet.class ), + any( AssemblerConfigurationSource.class ), + anyListOf( DependencySet.class ) ); + + verify( archiver ).addFile( artifactFile, "out/artifact", 511 ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setFileMode( 511 ); + verify( archiver ).setFileMode( 146); } @Test @@ -511,8 +511,7 @@ public void testAddModuleArtifact_ShouldThrowExceptionWhenArtifactFileIsNull() Artifact artifact = mock( Artifact.class ); try { - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), - null ).addModuleArtifact( artifact, null, null, null, null ); + this.phase.addModuleArtifact( artifact, null, null, null, null ); fail( "Expected ArchiveCreationException since artifact file is null." ); } @@ -526,10 +525,6 @@ public void testAddModuleArtifact_ShouldThrowExceptionWhenArtifactFileIsNull() public void testAddModuleArtifact_ShouldAddOneArtifact() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddArtifactTask macTask = new MockAndControlForAddArtifactTask( mm ); - Artifact artifact = mock( Artifact.class ); final File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); @@ -537,167 +532,159 @@ public void testAddModuleArtifact_ShouldAddOneArtifact() final MavenProject project = createProject( "group", "artifact", "version", null ); project.setArtifact( artifact ); - macTask.expectGetFinalName( "final-name" ); - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectGetMode( 0222, 0222 ); - - macTask.expectAddFile( artifactFile, "out/artifact", - TypeConversionUtils.modeToInt( "777", - new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ) ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); final ModuleBinaries binaries = new ModuleBinaries(); binaries.setOutputDirectory( "out" ); binaries.setOutputFileNameMapping( "artifact" ); binaries.setUnpack( false ); binaries.setFileMode( "777" ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - mm.replayAll(); + this.phase.addModuleArtifact( artifact, project, archiver, configSource, binaries ); - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), - null ).addModuleArtifact( artifact, project, macTask.archiver, - macTask.configSource, binaries ); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); - mm.verifyAll(); + verify( archiver ).addFile( artifactFile, "out/artifact", 511 ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setFileMode( 511 ); + verify( archiver ).setFileMode( 146 ); } @Test public void testAddModuleSourceFileSets_ShouldReturnImmediatelyIfSourcesIsNull() throws Exception { - createPhase( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ).addModuleSourceFileSets( null, null, null, - null ); + this.phase.addModuleSourceFileSets( null, null, null, null ); } @Test public void testAddModuleSourceFileSets_ShouldAddOneSourceDirectory() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForAddFileSetsTask macTask = new MockAndControlForAddFileSetsTask( mm ); - final MavenProject project = createProject( "group", "artifact", "version", null ); - macTask.expectGetProject( project ); - + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + when( configSource.getProject() ).thenReturn( project ); project.setArtifact( mock( Artifact.class ) ); final Set projects = singleton( project ); - final ModuleSources sources = new ModuleSources(); - final FileSet fs = new FileSet(); fs.setDirectory( "/src" ); fs.setDirectoryMode( "777" ); fs.setFileMode( "777" ); + final ModuleSources sources = new ModuleSources(); sources.addFileSet( fs ); - macTask.expectGetArchiveBaseDirectory(); - - final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - final int[] modes = { -1, -1, mode, mode }; - - macTask.expectAdditionOfSingleFileSet( project, "final-name", false, modes, 1, true, false ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); + // the logger sends a debug message with this info inside the addFileSet(..) method.. + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 ); + when( archiver.getOverrideFileMode() ).thenReturn( -1 ); + + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - mm.replayAll(); + when( logger.isDebugEnabled() ).thenReturn( true ); - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); + this.phase.addModuleSourceFileSets( sources, projects, archiver, + configSource ); - createPhase( logger, null ).addModuleSourceFileSets( sources, projects, macTask.archiver, - macTask.configSource ); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiveBaseDirectory(); + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getProject(); + verify( configSource, atLeastOnce() ).getMavenSession(); - mm.verifyAll(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } @Test public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsOnlyCurrentProject() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - final List projects = Collections.singletonList( project ); - macTask.expectGetReactorProjects( projects ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getProject() ).thenReturn( project ); + when( configSource.getReactorProjects() ).thenReturn( projects ); final ModuleSet moduleSet = new ModuleSet(); moduleSet.setIncludeSubModules( true ); - mm.replayAll(); - final Set moduleProjects = - ModuleSetAssemblyPhase.getModuleProjects( moduleSet, macTask.configSource, logger ); + ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger ); assertTrue( moduleProjects.isEmpty() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getReactorProjects(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsTwoSiblingProjects() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - final MavenProject project2 = createProject( "group", "artifact2", "version", null ); final List projects = new ArrayList<>(); projects.add( project ); projects.add( project2 ); - macTask.expectGetReactorProjects( projects ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getReactorProjects() ).thenReturn( projects ); + when( configSource.getProject() ).thenReturn( project ); final ModuleSet moduleSet = new ModuleSet(); moduleSet.setIncludeSubModules( true ); - mm.replayAll(); - final Set moduleProjects = - ModuleSetAssemblyPhase.getModuleProjects( moduleSet, macTask.configSource, logger ); + ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger ); assertTrue( moduleProjects.isEmpty() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getReactorProjects(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testGetModuleProjects_ShouldReturnModuleOfCurrentProject() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - final MavenProject project2 = createProject( "group", "artifact2", "version", project ); final List projects = new ArrayList<>(); projects.add( project ); projects.add( project2 ); - macTask.expectGetReactorProjects( projects ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getReactorProjects() ).thenReturn( projects ); + when( configSource.getProject() ).thenReturn( project ); final ModuleSet moduleSet = new ModuleSet(); moduleSet.setIncludeSubModules( true ); - mm.replayAll(); - final Set moduleProjects = - ModuleSetAssemblyPhase.getModuleProjects( moduleSet, macTask.configSource, logger ); + ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger ); assertFalse( moduleProjects.isEmpty() ); @@ -705,20 +692,16 @@ public void testGetModuleProjects_ShouldReturnModuleOfCurrentProject() assertEquals( "artifact2", result.getArtifactId() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getReactorProjects(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testGetModuleProjects_ShouldReturnDescendentModulesOfCurrentProject() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - final MavenProject project2 = createProject( "group", "artifact2", "version", project ); final MavenProject project3 = createProject( "group", "artifact3", "version", project2 ); @@ -727,15 +710,15 @@ public void testGetModuleProjects_ShouldReturnDescendentModulesOfCurrentProject( projects.add( project2 ); projects.add( project3 ); - macTask.expectGetReactorProjects( projects ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getReactorProjects() ).thenReturn( projects ); + when( configSource.getProject() ).thenReturn( project ); final ModuleSet moduleSet = new ModuleSet(); moduleSet.setIncludeSubModules( true ); - mm.replayAll(); - final Set moduleProjects = - ModuleSetAssemblyPhase.getModuleProjects( moduleSet, macTask.configSource, logger ); + ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger ); assertEquals( 2, moduleProjects.size() ); @@ -745,20 +728,17 @@ public void testGetModuleProjects_ShouldReturnDescendentModulesOfCurrentProject( verifyResultIs( check, moduleProjects ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getReactorProjects(); + verify( configSource, atLeastOnce() ).getProject(); } @Test public void testGetModuleProjects_ShouldExcludeModuleAndDescendentsTransitively() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - final MavenProject project = createProject( "group", "artifact", "version", null ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mm, project ); - Artifact artifact1 = mock( Artifact.class ); project.setArtifact( artifact1 ); @@ -784,21 +764,23 @@ public void testGetModuleProjects_ShouldExcludeModuleAndDescendentsTransitively( projects.add( project2 ); projects.add( project3 ); - macTask.expectGetReactorProjects( projects ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getReactorProjects() ).thenReturn( projects ); + when( configSource.getProject() ).thenReturn( project ); final ModuleSet moduleSet = new ModuleSet(); moduleSet.setIncludeSubModules( true ); moduleSet.addExclude( "group:artifact2" ); - mm.replayAll(); - final Set moduleProjects = - ModuleSetAssemblyPhase.getModuleProjects( moduleSet, macTask.configSource, logger ); + ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger ); assertTrue( moduleProjects.isEmpty() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getReactorProjects(); + verify( configSource, atLeastOnce() ).getProject(); } private void verifyResultIs( final List check, final Set moduleProjects ) @@ -870,24 +852,4 @@ private MavenProject createProject( final String groupId, final String artifactI return project; } - - private ModuleSetAssemblyPhase createPhase( final Logger logger, - final MockAndControlForAddDependencySetsTask macTask ) - { - ProjectBuilder projectBuilder = null; - - if ( macTask != null ) - { - projectBuilder = macTask.projectBuilder; - } - - DependencyResolver dr = EasyMock.createMock( DependencyResolver.class ); - return new ModuleSetAssemblyPhase( projectBuilder, dr, logger ); - } - - private ModuleSetAssemblyPhase createPhase( final Logger logger, DependencyResolver dr, - ProjectBuilder projectBuilder1 ) - { - return new ModuleSetAssemblyPhase( projectBuilder1, dr, logger ); - } } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/RepositoryAssemblyPhaseTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/RepositoryAssemblyPhaseTest.java index 3f38c9758..74dd87fa1 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/phase/RepositoryAssemblyPhaseTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/phase/RepositoryAssemblyPhaseTest.java @@ -19,9 +19,12 @@ * under the License. */ -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.junit.Assert.fail; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; import java.io.File; @@ -30,240 +33,110 @@ import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.plugins.assembly.model.Repository; import org.apache.maven.plugins.assembly.repository.RepositoryAssembler; -import org.apache.maven.plugins.assembly.repository.RepositoryAssemblyException; import org.apache.maven.plugins.assembly.repository.RepositoryBuilderConfigSource; import org.apache.maven.plugins.assembly.repository.model.RepositoryInfo; import org.apache.maven.plugins.assembly.utils.TypeConversionUtils; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.Archiver; -import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.FileSet; -import org.codehaus.plexus.archiver.util.DefaultFileSet; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMock; -import org.easymock.classextension.EasyMockSupport; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class RepositoryAssemblyPhaseTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private RepositoryAssemblyPhase phase; + + private RepositoryAssembler repositoryAssembler; + + @Before + public void setUp() + { + this.repositoryAssembler = mock( RepositoryAssembler.class ); + this.phase = new RepositoryAssemblyPhase( repositoryAssembler ); + this.phase.enableLogging( mock( Logger.class ) ); + } + @Test public void testExecute_ShouldNotIncludeRepositoryIfNonSpecifiedInAssembly() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm ); - final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm ); - final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm ); - - final File tempRoot = temporaryFolder.getRoot(); - - macCS.expectGetTemporaryRootDirectory( tempRoot ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getTemporaryRootDirectory() ).thenReturn( temporaryFolder.getRoot() ); final Assembly assembly = new Assembly(); - assembly.setId( "test" ); - mm.replayAll(); - - createPhase( macRepo.repositoryAssembler, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ).execute( assembly, - macArchiver.archiver, - macCS - .configSource ); - - mm.verifyAll(); + this.phase.execute( assembly, null, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getTemporaryRootDirectory(); + + verifyZeroInteractions( repositoryAssembler ); } @Test public void testExecute_ShouldIncludeOneRepository() throws Exception { - final EasyMockSupport mm = new EasyMockSupport(); - - final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm ); - final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm ); - final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm ); - final File tempRoot = temporaryFolder.getRoot(); - macCS.expectGetTemporaryRootDirectory( tempRoot ); - macCS.expectGetProject( new MavenProject( new Model() ) ); - macCS.expectGetFinalName( "final-name" ); - macCS.expectInterpolators(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() ); + when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + when( configSource.getMainProjectInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() ); + when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) ); + when( configSource.getTemporaryRootDirectory() ).thenReturn( tempRoot ); final Assembly assembly = new Assembly(); - assembly.setId( "test" ); final Repository repo = new Repository(); - repo.setOutputDirectory( "out" ); repo.setDirectoryMode( "777" ); repo.setFileMode( "777" ); - - final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - - final File outDir = new File( tempRoot, "out" ); - - macArchiver.expectModeChange( -1, -1, mode, mode, true ); - macArchiver.expectAddDirectory( outDir, "out/", null, null ); - - macRepo.expectAssemble(); - assembly.addRepository( repo ); - mm.replayAll(); - - createPhase( macRepo.repositoryAssembler, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ).execute( assembly, - macArchiver.archiver, - macCS - .configSource ); - - mm.verifyAll(); - } - - private RepositoryAssemblyPhase createPhase( final RepositoryAssembler repositoryAssembler, final Logger logger ) - { - final RepositoryAssemblyPhase phase = new RepositoryAssemblyPhase( repositoryAssembler ); - phase.enableLogging( logger ); - - return phase; - } - - private final class MockAndControlForArchiver - { - final Archiver archiver; - - public MockAndControlForArchiver( final EasyMockSupport mockManager ) - { - - archiver = mockManager.createMock( Archiver.class ); - } - - public void expectAddDirectory( final File outDir, final String location, final String[] includes, - final String[] excludes ) - { - try - { - final DefaultFileSet fs = new DefaultFileSet(); - fs.setDirectory( outDir ); - fs.setPrefix( location ); - fs.setIncludes( includes ); - fs.setExcludes( excludes ); - - archiver.addFileSet( (FileSet) anyObject() ); - } - catch ( final ArchiverException e ) - { - fail( "Should never happen." ); - } - - EasyMock.expectLastCall().atLeastOnce(); - } - - void expectModeChange( final int defaultDirMode, final int defaultFileMode, final int dirMode, - final int fileMode, final boolean expectTwoSets ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( defaultDirMode ); - - expect( archiver.getOverrideFileMode() ).andReturn( defaultFileMode ); - - if ( expectTwoSets ) - { - archiver.setDirectoryMode( dirMode ); - archiver.setFileMode( fileMode ); - } - - archiver.setDirectoryMode( defaultDirMode ); - archiver.setFileMode( defaultFileMode ); - } - - // public void expectAddFile( File file, String outputLocation, int fileMode ) - // { - // try - // { - // archiver.addFile( file, outputLocation, fileMode ); - // } - // catch ( ArchiverException e ) - // { - // Assert.fail( "Should never happen." ); - // } - // } - } - - private final class MockAndControlForConfigSource - { - final AssemblerConfigurationSource configSource; - - public MockAndControlForConfigSource( final EasyMockSupport mockManager ) - { - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); - } - - public void expectGetProject( final MavenProject project ) - { - expect( configSource.getProject() ).andReturn( project ).atLeastOnce(); - } - - public void expectGetFinalName( final String finalName ) - { - expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce(); - } - - public void expectInterpolators() - { - expect( configSource.getCommandLinePropsInterpolator() ).andReturn( - FixedStringSearchInterpolator.empty() ).anyTimes(); - expect( configSource.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.empty() ).anyTimes(); - expect( configSource.getMainProjectInterpolator() ).andReturn( - FixedStringSearchInterpolator.empty() ).anyTimes(); - } - - public void expectGetTemporaryRootDirectory( final File tempRoot ) - { - expect( configSource.getTemporaryRootDirectory() ).andReturn( tempRoot ).atLeastOnce(); - } - - // - // public void expectGetBasedir( File basedir ) - // { - // configSource.getBasedir(); - // control.setReturnValue( basedir, MockControl.ONE_OR_MORE ); - // } - } - - private final class MockAndControlForRepositoryAssembler - { - final RepositoryAssembler repositoryAssembler; - - MockAndControlForRepositoryAssembler( final EasyMockSupport mockManager ) - { - repositoryAssembler = mockManager.createMock( RepositoryAssembler.class ); - } - - public void expectAssemble() - { - try - { - repositoryAssembler.buildRemoteRepository( (File) anyObject(), (RepositoryInfo) anyObject(), - (RepositoryBuilderConfigSource) anyObject() ); - EasyMock.expectLastCall().atLeastOnce(); - } - catch ( final RepositoryAssemblyException e ) - { - fail( "Should never happen" ); - } + final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - } + final int defaultDirMode = -1; + final int defaultFileMode = -1; + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( defaultDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( defaultFileMode ); + + this.phase.execute( assembly, archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getCommandLinePropsInterpolator(); + verify( configSource ).getEnvInterpolator(); + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource ).getMainProjectInterpolator(); + verify( configSource ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + verify( configSource, atLeastOnce() ).getTemporaryRootDirectory(); + + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setDirectoryMode( mode ); + verify( archiver ).setFileMode( mode ); + verify( archiver ).setDirectoryMode( defaultDirMode ); + verify( archiver ).setFileMode( defaultFileMode ); + verify( archiver ).addFileSet( any( FileSet.class ) ); + + verify( repositoryAssembler ).buildRemoteRepository( any( File.class ), any( RepositoryInfo.class ), + any( RepositoryBuilderConfigSource.class ) ); } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddArtifactTaskTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddArtifactTaskTest.java index 32d13fa65..a9b8af858 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddArtifactTaskTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddArtifactTaskTest.java @@ -19,10 +19,12 @@ * under the License. */ -import static org.easymock.EasyMock.anyObject; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -32,46 +34,56 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.model.Model; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddArtifactTask; +import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.DependencySet; import org.apache.maven.plugins.assembly.utils.TypeConversionUtils; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.ArchivedFileSet; -import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class AddArtifactTaskTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EasyMockSupport mockManager; - - private MockAndControlForAddArtifactTask mac; + private MavenProject mainProject; + + private AssemblerConfigurationSource configSource; @Before public void setUp() throws IOException { - mockManager = new EasyMockSupport(); - Model model = new Model(); model.setGroupId( "group" ); model.setArtifactId( "main" ); model.setVersion( "1000" ); - MavenProject mainProject = new MavenProject( model ); + this.mainProject = new MavenProject( model ); - mac = new MockAndControlForAddArtifactTask( mockManager, mainProject ); - mac.expectGetFinalName( "final-name" ); + this.configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + } + + @After + public void tearDown() + { + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); } @Test @@ -84,18 +96,25 @@ public void testShouldAddArchiveFileWithoutUnpacking() File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); - mac.expectGetMode( 0222, 0222 ); - mac.expectGetDestFile( new File( "junk" ) ); - mac.expectAddFile( artifactFile, outputLocation ); - mac.expectInterpolators(); - - mockManager.replayAll(); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + + when( configSource.getProject() ).thenReturn( mainProject ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); AddArtifactTask task = createTask( artifact ); - task.execute( mac.archiver, mac.configSource ); + task.execute( archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getProject(); - mockManager.verifyAll(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).addFile( artifactFile, outputLocation ); } @Test @@ -114,15 +133,16 @@ public void testShouldAddArchiveFileWithDefaultOutputLocation() File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); - mac.expectGetMode( 0222, 0222 ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); - mac.expectGetDestFile( new File( "junk" ) ); - mac.expectAddFile( artifactFile, outputDir + artifactId + "-" + version + "." + ext ); - mac.expectInterpolators(); - mockManager.replayAll(); + when( configSource.getProject() ).thenReturn( mainProject ); - AddArtifactTask task = - new AddArtifactTask( artifact, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); + + AddArtifactTask task = new AddArtifactTask( artifact, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null ); task.setOutputDirectory( outputDir ); task.setFileNameMapping( new DependencySet().getOutputFileNameMapping() ); @@ -133,9 +153,15 @@ public void testShouldAddArchiveFileWithDefaultOutputLocation() MavenProject project = new MavenProject( model ); task.setProject( project ); - task.execute( mac.archiver, mac.configSource ); + task.execute( archiver, configSource ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getProject(); + + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).addFile( artifactFile, outputDir + artifactId + "-" + version + "." + ext ); } private AddArtifactTask createTask( Artifact artifact ) @@ -151,75 +177,79 @@ private AddArtifactTask createTask( Artifact artifact ) public void testShouldAddArchiveFileWithUnpack() throws ArchiveCreationException, AssemblyFormattingException, IOException { - mac.expectModeChange( -1, -1, -1, -1, 1 ); - mac.expectInterpolators(); + final int originalDirMode = -1; + final int originalFileMode = -1; + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( originalDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( originalFileMode ); + + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); Artifact artifact = mock( Artifact.class ); when( artifact.getFile() ).thenReturn( temporaryFolder.newFile() ); - mac.expectGetDestFile( new File( "junk" ) ); - try - { - mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() ); - } - catch ( ArchiverException e ) - { - fail( "Should never happen." ); - } - - mockManager.replayAll(); - AddArtifactTask task = createTask( artifact ); - task.setUnpack( true ); - task.execute( mac.archiver, mac.configSource ); - - mockManager.verifyAll(); + task.execute( archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addArchivedFileSet( any( ArchivedFileSet.class ), isNull( Charset.class ) ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } @Test public void testShouldAddArchiveFileWithUnpackAndModes() throws ArchiveCreationException, AssemblyFormattingException, IOException { - int directoryMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - int fileMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); - - mac.expectModeChange( -1, -1, directoryMode, fileMode, 2 ); - mac.expectInterpolators(); + final int directoryMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); + final int fileMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); + final int originalDirMode = -1; + final int originalFileMode = -1; + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( originalDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( originalFileMode ); + + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); Artifact artifact = mock( Artifact.class ); when( artifact.getFile() ).thenReturn( temporaryFolder.newFile() ); - - mac.expectGetDestFile( new File( "junk" ) ); - try - { - mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() ); - } - catch ( ArchiverException e ) - { - fail( "Should never happen." ); - } - - mockManager.replayAll(); AddArtifactTask task = createTask( artifact ); - task.setUnpack( true ); - task.setDirectoryMode( directoryMode ); task.setFileMode( fileMode ); - task.execute( mac.archiver, mac.configSource ); - - mockManager.verifyAll(); + task.execute( archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addArchivedFileSet( any( ArchivedFileSet.class ), isNull( Charset.class ) ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setDirectoryMode( directoryMode ); + verify( archiver ).setFileMode( fileMode ); + verify( archiver ).setDirectoryMode( originalDirMode ); + verify( archiver ).setFileMode( originalFileMode ); } @Test public void testShouldAddArchiveFileWithUnpackIncludesAndExcludes() throws ArchiveCreationException, AssemblyFormattingException, IOException { - mac.expectModeChange( -1, -1, -1, -1, 1 ); + final int originalDirMode = -1; + final int originalFileMode = -1; + + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( originalDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( originalFileMode ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); String[] includes = { "**/*.txt" }; String[] excludes = { "**/README.txt" }; @@ -227,21 +257,19 @@ public void testShouldAddArchiveFileWithUnpackIncludesAndExcludes() Artifact artifact = mock( Artifact.class ); when( artifact.getFile() ).thenReturn( temporaryFolder.newFile() ); - mac.expectGetDestFile( new File( "junk" ) ); - mac.expectAddArchivedFileSet(); - mac.expectInterpolators(); - - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); AddArtifactTask task = createTask( artifact ); - task.setUnpack( true ); task.setIncludes( Arrays.asList( includes ) ); task.setExcludes( Arrays.asList( excludes ) ); - task.execute( mac.archiver, mac.configSource ); + task.execute( archiver, configSource ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addArchivedFileSet( any( ArchivedFileSet.class ), isNull( Charset.class ) ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTaskTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTaskTest.java index 464de0af6..654ffba71 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTaskTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTaskTest.java @@ -22,40 +22,54 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddDependencySetsTask; import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.DependencySet; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingResult; +import org.codehaus.plexus.archiver.ArchivedFileSet; +import org.codehaus.plexus.archiver.Archiver; +import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.Collections; import java.util.HashSet; +import java.util.Properties; import java.util.Set; +@RunWith( MockitoJUnitRunner.class ) public class AddDependencySetsTaskTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private final EasyMockSupport mockManager = new EasyMockSupport(); - @Test public void testAddDependencySet_ShouldInterpolateDefaultOutputFileNameMapping() throws Exception @@ -101,32 +115,54 @@ public void testAddDependencySet_ShouldInterpolateDefaultOutputFileNameMapping() depProject.setArtifact( depArtifact ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mockManager, mainProject ); - - macTask.expectBuildFromRepository( depProject ); - macTask.expectCSGetFinalName( mainAid + "-" + mainVer ); - - macTask.expectCSGetRepositories( null, null ); + ProjectBuildingResult pbr = mock( ProjectBuildingResult.class ); + when( pbr.getProject() ).thenReturn( depProject ); + + final ProjectBuilder projectBuilder = mock( ProjectBuilder.class ); + when( projectBuilder.build( any( Artifact.class ), any( ProjectBuildingRequest.class ) ) ).thenReturn( pbr ); - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectAddFile( newFile, outDir + depAid + "-" + depVer + "." + depExt, 10 ); + final MavenSession session = mock( MavenSession.class ); + when( session.getProjectBuildingRequest() ).thenReturn( mock( ProjectBuildingRequest.class ) ); + when( session.getExecutionProperties() ).thenReturn( new Properties() ); - macTask.expectGetMode( 0222, 0222 ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( mainAid + "-" + mainVer ); + when( configSource.getProject() ).thenReturn( mainProject ); + when( configSource.getMavenSession() ).thenReturn( session ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, mainProject ); final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( ds ), Collections.singleton( depArtifact ), - depProject, macTask.projectBuilder, logger ); - - task.addDependencySet( ds, macTask.archiver, macTask.configSource ); + depProject, projectBuilder, logger ); - mockManager.verifyAll(); + task.addDependencySet( ds, archiver, configSource ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).addFile( newFile, outDir + depAid + "-" + depVer + "." + depExt, 10 ); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setDirectoryMode( 10 ); + verify( archiver ).setDirectoryMode( 146 ); + verify( archiver ).setFileMode( 10 ); + verify( archiver ).setFileMode( 146 ); + + verify( session ).getProjectBuildingRequest(); + verify( session, times( 2 ) ).getExecutionProperties(); + + verify( projectBuilder ).build( any( Artifact.class ), any( ProjectBuildingRequest.class ) ); } @Test @@ -135,22 +171,15 @@ public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectHasNone() { final MavenProject project = new MavenProject( new Model() ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mockManager ); - final DependencySet ds = new DependencySet(); ds.setOutputDirectory( "/out" ); - mockManager.replayAll(); - final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); final AddDependencySetsTask task = - new AddDependencySetsTask( Collections.singletonList( ds ), null, project, macTask.projectBuilder, logger ); + new AddDependencySetsTask( Collections.singletonList( ds ), null, project, null, logger ); - task.addDependencySet( ds, null, macTask.configSource ); - - mockManager.verifyAll(); + task.addDependencySet( ds, null, null ); } // TODO: Find a better way of testing the project-stubbing behavior when a ProjectBuildingException takes place. @@ -162,9 +191,6 @@ public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectIsStubbed() final ProjectBuildingException pbe = new ProjectBuildingException( "test", "Test error.", new Throwable() ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) ); - final String aid = "test-dep"; final String version = "2.0-SNAPSHOT"; final String type = "jar"; @@ -181,35 +207,55 @@ public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectIsStubbed() final File destFile = new File( "assembly-dep-set.zip" ); - macTask.expectGetDestFile( destFile ); - macTask.expectBuildFromRepository( pbe ); - macTask.expectCSGetRepositories( null, null ); - macTask.expectCSGetFinalName( "final-name" ); - macTask.expectAddFile( file, "out/" + aid + "-" + version + "." + type ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( destFile ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); - macTask.expectGetMode( 0222, 0222 ); + final ProjectBuilder projectBuilder = mock( ProjectBuilder.class ); + when( projectBuilder.build( any(Artifact.class), any(ProjectBuildingRequest.class) ) ).thenThrow( pbe ); + + final MavenSession session = mock( MavenSession.class ); + when( session.getProjectBuildingRequest() ).thenReturn( mock( ProjectBuildingRequest.class ) ); + when( session.getExecutionProperties() ).thenReturn( new Properties() ); + + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + when( configSource.getMavenSession() ).thenReturn( session ); + when( configSource.getProject() ).thenReturn( project ); + final DependencySet ds = new DependencySet(); ds.setOutputDirectory( "/out" ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( ds ), Collections.singleton( depArtifact ), - project, macTask.projectBuilder, logger ); + project, projectBuilder, logger ); - task.addDependencySet( ds, macTask.archiver, macTask.configSource ); + task.addDependencySet( ds, archiver, configSource ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + verify( configSource, atLeastOnce() ).getProject(); + + verify( archiver ).addFile( file, "out/" + aid + "-" + version + "." + type ); + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + + verify( session ).getProjectBuildingRequest(); + verify( session, times( 2 ) ).getExecutionProperties(); + + verify( projectBuilder ).build( any(Artifact.class), any(ProjectBuildingRequest.class) ); } @Test public void testAddDependencySet_ShouldAddOneDependencyFromProjectWithoutUnpacking() - throws AssemblyFormattingException, ArchiveCreationException, IOException, - InvalidAssemblerConfigurationException + throws Exception { verifyOneDependencyAdded( "out", false ); } @@ -222,8 +268,8 @@ public void testAddDependencySet_ShouldAddOneDependencyFromProjectUnpacked() } private void verifyOneDependencyAdded( final String outputLocation, final boolean unpack ) - throws AssemblyFormattingException, ArchiveCreationException, IOException, - InvalidAssemblerConfigurationException + throws AssemblyFormattingException, ArchiverException, ArchiveCreationException, IOException, + InvalidAssemblerConfigurationException, ProjectBuildingException { final MavenProject project = new MavenProject( new Model() ); @@ -236,44 +282,77 @@ private void verifyOneDependencyAdded( final String outputLocation, final boolea ds.setDirectoryMode( Integer.toString( 10, 8 ) ); ds.setFileMode( Integer.toString( 10, 8 ) ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) ); + final MavenSession session = mock( MavenSession.class ); + when( session.getProjectBuildingRequest() ).thenReturn( mock( ProjectBuildingRequest.class ) ); + when( session.getExecutionProperties() ).thenReturn( new Properties() ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getMavenSession() ).thenReturn( session ); + when( configSource.getFinalName() ).thenReturn( "final-name" ); + Artifact artifact = mock( Artifact.class ); final File artifactFile = temporaryFolder.newFile(); when( artifact.getFile() ).thenReturn( artifactFile ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getDestFile() ).thenReturn( new File( "junk" ) ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 ); + when( archiver.getOverrideFileMode() ).thenReturn( 0222 ); + if ( unpack ) { - macTask.expectAddArchivedFileSet(); + } else { - macTask.expectAddFile( artifactFile, outputLocation + "/artifact", 10 ); + when( configSource.getProject() ).thenReturn( project ); } - macTask.expectGetDestFile( new File( "junk" ) ); - macTask.expectCSGetFinalName( "final-name" ); - macTask.expectCSGetRepositories( null, null ); final MavenProject depProject = new MavenProject( new Model() ); - macTask.expectBuildFromRepository( depProject ); - macTask.expectGetMode( 0222, 0222 ); + ProjectBuildingResult pbr = mock( ProjectBuildingResult.class ); + when( pbr.getProject() ).thenReturn( depProject ); + + final ProjectBuilder projectBuilder = mock( ProjectBuilder.class ); + when( projectBuilder.build( any( Artifact.class ), any( ProjectBuildingRequest.class ) ) ).thenReturn( pbr ); final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( ds ), Collections.singleton( artifact ), project, - macTask.projectBuilder, logger ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mockManager.replayAll(); + projectBuilder, logger ); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); - task.addDependencySet( ds, macTask.archiver, macTask.configSource ); + task.addDependencySet( ds, archiver, configSource ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + + verify( archiver, atLeastOnce() ).getDestFile(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setFileMode( 10 ); + verify( archiver ).setFileMode( 146 ); + verify( archiver ).setDirectoryMode( 10 ); + verify( archiver ).setDirectoryMode( 146 ); + + verify( session ).getProjectBuildingRequest(); + verify( session, atLeastOnce() ).getExecutionProperties(); + + verify( projectBuilder ).build( any( Artifact.class ), any( ProjectBuildingRequest.class ) ); + + if ( unpack ) + { + verify( archiver ).addArchivedFileSet( any( ArchivedFileSet.class ), isNull( Charset.class ) ); + } + else + { + verify( archiver ).addFile( artifactFile, outputLocation + "/artifact", 10 ); + verify( configSource, atLeastOnce() ).getProject(); + } } @Test @@ -282,9 +361,6 @@ public void testGetDependencyArtifacts_ShouldGetOneDependencyArtifact() { final MavenProject project = new MavenProject( new Model() ); - final MockAndControlForAddDependencySetsTask macTask = - new MockAndControlForAddDependencySetsTask( mockManager ); - Artifact artifact = mock( Artifact.class ); project.setArtifacts( Collections.singleton( artifact ) ); @@ -292,20 +368,16 @@ public void testGetDependencyArtifacts_ShouldGetOneDependencyArtifact() final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - mockManager.replayAll(); - final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( dependencySet ), Collections.singleton( artifact ), project, - macTask.projectBuilder, logger ); + null, logger ); final Set result = task.resolveDependencyArtifacts( dependencySet ); assertNotNull( result ); assertEquals( 1, result.size() ); assertSame( artifact, result.iterator().next() ); - - mockManager.verifyAll(); } @Test @@ -336,8 +408,6 @@ public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclu final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - mockManager.replayAll(); - final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger ); @@ -346,8 +416,6 @@ public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclu assertNotNull( result ); assertEquals( 1, result.size() ); assertSame( am1, result.iterator().next() ); - - mockManager.verifyAll(); } @Test @@ -378,8 +446,6 @@ public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIn final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); - mockManager.replayAll(); - final AddDependencySetsTask task = new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger ); @@ -388,8 +454,6 @@ public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIn assertNotNull( result ); assertEquals( 1, result.size() ); assertSame( am1, result.iterator().next() ); - - mockManager.verifyAll(); } } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTaskTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTaskTest.java index 2a534e6fe..11fcbd1aa 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTaskTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTaskTest.java @@ -1,5 +1,6 @@ package org.apache.maven.plugins.assembly.archive.task; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -19,54 +20,54 @@ * under the License. */ +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.util.Collections; + import org.codehaus.plexus.archiver.Archiver; -import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.FileSet; -import org.easymock.classextension.EasyMockSupport; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -import java.io.File; -import java.util.Collections; - -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.junit.Assert.fail; - +@RunWith( MockitoJUnitRunner.class ) public class AddDirectoryTaskTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EasyMockSupport mockManager; - private Archiver archiver; @Before public void setUp() { - mockManager = new EasyMockSupport(); - - archiver = mockManager.createMock( Archiver.class ); + this.archiver = mock( Archiver.class ); } @Test public void testAddDirectory_ShouldNotAddDirectoryIfNonExistent() throws Exception { - File dir = new File( temporaryFolder.getRoot(), "non-existent." + System.currentTimeMillis() ); - - configureModeExpectations( -1, -1, -1, -1, false ); + final int defaultDirMode = -1; + final int defaultFileMode = -1; - mockManager.replayAll(); + when( archiver.getOverrideDirectoryMode() ).thenReturn( defaultDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( defaultFileMode ); - AddDirectoryTask task = new AddDirectoryTask( dir ); + AddDirectoryTask task = new AddDirectoryTask( new File( temporaryFolder.getRoot(), "non-existent" ) ); task.execute( archiver ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } @Test @@ -74,121 +75,70 @@ public void testAddDirectory_ShouldNotAddDirectoryIfNonExistent() public void testAddDirectory_ShouldAddDirectory() throws Exception { - File dir = temporaryFolder.getRoot(); - - try - { - archiver.addFileSet( (FileSet) anyObject() ); - } - catch ( ArchiverException e ) - { - fail( "Should never happen." ); - } - - configureModeExpectations( -1, -1, -1, -1, false ); - - mockManager.replayAll(); + final int defaultDirMode = -1; + final int defaultFileMode = -1; - AddDirectoryTask task = new AddDirectoryTask( dir ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( defaultDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( defaultFileMode ); + AddDirectoryTask task = new AddDirectoryTask( temporaryFolder.getRoot() ); task.setOutputDirectory( "dir" ); task.execute( archiver ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addFileSet( any( FileSet.class ) ); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } @Test public void testAddDirectory_ShouldAddDirectoryWithDirMode() throws Exception { - File dir = temporaryFolder.getRoot(); - - try - { - archiver.addFileSet( (FileSet) anyObject() ); - } - catch ( ArchiverException e ) - { - fail( "Should never happen." ); - } - - int dirMode = Integer.parseInt( "777", 8 ); - int fileMode = Integer.parseInt( "777", 8 ); - - configureModeExpectations( -1, -1, dirMode, fileMode, true ); - - mockManager.replayAll(); - - AddDirectoryTask task = new AddDirectoryTask( dir ); - + final int dirMode = Integer.parseInt( "777", 8 ); + final int fileMode = Integer.parseInt( "777", 8 ); + final int defaultDirMode = -1; + final int defaultFileMode = -1; + + when( archiver.getOverrideDirectoryMode() ).thenReturn( defaultDirMode ); + when( archiver.getOverrideFileMode() ).thenReturn( defaultFileMode ); + + AddDirectoryTask task = new AddDirectoryTask( temporaryFolder.getRoot() ); task.setDirectoryMode( dirMode ); task.setFileMode( fileMode ); task.setOutputDirectory( "dir" ); task.execute( archiver ); - - mockManager.verifyAll(); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addFileSet( any( FileSet.class ) ); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); + verify( archiver ).setDirectoryMode( dirMode ); + verify( archiver ).setFileMode( fileMode ); + verify( archiver ).setDirectoryMode( defaultDirMode ); + verify( archiver ).setFileMode( defaultFileMode ); } @Test public void testAddDirectory_ShouldAddDirectoryWithIncludesAndExcludes() throws Exception { - File dir = temporaryFolder.getRoot(); - - try - { - archiver.addFileSet( (FileSet) anyObject() ); - } - catch ( ArchiverException e ) - { - fail( "Should never happen." ); - } - - configureModeExpectations( -1, -1, -1, -1, false ); - - mockManager.replayAll(); - - AddDirectoryTask task = new AddDirectoryTask( dir ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 ); + when( archiver.getOverrideFileMode() ).thenReturn( -1 ); + AddDirectoryTask task = new AddDirectoryTask( temporaryFolder.getRoot() ); task.setIncludes( Collections.singletonList( "**/*.txt" ) ); task.setExcludes( Collections.singletonList( "**/README.txt" ) ); task.setOutputDirectory( "dir" ); task.execute( archiver ); - mockManager.verifyAll(); - } - - private void configureModeExpectations( int defaultDirMode, int defaultFileMode, int dirMode, int fileMode, - boolean expectTwoSets ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( defaultDirMode ); - expect( archiver.getOverrideFileMode() ).andReturn( defaultFileMode ); - - if ( expectTwoSets ) - { - if ( dirMode > -1 ) - { - archiver.setDirectoryMode( dirMode ); - } - - if ( fileMode > -1 ) - { - archiver.setFileMode( fileMode ); - } - } - - if ( dirMode > -1 ) - { - archiver.setDirectoryMode( defaultDirMode ); - } - - if ( fileMode > -1 ) - { - archiver.setFileMode( defaultFileMode ); - } + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( archiver ).addFileSet( any( FileSet.class ) ); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTaskTest.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTaskTest.java index 98d861fe5..b6a4dc071 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTaskTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTaskTest.java @@ -19,44 +19,39 @@ * under the License. */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.util.ArrayList; + import org.apache.maven.model.Model; +import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.archive.ArchiveCreationException; import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; -import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask; import org.apache.maven.plugins.assembly.model.FileSet; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -import java.io.File; -import java.util.ArrayList; - -import static org.easymock.EasyMock.expect; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - +@RunWith( MockitoJUnitRunner.class ) public class AddFileSetsTaskTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EasyMockSupport mockManager; - - private MockAndControlForAddFileSetsTask macTask; - - @Before - public void setUp() - { - mockManager = new EasyMockSupport(); - - macTask = new MockAndControlForAddFileSetsTask( mockManager ); - } - @Test public void testGetFileSetDirectory_ShouldReturnAbsoluteSourceDir() throws Exception @@ -127,36 +122,38 @@ public void testGetFileSetDirectory_ShouldReturnDirFromArchiveBasedirAndSourceDi public void testAddFileSet_ShouldAddDirectory() throws Exception { + File basedir = temporaryFolder.getRoot(); + final FileSet fs = new FileSet(); - - final String dirname = "dir"; - - fs.setDirectory( dirname ); + fs.setDirectory( temporaryFolder.newFolder( "dir" ).getName() ); fs.setOutputDirectory( "dir2" ); - // ensure this exists, so the directory addition will proceed. - final File srcDir = new File( macTask.archiveBaseDir, dirname ); - srcDir.mkdirs(); - - final int[] modes = { -1, -1, -1, -1 }; - - macTask.expectAdditionOfSingleFileSet( null, null, true, modes, 1, true, false ); - -// macTask.expectGetProject( null ); + // the logger sends a debug message with this info inside the addFileSet(..) method.. + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 ); + when( archiver.getOverrideFileMode() ).thenReturn( -1 ); + + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); final MavenProject project = new MavenProject( new Model() ); + project.setFile( new File( basedir, "pom.xml" ) ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); final AddFileSetsTask task = new AddFileSetsTask( new ArrayList() ); task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); task.setProject( project ); - task.addFileSet( fs, macTask.archiver, macTask.configSource, macTask.archiveBaseDir ); + task.addFileSet( fs, archiver, configSource, null ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + + verify( archiver, times( 2 ) ).getOverrideDirectoryMode(); + verify( archiver, times( 2 ) ).getOverrideFileMode(); + verify( archiver, atLeastOnce() ) .addFileSet( any( org.codehaus.plexus.archiver.FileSet.class ) ); } @Test @@ -164,9 +161,7 @@ public void testAddFileSet_ShouldAddDirectoryUsingSourceDirNameForDestDir() throws Exception { final FileSet fs = new FileSet(); - final String dirname = "dir"; - fs.setDirectory( dirname ); final File archiveBaseDir = temporaryFolder.newFolder(); @@ -175,25 +170,29 @@ public void testAddFileSet_ShouldAddDirectoryUsingSourceDirNameForDestDir() final File srcDir = new File( archiveBaseDir, dirname ); srcDir.mkdirs(); - final int[] modes = { -1, -1, -1, -1 }; - - macTask.expectAdditionOfSingleFileSet( null, null, true, modes, 1, true, false ); - - //macTask.expectGetProject( null ); + // the logger sends a debug message with this info inside the addFileSet(..) method.. + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 ); + when( archiver.getOverrideFileMode() ).thenReturn( -1 ); + + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); final MavenProject project = new MavenProject( new Model() ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); final AddFileSetsTask task = new AddFileSetsTask( new ArrayList() ); - task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); task.setProject( project ); - task.addFileSet( fs, macTask.archiver, macTask.configSource, archiveBaseDir ); + task.addFileSet( fs, archiver, configSource, archiveBaseDir ); - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); + + verify( archiver, times( 2 ) ).getOverrideDirectoryMode(); + verify( archiver, times( 2 ) ).getOverrideFileMode(); + verify( archiver ).addFileSet( any( org.codehaus.plexus.archiver.FileSet.class ) ); } @Test @@ -202,48 +201,47 @@ public void testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent() { final FileSet fs = new FileSet(); - final String dirname = "dir"; - - fs.setDirectory( dirname ); - + fs.setDirectory( "dir" ); final File archiveBaseDir = temporaryFolder.newFolder(); - macTask.expectGetFinalName( "finalName" ); - - //macTask.expectGetProject( null ); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getFinalName() ).thenReturn( "finalName" ); - expect( macTask.archiver.getOverrideDirectoryMode() ).andReturn( -1 ); - expect( macTask.archiver.getOverrideFileMode() ).andReturn( -1 ); + final Archiver archiver = mock( Archiver.class ); + when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 ); + when( archiver.getOverrideFileMode() ).thenReturn( -1 ); final MavenProject project = new MavenProject( new Model() ); - DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource ); - - mockManager.replayAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource, project ); final AddFileSetsTask task = new AddFileSetsTask( new ArrayList() ); task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); task.setProject( project ); - task.addFileSet( fs, macTask.archiver, macTask.configSource, archiveBaseDir ); + task.addFileSet( fs, archiver, configSource, archiveBaseDir ); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource, atLeastOnce() ).getFinalName(); + verify( configSource, atLeastOnce() ).getMavenSession(); - mockManager.verifyAll(); + verify( archiver ).getOverrideDirectoryMode(); + verify( archiver ).getOverrideFileMode(); } @Test public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNonExistent() throws Exception { - macTask.archiveBaseDir = new File( temporaryFolder.getRoot(), "archive"); - macTask.expectGetArchiveBaseDirectory(); - - mockManager.replayAll(); + File archiveBaseDir = new File( temporaryFolder.getRoot(), "archive"); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getArchiveBaseDirectory() ).thenReturn( archiveBaseDir ); final AddFileSetsTask task = new AddFileSetsTask( new ArrayList() ); try { - task.execute( macTask.archiver, macTask.configSource ); + task.execute( null, configSource ); fail( "Should throw exception due to non-existent archiveBasedir location that was provided." ); } @@ -252,23 +250,23 @@ public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNonExisten // should do this, because it cannot use the provide archiveBasedir. } - mockManager.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( configSource ).getArchiveBaseDirectory(); } @Test public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNotADirectory() throws Exception { - macTask.archiveBaseDir = temporaryFolder.newFile(); - macTask.expectGetArchiveBaseDirectory(); - - mockManager.replayAll(); + File archiveBaseDir = temporaryFolder.newFile(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.getArchiveBaseDirectory() ).thenReturn( archiveBaseDir ); final AddFileSetsTask task = new AddFileSetsTask( new ArrayList() ); try { - task.execute( macTask.archiver, macTask.configSource ); + task.execute( null, configSource ); fail( "Should throw exception due to non-directory archiveBasedir location that was provided." ); } @@ -276,8 +274,8 @@ public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNotADirect { // should do this, because it cannot use the provide archiveBasedir. } - - mockManager.verifyAll(); + + verify( configSource ).getArchiveBaseDirectory(); } } diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddArtifactTask.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddArtifactTask.java deleted file mode 100644 index d5c8f5121..000000000 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddArtifactTask.java +++ /dev/null @@ -1,199 +0,0 @@ -package org.apache.maven.plugins.assembly.archive.task.testutils; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import junit.framework.Assert; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; -import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest; -import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException; -import org.apache.maven.plugins.assembly.artifact.DependencyResolver; -import org.apache.maven.plugins.assembly.model.Assembly; -import org.apache.maven.plugins.assembly.model.DependencySet; -import org.apache.maven.plugins.assembly.model.ModuleSet; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.archiver.ArchivedFileSet; -import org.codehaus.plexus.archiver.Archiver; -import org.codehaus.plexus.archiver.ArchiverException; -import org.easymock.EasyMock; -import org.easymock.classextension.EasyMockSupport; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Set; - -import static org.easymock.EasyMock.anyInt; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - -public class MockAndControlForAddArtifactTask -{ - - public final Archiver archiver; - - public final AssemblerConfigurationSource configSource; - - public final DependencyResolver dependencyResolver; - - private MavenProject project = null; - - public MockAndControlForAddArtifactTask( final EasyMockSupport mockManager ) - { - this( mockManager, null ); - } - - public MockAndControlForAddArtifactTask( final EasyMockSupport mockManager, final MavenProject project ) - { - this.project = project; - - archiver = mockManager.createMock( Archiver.class ); - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - dependencyResolver = mockManager.createMock( DependencyResolver.class ); - - enableDefaultExpectations(); - } - - private void enableDefaultExpectations() - { - expect( configSource.getProject() ).andReturn( project ).anyTimes(); - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); - } - - public void expectGetFinalName( final String finalName ) - { - expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce(); - } - - public void expectGetDestFile( final File destFile ) - { - expect( archiver.getDestFile() ).andReturn( destFile ).atLeastOnce(); - } - - public void expectGetMode( final int originalDirMode, final int originalFileMode ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( originalDirMode ); - expect( archiver.getOverrideFileMode() ).andReturn( originalFileMode ); - archiver.setFileMode( anyInt() ); - EasyMock.expectLastCall().anyTimes(); - } - - - public void expectAddArchivedFileSet() - { - try - { - archiver.addArchivedFileSet( (File) anyObject(), (String) anyObject(), (String[]) anyObject(), - (String[]) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectModeChange( final int originalDirMode, final int originalFileMode, final int dirMode, - final int fileMode, final int numberOfChanges ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( originalDirMode ); - expect( archiver.getOverrideFileMode() ).andReturn( originalFileMode ); - - // one of the changes will occur below, when we restore the original mode. - if ( numberOfChanges > 1 ) - { - for ( int i = 1; i < numberOfChanges; i++ ) - { - if ( dirMode > -1 ) - { - archiver.setDirectoryMode( dirMode ); - } - - if ( fileMode > -1 ) - { - archiver.setFileMode( fileMode ); - } - } - } - - if ( dirMode > -1 ) - { - archiver.setDirectoryMode( originalDirMode ); - } - - if ( fileMode > -1 ) - { - archiver.setFileMode( originalFileMode ); - } - } - - public void expectAddFile( final File file, final String outputLocation, final int fileMode ) - { - try - { - archiver.addFile( (File) anyObject(), (String) anyObject(), anyInt() ); - EasyMock.expectLastCall().anyTimes(); - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - - public void expectAddFile( final File file, final String outputLocation ) - { - try - { - archiver.addFile( file, outputLocation ); - EasyMock.expectLastCall().atLeastOnce(); - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectGetReactorProjects( final List projects ) - { - expect( configSource.getReactorProjects() ).andReturn( projects ).atLeastOnce(); - } - - - public void expectResolveDependencySets() - throws DependencyResolutionException - { - expect( dependencyResolver.resolveDependencySets( (Assembly) anyObject(), (ModuleSet) anyObject(), - (AssemblerConfigurationSource) anyObject(), - (List) anyObject() ) ).andReturn( - new LinkedHashMap>() ).anyTimes(); - - } - - public void expectInterpolators() - { - DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - - } -} diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddDependencySetsTask.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddDependencySetsTask.java deleted file mode 100644 index 4989f0b41..000000000 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddDependencySetsTask.java +++ /dev/null @@ -1,292 +0,0 @@ -package org.apache.maven.plugins.assembly.archive.task.testutils; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import junit.framework.Assert; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.building.ModelProblem; -import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; -import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException; -import org.apache.maven.plugins.assembly.artifact.DependencyResolver; -import org.apache.maven.plugins.assembly.model.Assembly; -import org.apache.maven.plugins.assembly.model.DependencySet; -import org.apache.maven.project.DependencyResolutionResult; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.project.ProjectBuildingResult; -import org.codehaus.plexus.archiver.ArchivedFileSet; -import org.codehaus.plexus.archiver.Archiver; -import org.codehaus.plexus.archiver.ArchiverException; -import org.codehaus.plexus.archiver.manager.ArchiverManager; -import org.easymock.EasyMock; -import org.easymock.classextension.EasyMockSupport; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -import static org.easymock.EasyMock.anyInt; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - -public class MockAndControlForAddDependencySetsTask -{ - - public final Archiver archiver; - - public final AssemblerConfigurationSource configSource; - - public final ProjectBuilder projectBuilder; - - public final ArchiverManager archiverManager; - - public final DependencyResolver dependencyResolver; - - private final MavenProject project; - - private final MavenSession session; - - private final ProjectBuildingRequest projectBuildingRequest; - - public MockAndControlForAddDependencySetsTask( final EasyMockSupport mockManager ) - { - this( mockManager, null ); - } - - public MockAndControlForAddDependencySetsTask( final EasyMockSupport mockManager, final MavenProject project ) - { - this.project = project; - - this.session = mockManager.createMock( MavenSession.class ); - - this.projectBuildingRequest = mockManager.createMock( ProjectBuildingRequest.class ); - - archiver = mockManager.createMock( Archiver.class ); - - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - - projectBuilder = mockManager.createMock( ProjectBuilder.class ); - - archiverManager = mockManager.createMock( ArchiverManager.class ); - - dependencyResolver = mockManager.createMock( DependencyResolver.class ); - - enableDefaultExpectations(); - } - - private void enableDefaultExpectations() - { - expect( configSource.getProject() ).andReturn( project ).anyTimes(); - expect( session.getProjectBuildingRequest() ).andReturn( projectBuildingRequest ).anyTimes(); - expect( session.getSystemProperties() ).andReturn( new Properties() ).anyTimes(); - expect( session.getUserProperties() ).andReturn( new Properties() ).anyTimes(); - expect( session.getExecutionProperties() ).andReturn( new Properties() ).anyTimes(); - - expectGetSession( session ); - } - - public void expectAddArchivedFileSet() - { - try - { - archiver.addArchivedFileSet( (File) anyObject(), (String) anyObject(), (String[]) anyObject(), - (String[]) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() ); - EasyMock.expectLastCall().anyTimes(); - - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectModeChange( final int originalDirMode, final int originalFileMode, final int dirMode, - final int fileMode, final int numberOfChanges ) - { - expectGetMode( originalDirMode, originalFileMode ); - // one of the changes will occur below, when we restore the original mode. - if ( numberOfChanges > 1 ) - { - for ( int i = 1; i < numberOfChanges; i++ ) - { - archiver.setDirectoryMode( dirMode ); - archiver.setFileMode( fileMode ); - } - } - - archiver.setDirectoryMode( originalDirMode ); - archiver.setFileMode( originalFileMode ); - } - - public void expectGetMode( final int originalDirMode, final int originalFileMode ) - { - archiver.setFileMode( anyInt() ); - EasyMock.expectLastCall().anyTimes(); - expect( archiver.getOverrideDirectoryMode() ).andReturn( originalDirMode ); - expect( archiver.getOverrideFileMode() ).andReturn( originalFileMode ); - archiver.setDirectoryMode( anyInt() ); - EasyMock.expectLastCall().anyTimes(); - - } - - public void expectAddFile( final File file, final String outputLocation ) - { - try - { - archiver.addFile( file, outputLocation ); - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectAddFile( final File file, final String outputLocation, final int fileMode ) - { - try - { - archiver.addFile( file, outputLocation, fileMode ); - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectAddAnyFile() - { - try - { - archiver.addFile( (File) anyObject(), (String) anyObject(), anyInt() ); - } - catch ( final ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - public void expectGetReactorProjects( final List projects ) - { - expect( configSource.getReactorProjects() ).andReturn( projects ).anyTimes(); - } - - public void expectCSGetFinalName( final String finalName ) - { - expect( configSource.getFinalName() ).andReturn( finalName ).anyTimes(); - } - - public void expectGetDestFile( final File destFile ) - { - expect( archiver.getDestFile() ).andReturn( destFile ).anyTimes(); - } - - public void expectCSGetRepositories( final ArtifactRepository localRepo, - final List remoteRepos ) - { - expect( configSource.getLocalRepository() ).andReturn( localRepo ).anyTimes(); - expect( configSource.getRemoteRepositories() ).andReturn( remoteRepos ).anyTimes(); - } - - public void expectBuildFromRepository( final ProjectBuildingException error ) - { - try - { - expect( projectBuilder.build( (Artifact) anyObject(), (ProjectBuildingRequest) anyObject() ) ).andThrow( - error ); -// projectBuilderCtl.setThrowable( error, MockControl.ONE_OR_MORE ); - } - catch ( final ProjectBuildingException e ) - { - Assert.fail( "should never happen" ); - } - } - - public void expectBuildFromRepository( final MavenProject project ) - { - ProjectBuildingResult pbr = new ProjectBuildingResult() - { - @Override - public String getProjectId() - { - return null; - } - - @Override - public File getPomFile() - { - return null; - } - - @Override - public MavenProject getProject() - { - return project; - } - - @Override - public List getProblems() - { - return null; - } - - @Override - public DependencyResolutionResult getDependencyResolutionResult() - { - return null; - } - }; - - try - { - expect( projectBuilder.build( (Artifact) anyObject(), (ProjectBuildingRequest) anyObject() ) ).andReturn( - pbr ).anyTimes(); - } - catch ( final ProjectBuildingException e ) - { - Assert.fail( "should never happen" ); - } - } - - public void expectGetSession( final MavenSession session ) - { - expect( configSource.getMavenSession() ).andReturn( session ).anyTimes(); - } - - public void expectResolveDependencySets() - throws DependencyResolutionException - { - expect( dependencyResolver.resolveDependencySets( (Assembly) anyObject(), - (AssemblerConfigurationSource) anyObject(), - (List) anyObject() ) ).andReturn( - new LinkedHashMap>() ).anyTimes(); - - } - - -} diff --git a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddFileSetsTask.java b/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddFileSetsTask.java deleted file mode 100644 index 3d4b5735e..000000000 --- a/src/test/java/org/apache/maven/plugins/assembly/archive/task/testutils/MockAndControlForAddFileSetsTask.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.apache.maven.plugins.assembly.archive.task.testutils; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import junit.framework.Assert; -import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.archiver.Archiver; -import org.codehaus.plexus.archiver.ArchiverException; -import org.codehaus.plexus.archiver.FileSet; -import org.easymock.EasyMock; -import org.easymock.classextension.EasyMockSupport; - -import java.io.File; - -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - -public class MockAndControlForAddFileSetsTask -{ - - public final AssemblerConfigurationSource configSource; - - public final Archiver archiver; - - public File archiveBaseDir; - - public MockAndControlForAddFileSetsTask( EasyMockSupport mockManager ) - { - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - archiver = mockManager.createMock( Archiver.class ); - - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); - } - - public void expectGetArchiveBaseDirectory() - { - expect( configSource.getArchiveBaseDirectory() ).andReturn( archiveBaseDir ).anyTimes(); - } - - void expectModeChanges( int[] modes, int modeChangeCount ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( modes[0] ); - expect( archiver.getOverrideFileMode() ).andReturn( modes[1] ); - - if ( modeChangeCount > 1 ) - { - for ( int i = 1; i < modeChangeCount; i++ ) - { - if ( modes[2] > -1 ) - { - archiver.setDirectoryMode( modes[2] ); - } - - if ( modes[3] > -1 ) - { - archiver.setFileMode( modes[3] ); - } - } - } - - if ( modes[2] > -1 ) - { - archiver.setDirectoryMode( modes[0] ); - } - - if ( modes[3] > -1 ) - { - archiver.setFileMode( modes[1] ); - } - } - - public void expectAdditionOfSingleFileSet( MavenProject project, String finalName, boolean shouldAddDir, - int[] modes, int modeChangeCount, boolean isDebugEnabled ) - { - expectAdditionOfSingleFileSet( project, finalName, shouldAddDir, modes, modeChangeCount, isDebugEnabled, true ); - - } - - public void expectAdditionOfSingleFileSet( MavenProject project, String finalName, boolean shouldAddDir, - int[] modes, int modeChangeCount, boolean isDebugEnabled, - boolean isProjectUsed ) - { - // the logger sends a debug message with this info inside the addFileSet(..) method.. - if ( isDebugEnabled ) - { - expect( archiver.getOverrideDirectoryMode() ).andReturn( modes[0] ); - expect( archiver.getOverrideFileMode() ).andReturn( modes[1] ); - } - - if ( isProjectUsed ) - { - expect( configSource.getProject() ).andReturn( project ).atLeastOnce(); - } - - expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce(); - - if ( shouldAddDir ) - { - expectModeChanges( modes, modeChangeCount ); - - try - { - archiver.addFileSet( (FileSet) anyObject() ); - EasyMock.expectLastCall().atLeastOnce(); - } - catch ( ArchiverException e ) - { - Assert.fail( "Should never happen." ); - } - } - - } - - public void expectGetProject( MavenProject project ) - { - expect( configSource.getProject() ).andReturn( project ).atLeastOnce(); - } - - public void expectGetFinalName( String finalName ) - { - expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce(); - } - -} diff --git a/src/test/java/org/apache/maven/plugins/assembly/artifact/DefaultDependencyResolverTest.java b/src/test/java/org/apache/maven/plugins/assembly/artifact/DefaultDependencyResolverTest.java index 7b238c22e..66e8ecb8d 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/artifact/DefaultDependencyResolverTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/artifact/DefaultDependencyResolverTest.java @@ -19,7 +19,9 @@ * under the License. */ -import static org.easymock.EasyMock.expect; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.io.File; import java.util.Arrays; @@ -46,7 +48,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.repository.internal.MavenRepositorySystemSession; import org.codehaus.plexus.PlexusTestCase; -import org.easymock.classextension.EasyMockSupport; public class DefaultDependencyResolverTest extends PlexusTestCase @@ -124,10 +125,6 @@ public void test_getDependencySetResolutionRequirements_nonTransitive() public void test_getModuleSetResolutionRequirements_withoutBinaries() throws DependencyResolutionException { - final EasyMockSupport mm = new EasyMockSupport(); - - final AssemblerConfigurationSource cs = mm.createMock( AssemblerConfigurationSource.class ); - final File rootDir = new File( "root" ); final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir ); final MavenProject module1 = @@ -138,30 +135,18 @@ public void test_getModuleSetResolutionRequirements_withoutBinaries() project.getModel().addModule( module1.getArtifactId() ); project.getModel().addModule( module2.getArtifactId() ); - expect( cs.getReactorProjects() ).andReturn( Arrays.asList( project, module1, module2 ) ).anyTimes(); - expect( cs.getProject() ).andReturn( project ).anyTimes(); - expect( cs.getMavenSession() ).andReturn( newMavenSession( project ) ).anyTimes(); - final ResolutionManagementInfo info = new ResolutionManagementInfo(); final ModuleSet ms = new ModuleSet(); ms.setBinaries( null ); - mm.replayAll(); - - resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, cs ); + resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, null ); assertTrue( info.getArtifacts().isEmpty() ); - - mm.verifyAll(); } public void test_getModuleSetResolutionRequirements_includeDeps() throws DependencyResolutionException { - final EasyMockSupport mm = new EasyMockSupport(); - - final AssemblerConfigurationSource cs = mm.createMock( AssemblerConfigurationSource.class ); - final File rootDir = new File( "root" ); final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir ); final MavenProject module1 = @@ -177,9 +162,9 @@ public void test_getModuleSetResolutionRequirements_includeDeps() project.getModel().addModule( module1.getArtifactId() ); project.getModel().addModule( module2.getArtifactId() ); - expect( cs.getReactorProjects() ).andReturn( Arrays.asList( project, module1, module2 ) ).anyTimes(); - expect( cs.getProject() ).andReturn( project ).anyTimes(); - expect( cs.getMavenSession() ).andReturn( newMavenSession( project ) ).anyTimes(); + final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getReactorProjects() ).thenReturn( Arrays.asList( project, module1, module2 ) ); + when( cs.getProject() ).thenReturn( project ); final ResolutionManagementInfo info = new ResolutionManagementInfo(); @@ -189,12 +174,12 @@ public void test_getModuleSetResolutionRequirements_includeDeps() ms.setBinaries( mb ); ms.addInclude( "*:module-1" ); - mm.replayAll(); - resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, cs ); assertEquals( module1Artifacts, info.getArtifacts() ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( cs ).getReactorProjects(); + verify( cs ).getProject(); } private MavenProject createMavenProject( final String groupId, final String artifactId, final String version, diff --git a/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyExpressionEvaluatorTest.java b/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyExpressionEvaluatorTest.java index 075083dcc..b00e4012e 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyExpressionEvaluatorTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyExpressionEvaluatorTest.java @@ -19,9 +19,13 @@ * under the License. */ -import junit.framework.TestCase; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.execution.MavenSession; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; + +import java.util.Properties; + import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; @@ -30,19 +34,16 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource; -import org.easymock.classextension.EasyMockSupport; -import org.easymock.classextension.IMocksControl; - -import java.util.Properties; - -import static org.easymock.EasyMock.expect; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class AssemblyExpressionEvaluatorTest - extends TestCase { - private final PojoConfigSource configSourceStub = new PojoConfigSource(); + @Test public void testShouldResolveModelGroupId() throws ExpressionEvaluationException { @@ -65,9 +66,9 @@ private void setupInterpolation() configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() ); configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() ); configSourceStub.setEnvInterpolator( FixedStringSearchInterpolator.create() ); - } + @Test public void testShouldResolveModelPropertyBeforeModelGroupId() throws ExpressionEvaluationException { @@ -90,6 +91,7 @@ public void testShouldResolveModelPropertyBeforeModelGroupId() assertEquals( "assembly.other.id", result ); } + @Test public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId() throws ExpressionEvaluationException { @@ -104,41 +106,29 @@ public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAsse model.setProperties( props ); - final EasyMockSupport mm = new EasyMockSupport(); - - MavenSession session = mm.createControl().createMock( MavenSession.class ); - final Properties execProps = new Properties(); execProps.setProperty( "groupId", "still.another.id" ); PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource( execProps ); - expect( session.getExecutionProperties() ).andReturn( execProps ).anyTimes(); - expect( session.getUserProperties() ).andReturn( new Properties() ).anyTimes(); - - AssemblerConfigurationSource cs = mm.createControl().createMock( AssemblerConfigurationSource.class ); - expect( cs.getCommandLinePropsInterpolator() ).andReturn( - FixedStringSearchInterpolator.create( cliProps ) ).anyTimes(); - expect( cs.getRepositoryInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes(); - expect( cs.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes(); - - expect( cs.getMavenSession() ).andReturn( session ).anyTimes(); - expect( cs.getProject() ).andReturn( new MavenProject( model ) ); - - final IMocksControl lrCtl = mm.createControl(); - final ArtifactRepository lr = lrCtl.createMock( ArtifactRepository.class ); - - expect( lr.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes(); - expect( cs.getLocalRepository() ).andReturn( lr ).anyTimes(); - mm.replayAll(); + AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create( cliProps ) ); + when( cs.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( cs.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( cs.getProject() ).thenReturn( new MavenProject( model ) ); final Object result = new AssemblyExpressionEvaluator( cs ).evaluate( "assembly.${groupId}" ); assertEquals( "assembly.still.another.id", result ); - mm.verifyAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( cs ).getCommandLinePropsInterpolator(); + verify( cs ).getRepositoryInterpolator(); + verify( cs ).getEnvInterpolator(); + verify( cs ).getProject(); } + @Test public void testShouldReturnUnchangedInputForUnresolvedExpression() throws ExpressionEvaluationException { @@ -156,6 +146,7 @@ public void testShouldReturnUnchangedInputForUnresolvedExpression() assertEquals( "assembly.${unresolved}", result ); } + @Test public void testShouldInterpolateMultiDotProjectExpression() throws ExpressionEvaluationException { diff --git a/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyInterpolatorTest.java b/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyInterpolatorTest.java index 1fdbe115d..dfab534b2 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyInterpolatorTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/interpolation/AssemblyInterpolatorTest.java @@ -19,9 +19,15 @@ * under the License. */ -import junit.framework.TestCase; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.execution.MavenSession; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.StringReader; +import java.util.List; +import java.util.Properties; + import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; @@ -35,19 +41,14 @@ import org.apache.maven.project.MavenProject; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource; -import org.easymock.classextension.EasyMockSupport; - -import java.io.IOException; -import java.io.StringReader; -import java.util.List; -import java.util.Properties; - -import static org.easymock.EasyMock.expect; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class AssemblyInterpolatorTest - extends TestCase { - + @Test public void testDependencySetOutputFileNameMappingsAreNotInterpolated() throws IOException, AssemblyInterpolationException, AssemblyReadException, InvalidAssemblerConfigurationException @@ -86,6 +87,7 @@ public void testDependencySetOutputFileNameMappingsAreNotInterpolated() assertEquals( "${artifactId}.${packaging}", outputSet.getOutputFileNameMapping() ); } + @Test public void testDependencySetOutputDirectoryIsNotInterpolated() throws IOException, AssemblyInterpolationException, AssemblyReadException, InvalidAssemblerConfigurationException @@ -122,13 +124,14 @@ public void testDependencySetOutputDirectoryIsNotInterpolated() assertEquals( "${artifactId}.${packaging}", outputSet.getOutputDirectory() ); } - public Assembly roundTripInterpolation( Assembly assembly, AssemblerConfigurationSource configSource ) + private Assembly roundTripInterpolation( Assembly assembly, AssemblerConfigurationSource configSource ) throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException { final StringReader stringReader = DefaultAssemblyReaderTest.writeToStringReader( assembly ); return new DefaultAssemblyReader().readAssembly( stringReader, "testLocation", null, configSource ); } + @Test public void testShouldResolveModelGroupIdInAssemblyId() throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException, IOException @@ -153,6 +156,7 @@ public void testShouldResolveModelGroupIdInAssemblyId() assertEquals( "assembly.group.id", outputAssembly.getId() ); } + @Test public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId() throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException, IOException @@ -184,9 +188,9 @@ public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId() assertEquals( "assembly.other.id", result.getId() ); } + @Test public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId() - throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException, - IOException + throws Exception { final Model model = new Model(); model.setArtifactId( "artifact-id" ); @@ -203,42 +207,23 @@ public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAsse assembly.setId( "assembly.${groupId}" ); - final EasyMockSupport mm = new EasyMockSupport(); - - final MavenSession session = mm.createMock( MavenSession.class ); - final Properties execProps = new Properties(); execProps.setProperty( "groupId", "still.another.id" ); - expect( session.getExecutionProperties() ).andReturn( execProps ).anyTimes(); - - expect( session.getUserProperties() ).andReturn( new Properties() ).anyTimes(); - - final PojoConfigSource cs = new PojoConfigSource(); - - final ArtifactRepository lr = mm.createMock( ArtifactRepository.class ); - - cs.setLocalRepository( lr ); - cs.setMavenSession( session ); - cs.setRootInterpolator( FixedStringSearchInterpolator.create() ); - cs.setEnvironmentInterpolator( - FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( execProps ) ) ); - cs.setEnvInterpolator( FixedStringSearchInterpolator.empty() ); - - expect( lr.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes(); - - mm.replayAll(); + final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() ); + when( cs.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( execProps ) ) ); + when( cs.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() ); final MavenProject project = new MavenProject( model ); - cs.setMavenProject( project ); + when( cs.getProject() ) .thenReturn( project ); + final Assembly result = roundTripInterpolation( assembly, cs ); assertEquals( "assembly.still.another.id", result.getId() ); - - mm.verifyAll(); - mm.resetAll(); } + @Test public void testShouldNotTouchUnresolvedExpression() throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException, IOException @@ -264,6 +249,7 @@ public void testShouldNotTouchUnresolvedExpression() assertEquals( "assembly.${unresolved}", result.getId() ); } + @Test public void testShouldInterpolateMultiDotProjectExpression() throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException, IOException diff --git a/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java b/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java index f29e73d9e..4792b7aec 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java @@ -19,10 +19,11 @@ * under the License. */ -import static org.easymock.EasyMock.expect; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.File; import java.io.FileOutputStream; @@ -40,7 +41,6 @@ import java.util.List; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; @@ -61,19 +61,19 @@ import org.codehaus.plexus.interpolation.fixed.InterpolationState; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class DefaultAssemblyReaderTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EasyMockSupport mockManager; - private AssemblerConfigurationSource configSource; public static StringReader writeToStringReader( Assembly assembly ) @@ -90,18 +90,7 @@ public static StringReader writeToStringReader( Assembly assembly ) @Before public void setUp() { -// fileManager = new TestFileManager( "assembly-reader.test.", ".xml" ); - mockManager = new EasyMockSupport(); - - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - - ArtifactRepository localRepo = mockManager.createMock( ArtifactRepository.class ); - - expect( localRepo.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes(); - expect( configSource.getLocalRepository() ).andReturn( localRepo ).anyTimes(); - expect( configSource.getRemoteRepositories() ).andReturn( - Collections.emptyList() ).anyTimes(); - expect( configSource.getMavenSession() ).andReturn( null ).anyTimes(); + configSource = mock( AssemblerConfigurationSource.class ); } @Test @@ -111,12 +100,10 @@ public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent() final File siteDir = File.createTempFile( "assembly-reader.", ".test" ); siteDir.delete(); - expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes(); + when( configSource.getSiteDirectory() ).thenReturn( siteDir ); final Assembly assembly = new Assembly(); - mockManager.replayAll(); - try { new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource ); @@ -127,99 +114,18 @@ public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent() { // this should happen. } - - mockManager.verifyAll(); } - // @Test public void testReadComponent_ShouldReadComponentFromXml() - // throws IOException, AssemblyReadException - // { - // Component component = new Component(); - // - // FileSet fileSet = new FileSet(); - // fileSet.setDirectory( "/dir" ); - // - // component.addFileSet( fileSet ); - // - // StringWriter sw = new StringWriter(); - // - // ComponentXpp3Writer componentWriter = new ComponentXpp3Writer(); - // - // componentWriter.write( sw, component ); - // - // Component result = new DefaultAssemblyReader().readComponent( new StringReader( sw.toString() ) ); - // - // List fileSets = result.getFileSets(); - // - // assertNotNull( fileSets ); - // assertEquals( 1, fileSets.size() ); - // - // FileSet fs = (FileSet) fileSets.get( 0 ); - // - // assertEquals( "/dir", fs.getDirectory() ); - // } - // - // @Test public void testGetComponentFromFile_ShouldReadComponent() - // throws IOException, AssemblyReadException - // { - // Component component = new Component(); - // - // FileSet fileSet = new FileSet(); - // fileSet.setDirectory( "/dir" ); - // - // component.addFileSet( fileSet ); - // - // File componentFile = fileManager.createTempFile(); - // - // FileWriter writer = null; - // - // try - // { - // writer = new FileWriter( componentFile ); - // - // ComponentXpp3Writer componentWriter = new ComponentXpp3Writer(); - // - // componentWriter.write( writer, component ); - // } - // finally - // { - // IOUtil.close( writer ); - // } - // - // File basedir = componentFile.getParentFile(); - // String filename = componentFile.getName(); - // - // configSource.getBasedir(); - // configSourceControl.setReturnValue( basedir ); - // - // mockManager.replayAll(); - // - // Component result = new DefaultAssemblyReader().getComponentFromFile( filename, configSource ); - // - // List fileSets = result.getFileSets(); - // - // assertNotNull( fileSets ); - // assertEquals( 1, fileSets.size() ); - // - // FileSet fs = (FileSet) fileSets.get( 0 ); - // - // assertEquals( "/dir", fs.getDirectory() ); - // - // mockManager.verifyAll(); - // } - @Test public void testIncludeSiteInAssembly_ShouldAddSiteDirFileSetWhenDirExists() throws Exception { final File siteDir = temporaryFolder.getRoot(); - expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes(); + when( configSource.getSiteDirectory() ).thenReturn( siteDir ); final Assembly assembly = new Assembly(); - mockManager.replayAll(); - new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource ); final List fileSets = assembly.getFileSets(); @@ -230,8 +136,6 @@ public void testIncludeSiteInAssembly_ShouldAddSiteDirFileSetWhenDirExists() final FileSet fs = fileSets.get( 0 ); assertEquals( siteDir.getPath(), fs.getDirectory() ); - - mockManager.verifyAll(); } @Test @@ -381,53 +285,6 @@ public void testMergeComponentWithAssembly_ShouldAddOneRepositoryToExistingListO assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 ).getScope() ); } - // FIXME: Deep merging should take place... - // public void - // testMergeComponentWithAssembly_ShouldMergeOneFileSetToOneOfExistingTwo() - // { - // Assembly assembly = new Assembly(); - // - // FileSet fs = new FileSet(); - // fs.setDirectory( "/dir" ); - // fs.addInclude( "**/test.txt" ); - // - // assembly.addFileSet( fs ); - // - // fs = new FileSet(); - // fs.setDirectory( "/other-dir" ); - // assembly.addFileSet( fs ); - // - // fs = new FileSet(); - // fs.setDirectory( "/dir" ); - // fs.addInclude( "**/components.txt" ); - // - // Component component = new Component(); - // - // component.addFileSet( fs ); - // - // new DefaultAssemblyReader().mergeComponentWithAssembly( component, - // assembly ); - // - // List fileSets = assembly.getFileSets(); - // - // assertNotNull( fileSets ); - // assertEquals( 2, fileSets.size() ); - // - // FileSet rfs1 = (FileSet) fileSets.get( 0 ); - // assertEquals( "/dir", rfs1.getDirectory() ); - // - // List includes = rfs1.getIncludes(); - // - // assertNotNull( includes ); - // assertEquals( 2, includes.size() ); - // assertTrue( includes.contains( "**/test.txt" ) ); - // assertTrue( includes.contains( "**/components.txt" ) ); - // - // FileSet rfs2 = (FileSet) fileSets.get( 1 ); - // assertEquals( "/other-dir", rfs2.getDirectory() ); - // - // } - @Test public void testMergeComponentWithAssembly_ShouldAddOneContainerDescriptorHandlerToExistingListOfTwo() { @@ -492,16 +349,14 @@ public void testMergeComponentsWithMainAssembly_ShouldAddOneFileSetToAssembly() final MavenProject project = new MavenProject(); - expect( configSource.getProject() ).andReturn( project ).anyTimes(); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getProject() ).thenReturn( project ); + when( configSource.getBasedir() ).thenReturn( basedir ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); InterpolationState is = new InterpolationState(); ComponentXpp3Reader.ContentTransformer componentIp = AssemblyInterpolator.componentInterpolator( FixedStringSearchInterpolator.create(), is, new ConsoleLogger( Logger.LEVEL_DEBUG, "console" ) ); - mockManager.replayAll(); - new DefaultAssemblyReader().mergeComponentsWithMainAssembly( assembly, null, configSource, componentIp ); final List fileSets = assembly.getFileSets(); @@ -512,8 +367,6 @@ public void testMergeComponentsWithMainAssembly_ShouldAddOneFileSetToAssembly() final FileSet fs = fileSets.get( 0 ); assertEquals( "/dir", fs.getDirectory() ); - - mockManager.verifyAll(); } @Test @@ -526,8 +379,6 @@ public void testReadAssembly_ShouldReadAssemblyWithoutComponentsInterpolationOrS final Assembly result = doReadAssembly( assembly ); assertEquals( assembly.getId(), result.getId() ); - - mockManager.verifyAll(); } @Test @@ -543,11 +394,11 @@ public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyW final File siteDir = temporaryFolder.newFolder( "site" ); - expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes(); + when( configSource.getSiteDirectory() ).thenReturn( siteDir ); final File basedir = temporaryFolder.getRoot(); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); final Model model = new Model(); model.setGroupId( "group" ); @@ -556,11 +407,10 @@ public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyW final MavenProject project = new MavenProject( model ); - expect( configSource.getProject() ).andReturn( project ).anyTimes(); + when( configSource.getProject() ).thenReturn( project ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - mockManager.replayAll(); final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource ); @@ -571,8 +421,6 @@ public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyW assertEquals( 1, fileSets.size() ); assertEquals( "/site", fileSets.get( 0 ).getOutputDirectory() ); - - mockManager.verifyAll(); } @Test @@ -603,7 +451,7 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclus final StringReader sr = writeToStringReader( assembly ); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); final Model model = new Model(); model.setGroupId( "group" ); @@ -611,12 +459,10 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclus model.setVersion( "version" ); final MavenProject project = new MavenProject( model ); - expect( configSource.getProject() ).andReturn( project ).anyTimes(); + when( configSource.getProject() ).thenReturn( project ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - mockManager.replayAll(); - final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource ); assertEquals( assembly.getId(), result.getId() ); @@ -626,8 +472,6 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclus assertEquals( 1, fileSets.size() ); assertEquals( "/dir", fileSets.get( 0 ).getDirectory() ); - - mockManager.verifyAll(); } @Test @@ -658,7 +502,7 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithout final StringReader sr = writeToStringReader( assembly ); - expect( configSource.getBasedir() ).andReturn( basedir ).atLeastOnce(); + when( configSource.getBasedir() ).thenReturn( basedir ); final Model model = new Model(); model.setGroupId( "group" ); @@ -667,12 +511,10 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithout final MavenProject project = new MavenProject( model ); - expect( configSource.getProject() ).andReturn( project ).atLeastOnce(); + when( configSource.getProject() ).thenReturn( project ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - mockManager.replayAll(); - final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource ); assertEquals( assembly.getId(), result.getId() ); @@ -682,8 +524,6 @@ public void testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithout assertEquals( 1, fileSets.size() ); assertEquals( "group-dir", fileSets.get( 0 ).getDirectory() ); - - mockManager.verifyAll(); } @Test @@ -696,8 +536,6 @@ public void testReadAssembly_ShouldReadAssemblyWithInterpolationWithoutComponent final Assembly result = doReadAssembly( assembly ); assertEquals( "group-assembly", result.getId() ); - - mockManager.verifyAll(); } private Assembly doReadAssembly( Assembly assembly ) @@ -707,7 +545,7 @@ private Assembly doReadAssembly( Assembly assembly ) final File basedir = temporaryFolder.getRoot(); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); final Model model = new Model(); model.setGroupId( "group" ); @@ -716,12 +554,10 @@ private Assembly doReadAssembly( Assembly assembly ) final MavenProject project = new MavenProject( model ); - expect( configSource.getProject() ).andReturn( project ).anyTimes(); + when( configSource.getProject() ).thenReturn( project ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - mockManager.replayAll(); - return new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource ); } @@ -741,9 +577,9 @@ public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly() final File basedir = assemblyFile.getParentFile(); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); - expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes(); + when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); @@ -752,13 +588,9 @@ public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly() new AssemblyXpp3Writer().write( writer, assembly ); } - mockManager.replayAll(); - final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile( assemblyFile, configSource ); assertEquals( assembly.getId(), result.getId() ); - - mockManager.verifyAll(); } @Test @@ -767,21 +599,15 @@ public void testGetAssemblyForDescriptorReference_ShouldReadBinaryAssemblyRef() { final File basedir = temporaryFolder.getRoot(); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); - - expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); - expect( configSource.isIgnoreMissingDescriptor() ).andReturn( false ).anyTimes(); + when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) ); DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - mockManager.replayAll(); - final Assembly result = new DefaultAssemblyReader().getAssemblyForDescriptorReference( "bin", configSource ); assertEquals( "bin", result.getId() ); - - mockManager.verifyAll(); } @Test @@ -1001,26 +827,27 @@ private List performReadAssemblies( final File basedir, final String[] final boolean ignoreMissing ) throws AssemblyReadException, InvalidAssemblerConfigurationException { - expect( configSource.getDescriptorReferences() ).andReturn( descriptorRefs ); + when( configSource.getDescriptorReferences() ).thenReturn( descriptorRefs ); - expect( configSource.getDescriptors() ).andReturn( descriptors ); + when( configSource.getDescriptors() ).thenReturn( descriptors ); - expect( configSource.getDescriptorSourceDirectory() ).andReturn( descriptorDir ); + when( configSource.getDescriptorSourceDirectory() ).thenReturn( descriptorDir ); - expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes(); + when( configSource.getBasedir() ).thenReturn( basedir ); //.atLeastOnce(); - expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes(); - - expect( configSource.isIgnoreMissingDescriptor() ).andReturn( ignoreMissing ).anyTimes(); - DefaultAssemblyArchiverTest.setupInterpolators( configSource ); - - mockManager.replayAll(); - - final List assemblies = new DefaultAssemblyReader().readAssemblies( configSource ); + if ( descriptors == null && descriptorRefs == null && descriptorDir == null ) + { + when( configSource.isIgnoreMissingDescriptor() ).thenReturn( ignoreMissing ); //.atLeastOnce(); + } + + if ( !ignoreMissing ) + { + when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) ); //.atLeastOnce(); - mockManager.verifyAll(); + DefaultAssemblyArchiverTest.setupInterpolators( configSource ); + } - return assemblies; + return new DefaultAssemblyReader().readAssemblies( configSource ); } } diff --git a/src/test/java/org/apache/maven/plugins/assembly/utils/AssemblyFormatUtilsTest.java b/src/test/java/org/apache/maven/plugins/assembly/utils/AssemblyFormatUtilsTest.java index af34c81c3..40eb100c8 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/utils/AssemblyFormatUtilsTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/utils/AssemblyFormatUtilsTest.java @@ -1,8 +1,5 @@ package org.apache.maven.plugins.assembly.utils; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.handler.ArtifactHandler; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -22,6 +19,17 @@ * under the License. */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Properties; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Build; import org.apache.maven.model.Model; @@ -30,23 +38,13 @@ import org.apache.maven.plugins.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.project.MavenProject; -import org.easymock.classextension.EasyMockSupport; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -import java.util.Properties; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import static org.easymock.EasyMock.expect; - +@RunWith( MockitoJUnitRunner.class ) public class AssemblyFormatUtilsTest { - - private final EasyMockSupport mockManager = new EasyMockSupport(); - @Test public void testFixRelativePathRefs_ShouldRemoveRelativeRefToCurrentDir() throws Exception @@ -339,19 +337,15 @@ public void testEvalFileNameMapping_ShouldResolveArtifactIdAndBaseVersionInOutDi final MavenProject artifactProject = createProject( "group", "artifact", artifactVersion, null ); Artifact artifact = mock( Artifact.class ); - when( artifact.getVersion() ).thenReturn( artifactVersion ); when( artifact.getBaseVersion() ).thenReturn( artifactBaseVersion ); artifactProject.setArtifact( artifact ); final MavenSession session = mock( MavenSession.class ); - when( session.getUserProperties() ).thenReturn( new Properties() ); - - final AssemblerConfigurationSource cs = mockManager.createMock( AssemblerConfigurationSource.class ); - expect( cs.getMavenSession() ).andReturn( session ); - DefaultAssemblyArchiverTest.setupInterpolators( cs ); - mockManager.replayAll(); + final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getMavenSession() ).thenReturn( session ); + DefaultAssemblyArchiverTest.setupInterpolators( cs, mainProject ); final String result = AssemblyFormatUtils.evaluateFileNameMapping( "${artifact.artifactId}-${artifact.baseVersion}", @@ -360,11 +354,9 @@ public void testEvalFileNameMapping_ShouldResolveArtifactIdAndBaseVersionInOutDi AssemblyFormatUtils.artifactProjectInterpolator( artifactProject ) ); assertEquals( "artifact-2-SNAPSHOT", result ); - - mockManager.verifyAll(); - - // clear out for next call. - mockManager.resetAll(); + + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( cs ).getMavenSession(); } @Test @@ -683,14 +675,12 @@ private void verifyEvalFileNameMapping( final String expression, final String cl final MavenSession session = mock( MavenSession.class ); when( session.getExecutionProperties() ).thenReturn( System.getProperties() ); - when( session.getUserProperties() ).thenReturn( new Properties() ); - final AssemblerConfigurationSource cs = mockManager.createMock( AssemblerConfigurationSource.class ); - expect( cs.getMavenSession() ).andReturn( session ).anyTimes(); + final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getMavenSession() ).thenReturn( session ); + DefaultAssemblyArchiverTest.setupInterpolators( cs, mainProject ); - mockManager.replayAll(); - final String result = AssemblyFormatUtils.evaluateFileNameMapping( expression, artifactMock, mainProject, moduleArtifactMock, cs, @@ -699,10 +689,8 @@ private void verifyEvalFileNameMapping( final String expression, final String cl assertEquals( checkValue, result ); - mockManager.verifyAll(); - - // clear out for next call. - mockManager.resetAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( cs ).getMavenSession(); } private void verifyOutputDir( final String outDir, final String finalName, final String projectFinalName, @@ -786,45 +774,39 @@ private void verifyOutputDir( final String outDir, final String finalName, final final MavenSession session = mock( MavenSession.class ); when( session.getExecutionProperties() ).thenReturn( System.getProperties() ); - when( session.getUserProperties() ).thenReturn( new Properties() ); - final AssemblerConfigurationSource cs = mockManager.createMock( AssemblerConfigurationSource.class ); - expect( cs.getMavenSession() ).andReturn( session ).anyTimes(); + final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class ); + when( cs.getMavenSession() ).thenReturn( session ); + DefaultAssemblyArchiverTest.setupInterpolators( cs, mainProject ); - String result; - - mockManager.replayAll(); - result = + String result = AssemblyFormatUtils.getOutputDirectory( outDir, finalName, cs, AssemblyFormatUtils.moduleProjectInterpolator( moduleProject ), AssemblyFormatUtils.artifactProjectInterpolator( artifactProject ) ); assertEquals( checkValue, result ); - mockManager.verifyAll(); - - mockManager.resetAll(); + // result of easymock migration, should be assert of expected result instead of verifying methodcalls + verify( cs ).getMavenSession(); } private void verifyDistroName( final String assemblyId, final String finalName, final boolean appendAssemblyId, final String checkValue ) { - final MockAndControlForGetDistroName mac = new MockAndControlForGetDistroName( finalName, appendAssemblyId ); - - mockManager.replayAll(); + final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class ); + when( configSource.isAssemblyIdAppended() ).thenReturn( appendAssemblyId ); + when( configSource.getFinalName() ).thenReturn( finalName ); final Assembly assembly = new Assembly(); assembly.setId( assemblyId ); - final String result = AssemblyFormatUtils.getDistributionName( assembly, mac.configSource ); + final String result = AssemblyFormatUtils.getDistributionName( assembly, configSource ); assertEquals( checkValue, result ); - mockManager.verifyAll(); - - // clear it out for the next call. - mockManager.resetAll(); + verify( configSource, atLeast( 1 ) ).isAssemblyIdAppended(); + verify( configSource, atLeast( 1 ) ).getFinalName(); } @Test @@ -839,30 +821,4 @@ public void testLinuxRootReferencePath() assertTrue( AssemblyFormatUtils.isUnixRootReference( "/etc/home" ) ); } - private final class MockAndControlForGetDistroName - { - final AssemblerConfigurationSource configSource; - - private final boolean isAssemblyIdAppended; - - private final String finalName; - - public MockAndControlForGetDistroName( final String finalName, final boolean isAssemblyIdAppended ) - { - this.finalName = finalName; - this.isAssemblyIdAppended = isAssemblyIdAppended; - - configSource = mockManager.createMock( AssemblerConfigurationSource.class ); - - enableExpectations(); - } - - private void enableExpectations() - { - expect( configSource.isAssemblyIdAppended() ).andReturn( isAssemblyIdAppended ).atLeastOnce(); - - expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce(); - } - } - } diff --git a/src/test/java/org/apache/maven/plugins/assembly/utils/FilterUtilsTest.java b/src/test/java/org/apache/maven/plugins/assembly/utils/FilterUtilsTest.java index c1793b034..b5df696e6 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/utils/FilterUtilsTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/utils/FilterUtilsTest.java @@ -19,18 +19,11 @@ * under the License. */ -import org.hamcrest.Matchers; - -import junit.framework.TestCase; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.resolver.filter.ArtifactFilter; -import org.apache.maven.model.Model; -import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.easymock.classextension.EasyMockSupport; -import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Arrays; @@ -39,47 +32,38 @@ import java.util.List; import java.util.Set; -import static org.easymock.EasyMock.expect; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.console.ConsoleLogger; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith( MockitoJUnitRunner.class ) public class FilterUtilsTest - extends TestCase { - - private final EasyMockSupport mockManager = new EasyMockSupport(); - private Logger logger; - private static Model buildModel( final String groupId, final String artifactId ) - { - final Model model = new Model(); - model.setGroupId( groupId ); - model.setArtifactId( artifactId ); - - return model; - } - - @Override + @Before public void setUp() - { - clearAll(); - } - - private void clearAll() { logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ); } + @Test public void testFilterArtifacts_ShouldThrowExceptionUsingStrictModeWithUnmatchedInclude() { - final Artifact artifact = mockManager.createMock( Artifact.class ); - - expect( artifact.getGroupId() ).andReturn( "group" ).atLeastOnce(); - - expect( artifact.getArtifactId() ).andReturn( "artifact" ).atLeastOnce(); - - expect( artifact.getId() ).andReturn( "group:artifact:type:version" ).atLeastOnce(); - - expect( artifact.getDependencyConflictId() ).andReturn( "group:artifact:type" ).atLeastOnce(); + final Artifact artifact = mock( Artifact.class ); + when( artifact.getGroupId() ).thenReturn( "group" ); + when( artifact.getArtifactId() ).thenReturn( "artifact" ); + when( artifact.getId() ).thenReturn( "group:artifact:type:version" ); + when( artifact.getDependencyConflictId() ).thenReturn( "group:artifact:type" ); final List includes = new ArrayList<>(); @@ -90,8 +74,6 @@ public void testFilterArtifacts_ShouldThrowExceptionUsingStrictModeWithUnmatched final Set artifacts = new HashSet<>(); artifacts.add( artifact ); - mockManager.replayAll(); - try { FilterUtils.filterArtifacts( artifacts, includes, excludes, true, false, logger ); @@ -102,52 +84,53 @@ public void testFilterArtifacts_ShouldThrowExceptionUsingStrictModeWithUnmatched { // expected. } - - mockManager.verifyAll(); } + @Test public void testFilterArtifacts_ShouldNotRemoveArtifactDirectlyIncluded() - throws InvalidAssemblerConfigurationException + throws Exception { verifyArtifactInclusion( "group", "artifact", "group:artifact", null, null, null ); verifyArtifactInclusion( "group", "artifact", "group:artifact:jar", null, null, null ); } + @Test public void testFilterArtifacts_ShouldNotRemoveArtifactTransitivelyIncluded() - throws InvalidAssemblerConfigurationException + throws Exception { verifyArtifactInclusion( "group", "artifact", "group:dependentArtifact", null, Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ), null ); } + @Test public void testFilterArtifacts_ShouldRemoveArtifactTransitivelyExcluded() - throws InvalidAssemblerConfigurationException + throws Exception { verifyArtifactExclusion( "group", "artifact", null, "group:dependentArtifact", Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ), null ); } + @Test public void testFilterArtifacts_ShouldRemoveArtifactDirectlyExcluded() - throws InvalidAssemblerConfigurationException + throws Exception { verifyArtifactExclusion( "group", "artifact", null, "group:artifact", null, null ); - - clearAll(); - verifyArtifactExclusion( "group", "artifact", null, "group:artifact:jar", null, null ); } + @Test public void testFilterArtifacts_ShouldNotRemoveArtifactNotIncludedAndNotExcluded() - throws InvalidAssemblerConfigurationException + throws Exception { verifyArtifactInclusion( "group", "artifact", null, null, null, null ); verifyArtifactInclusion( "group", "artifact", null, null, null, null ); } + @Test public void testFilterArtifacts_ShouldRemoveArtifactExcludedByAdditionalFilter() - throws InvalidAssemblerConfigurationException + throws Exception { final ArtifactFilter filter = new ArtifactFilter() { @@ -162,36 +145,42 @@ public boolean include( final Artifact artifact ) verifyArtifactExclusion( "group", "artifact", "fail:fail", null, null, filter ); } + @Test public void testFilterProjects_ShouldNotRemoveProjectDirectlyIncluded() { verifyProjectInclusion( "group", "artifact", "group:artifact", null, null ); verifyProjectInclusion( "group", "artifact", "group:artifact:jar", null, null ); } + @Test public void testFilterProjects_ShouldNotRemoveProjectTransitivelyIncluded() { verifyProjectInclusion( "group", "artifact", "group:dependentArtifact", null, Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ) ); } + @Test public void testFilterProjects_ShouldRemoveProjectTransitivelyExcluded() { verifyProjectExclusion( "group", "artifact", null, "group:dependentArtifact", Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ) ); } + @Test public void testFilterProjects_ShouldRemoveProjectDirectlyExcluded() { verifyProjectExclusion( "group", "artifact", null, "group:artifact", null ); verifyProjectExclusion( "group", "artifact", null, "group:artifact:jar", null ); } + @Test public void testFilterProjects_ShouldNotRemoveProjectNotIncludedAndNotExcluded() { verifyProjectInclusion( "group", "artifact", null, null, null ); verifyProjectInclusion( "group", "artifact", null, null, null ); } + @Test public void testTransitiveScopes() { Assert.assertThat( FilterUtils.newScopeFilter( "compile" ).getIncluded(), @@ -234,9 +223,18 @@ private void verifyArtifactFiltering( final String groupId, final String artifac final boolean verifyInclusion, final ArtifactFilter additionalFilter ) throws InvalidAssemblerConfigurationException { - final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, depTrail ); + Artifact artifact = mock( Artifact.class ); - mockManager.replayAll(); + // this is always enabled, for verification purposes. + when( artifact.getDependencyConflictId() ).thenReturn( groupId + ":" + artifactId + ":jar" ); + when( artifact.getGroupId() ).thenReturn( groupId ); + when( artifact.getArtifactId() ).thenReturn( artifactId ); + when( artifact.getId() ).thenReturn( groupId + ":" + artifactId + ":version:null:jar" ); + + if ( depTrail != null ) + { + when( artifact.getDependencyTrail() ).thenReturn( depTrail ); + } List inclusions; if ( inclusionPattern != null ) @@ -258,8 +256,7 @@ private void verifyArtifactFiltering( final String groupId, final String artifac exclusions = Collections.emptyList(); } - final Set artifacts = new HashSet<>(); - artifacts.add( mac.artifact ); + final Set artifacts = new HashSet<>( Collections.singleton( artifact ) ); FilterUtils.filterArtifacts( artifacts, inclusions, exclusions, false, depTrail != null, logger, additionalFilter ); @@ -267,21 +264,16 @@ private void verifyArtifactFiltering( final String groupId, final String artifac if ( verifyInclusion ) { assertEquals( 1, artifacts.size() ); - assertEquals( mac.artifact.getDependencyConflictId(), + assertEquals( artifact.getDependencyConflictId(), artifacts.iterator().next().getDependencyConflictId() ); } else { // just make sure this trips, to meet the mock's expectations. - mac.artifact.getDependencyConflictId(); + artifact.getDependencyConflictId(); assertTrue( artifacts.isEmpty() ); } - - mockManager.verifyAll(); - - // get ready for multiple calls per test. - mockManager.resetAll(); } private void verifyProjectInclusion( final String groupId, final String artifactId, final String inclusionPattern, @@ -300,15 +292,25 @@ private void verifyProjectFiltering( final String groupId, final String artifact final String exclusionPattern, final List depTrail, final boolean verifyInclusion ) { - final ProjectWithArtifactMockControl pmac = new ProjectWithArtifactMockControl( groupId, artifactId, depTrail ); + final Artifact artifact = mock( Artifact.class ); + + // this is always enabled, for verification purposes. + when( artifact.getDependencyConflictId() ).thenReturn( groupId + ":" + artifactId + ":jar" ); + when( artifact.getGroupId() ).thenReturn( groupId ); + when( artifact.getArtifactId() ).thenReturn( artifactId ); + when( artifact.getId() ).thenReturn( groupId + ":" + artifactId + ":version:null:jar" ); - mockManager.replayAll(); + if ( depTrail != null ) + { + when( artifact.getDependencyTrail() ).thenReturn( depTrail ); + } - // make sure the mock is satisfied...you can't disable this expectation. - pmac.mac.artifact.getDependencyConflictId(); + MavenProject project = mock( MavenProject.class ); + when( project.getId() ).thenReturn( "group:artifact:jar:1.0" ); + when( project.getArtifact() ).thenReturn( artifact ); final Set projects = new HashSet<>(); - projects.add( pmac ); + projects.add( project ); List inclusions; if ( inclusionPattern != null ) @@ -334,87 +336,15 @@ private void verifyProjectFiltering( final String groupId, final String artifact Set result = FilterUtils.filterProjects( projects, inclusions, exclusions, depTrail != null, logger ); - + if ( verifyInclusion ) { assertEquals( 1, result.size() ); - assertEquals( pmac.getId(), result.iterator().next().getId() ); + assertEquals( project.getId(), result.iterator().next().getId() ); } else { assertTrue( result.isEmpty() ); } - - mockManager.verifyAll(); - - // get ready for multiple calls per test. - mockManager.resetAll(); - } - - private final class ProjectWithArtifactMockControl - extends MavenProject - { - final ArtifactMockAndControl mac; - - ProjectWithArtifactMockControl( final String groupId, final String artifactId, final List depTrail ) - { - super( buildModel( groupId, artifactId ) ); - - mac = new ArtifactMockAndControl( groupId, artifactId, depTrail ); - - setArtifact( mac.artifact ); - - setVersion( "1.0" ); - } - - } - - private final class ArtifactMockAndControl - { - final Artifact artifact; - - final String groupId; - - final String artifactId; - - final List dependencyTrail; - - ArtifactMockAndControl( final String groupId, final String artifactId, final List dependencyTrail ) - { - this.groupId = groupId; - this.artifactId = artifactId; - this.dependencyTrail = dependencyTrail; - - artifact = mockManager.createMock( Artifact.class ); - - // this is always enabled, for verification purposes. - enableGetDependencyConflictId(); - enableGetGroupIdArtifactIdAndId(); - - if ( dependencyTrail != null ) - { - enableGetDependencyTrail(); - } - } - - void enableGetDependencyTrail() - { - expect( artifact.getDependencyTrail() ).andReturn( dependencyTrail ).anyTimes(); - } - - void enableGetDependencyConflictId() - { - expect( artifact.getDependencyConflictId() ).andReturn( groupId + ":" + artifactId + ":jar" ).anyTimes(); - } - - void enableGetGroupIdArtifactIdAndId() - { - expect( artifact.getGroupId() ).andReturn( groupId ).anyTimes(); - - expect( artifact.getArtifactId() ).andReturn( artifactId ).anyTimes(); - - expect( artifact.getId() ).andReturn( groupId + ":" + artifactId + ":version:null:jar" ).anyTimes(); - } } - }