diff options
author | Martin Schreier <martin_s@apache.org> | 2022-01-04 20:46:35 +0100 |
---|---|---|
committer | Martin Schreier <martin_s@apache.org> | 2022-01-04 20:46:35 +0100 |
commit | e236e7f98d2849e96aea56cdc4e308a2911fe8f0 (patch) | |
tree | eaa583e66ee8f8ebc0d074965caf4e551d912a93 /archiva-modules/archiva-web | |
parent | 3756ba57f6ebc5e3d256c25c220ae5b83f41eb22 (diff) | |
download | archiva-e236e7f98d2849e96aea56cdc4e308a2911fe8f0.tar.gz archiva-e236e7f98d2849e96aea56cdc4e308a2911fe8f0.zip |
Unifying mocks to mockito
Diffstat (limited to 'archiva-modules/archiva-web')
10 files changed, 281 insertions, 406 deletions
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 3b5f3891d..7dd8dad50 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -359,6 +359,11 @@ <!-- TEST Scope --> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>test</scope> @@ -370,11 +375,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>jakarta.transaction</groupId> <artifactId>jakarta.transaction-api</artifactId> <scope>test</scope> diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java index 5bc1d9b28..07588dad9 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java @@ -22,8 +22,8 @@ import org.apache.archiva.common.filelock.DefaultFileLockManager; import org.apache.archiva.repository.storage.fs.FilesystemAsset; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.storage.StorageAsset; -import org.easymock.TestSubject; import org.junit.Test; +import org.mockito.InjectMocks; import java.io.IOException; import java.nio.file.Path; @@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class ArtifactBuilderTest { - @TestSubject + @InjectMocks private ArtifactBuilder builder = new ArtifactBuilder(); StorageAsset getFile(String path) throws IOException { diff --git a/archiva-modules/archiva-web/archiva-rss/pom.xml b/archiva-modules/archiva-web/archiva-rss/pom.xml index f67f8a4ee..d74c24847 100644 --- a/archiva-modules/archiva-web/archiva-rss/pom.xml +++ b/archiva-modules/archiva-web/archiva-rss/pom.xml @@ -92,11 +92,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.apache.archiva</groupId> <artifactId>archiva-test-utils</artifactId> <version>${project.version}</version> @@ -107,6 +102,11 @@ <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> diff --git a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java index 2e5913108..a92721477 100644 --- a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java +++ b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java @@ -28,23 +28,19 @@ import org.apache.archiva.metadata.repository.RepositorySession; import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.apache.archiva.rss.RssFeedGenerator; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import java.time.LocalDateTime; -import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.TimeZone; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith (ArchivaBlockJUnit4ClassRunner.class) public class NewArtifactsRssFeedProcessorTest @@ -56,10 +52,8 @@ public class NewArtifactsRssFeedProcessorTest private MetadataRepositoryMock metadataRepository; - private IMocksControl sessionFactoryControl; private RepositorySessionFactory sessionFactory; - private IMocksControl sessionControl; private RepositorySession session; @Before @@ -74,18 +68,11 @@ public class NewArtifactsRssFeedProcessorTest metadataRepository = new MetadataRepositoryMock(); - sessionFactoryControl = EasyMock.createControl(); - sessionControl = EasyMock.createControl(); - sessionControl.resetToNice(); - - sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class ); - session = sessionControl.createMock( RepositorySession.class ); - - EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session ); - EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository ); + sessionFactory = mock( RepositorySessionFactory.class ); + session = mock( RepositorySession.class ); - sessionFactoryControl.replay(); - sessionControl.replay(); + when( sessionFactory.createSession() ).thenReturn( session ); + when( session.getRepository( ) ).thenReturn( metadataRepository ); newArtifactsProcessor.setRepositorySessionFactory( sessionFactory ); diff --git a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessorTest.java b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessorTest.java index 28b679a25..ddb35ff33 100644 --- a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessorTest.java +++ b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessorTest.java @@ -34,8 +34,6 @@ import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.rss.RssFeedGenerator; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,8 +49,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.easymock.EasyMock.createControl; -import static org.easymock.EasyMock.expect; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + @RunWith(ArchivaBlockJUnit4ClassRunner.class) public class NewVersionsOfArtifactRssFeedProcessorTest @@ -66,17 +65,12 @@ public class NewVersionsOfArtifactRssFeedProcessorTest private static final String ARTIFACT_ID = "artifact-two"; - private IMocksControl metadataRepositoryControl; - private MetadataRepository metadataRepository; - private IMocksControl sessionFactoryControl; private RepositorySessionFactory sessionFactory; - private IMocksControl sessionControl; private RepositorySession session; - private IMocksControl repositoryRegistryControl; private RepositoryRegistry repositoryRegistry; @@ -90,28 +84,20 @@ public class NewVersionsOfArtifactRssFeedProcessorTest newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor(); newVersionsProcessor.setGenerator( new RssFeedGenerator() ); - metadataRepositoryControl = createControl(); - metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class ); + metadataRepository = mock( MetadataRepository.class ); - sessionFactoryControl = EasyMock.createControl(); - sessionControl = EasyMock.createControl(); - sessionControl.resetToNice(); - sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class ); - session = sessionControl.createMock( RepositorySession.class ); + sessionFactory = mock( RepositorySessionFactory.class ); + session = mock( RepositorySession.class ); - EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session ); - EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository ); - sessionFactoryControl.replay(); - sessionControl.replay(); + when( sessionFactory.createSession() ).thenReturn( session ); + when( session.getRepository( ) ).thenReturn( metadataRepository ); - repositoryRegistryControl = EasyMock.createControl(); - repositoryRegistry = repositoryRegistryControl.createMock( ArchivaRepositoryRegistry.class ); + repositoryRegistry = mock( ArchivaRepositoryRegistry.class ); List<Repository> reg = new ArrayList<>( ); reg.add( new BasicManagedRepository( TEST_REPO, TEST_REPO, new FilesystemStorage( Paths.get("target/test-storage"), new DefaultFileLockManager() ) ) ); - EasyMock.expect( repositoryRegistry.getRepositories() ).andStubReturn( reg ); - repositoryRegistryControl.replay(); + when( repositoryRegistry.getRepositories() ).thenReturn( reg ); newVersionsProcessor.setRepositorySessionFactory( sessionFactory ); newVersionsProcessor.setRepositoryRegistry( repositoryRegistry ); @@ -137,15 +123,14 @@ public class NewVersionsOfArtifactRssFeedProcessorTest reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID ); reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID ); - expect(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn( + when(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).thenReturn( Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT")); - expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn( + when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).thenReturn( Collections.singletonList(artifact1)); - expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).andReturn( + when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).thenReturn( Collections.singletonList(artifact2)); - expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn( + when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).thenReturn( Collections.singletonList(artifact3)); - metadataRepositoryControl.replay(); SyndFeed feed = newVersionsProcessor.process( reqParams ); @@ -166,7 +151,6 @@ public class NewVersionsOfArtifactRssFeedProcessorTest assertEquals( whenGatheredNext.toInstant(), entries.get( 1 ).getPublishedDate().toInstant() ); - metadataRepositoryControl.verify(); } private ArtifactMetadata createArtifact(ZonedDateTime whenGathered, String version ) diff --git a/archiva-modules/archiva-web/archiva-security/pom.xml b/archiva-modules/archiva-web/archiva-security/pom.xml index c56bd89bb..e84139a5c 100644 --- a/archiva-modules/archiva-web/archiva-security/pom.xml +++ b/archiva-modules/archiva-web/archiva-security/pom.xml @@ -147,6 +147,11 @@ <!-- Test Scoped --> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.archiva.maven</groupId> <artifactId>archiva-maven-proxy</artifactId> <scope>test</scope> @@ -162,11 +167,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.apache.archiva.redback</groupId> <artifactId>redback-keys-jpa</artifactId> <scope>test</scope> diff --git a/archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/archiva/security/ArchivaServletAuthenticatorTest.java b/archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/archiva/security/ArchivaServletAuthenticatorTest.java index 9258abea8..75a1887b2 100644 --- a/archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/archiva/security/ArchivaServletAuthenticatorTest.java +++ b/archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/archiva/security/ArchivaServletAuthenticatorTest.java @@ -27,8 +27,6 @@ import org.apache.archiva.redback.system.SecuritySession; import org.apache.archiva.redback.users.User; import org.apache.archiva.redback.users.UserManager; import org.apache.archiva.security.common.ArchivaRoleConstants; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.Before; import org.junit.Test; @@ -36,6 +34,9 @@ import javax.inject.Inject; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * ArchivaServletAuthenticatorTest */ @@ -46,8 +47,6 @@ public class ArchivaServletAuthenticatorTest @Named( value = "servletAuthenticator#test" ) private ServletAuthenticator servletAuth; - private IMocksControl httpServletRequestControl; - private HttpServletRequest request; @Before @@ -57,8 +56,7 @@ public class ArchivaServletAuthenticatorTest { super.setUp(); - httpServletRequestControl = EasyMock.createControl( ); - request = httpServletRequestControl.createMock( HttpServletRequest.class ); + request = mock( HttpServletRequest.class ); setupRepository( "corporate" ); } @@ -126,7 +124,7 @@ public class ArchivaServletAuthenticatorTest assignRepositoryObserverRole( USER_ALPACA, "corporate" ); //httpServletRequestControl.expectAndReturn( request.getRemoteAddr(), "192.168.111.111" ); - EasyMock.expect( request.getRemoteAddr() ).andReturn( "192.168.111.111" ); + when( request.getRemoteAddr() ).thenReturn( "192.168.111.111" ); UserManager userManager = securitySystem.getUserManager(); User user = userManager.findUser( USER_ALPACA ); @@ -135,8 +133,6 @@ public class ArchivaServletAuthenticatorTest SecuritySession session = new DefaultSecuritySession( result, user ); - httpServletRequestControl.replay(); - try { servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ); @@ -147,8 +143,6 @@ public class ArchivaServletAuthenticatorTest assertEquals( "Access denied for repository corporate", e.getMessage() ); } - httpServletRequestControl.verify(); - restoreGuestInitialValues( USER_ALPACA ); } diff --git a/archiva-modules/archiva-web/archiva-webdav/pom.xml b/archiva-modules/archiva-web/archiva-webdav/pom.xml index dcfacf75c..747293b41 100644 --- a/archiva-modules/archiva-web/archiva-webdav/pom.xml +++ b/archiva-modules/archiva-web/archiva-webdav/pom.xml @@ -278,8 +278,8 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> <dependency> diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java index 2de2b9a78..3650a9704 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java @@ -56,12 +56,11 @@ import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.DavResourceLocator; import org.apache.jackrabbit.webdav.DavServletRequest; import org.apache.jackrabbit.webdav.DavServletResponse; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -78,7 +77,8 @@ import java.util.Locale; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; -import static org.easymock.EasyMock.*; +import static org.mockito.Mockito.*; + /** * ArchivaDavResourceFactoryTest @@ -102,26 +102,16 @@ public class ArchivaDavResourceFactoryTest private OverridingArchivaDavResourceFactory resourceFactory; - private IMocksControl requestControl; - private DavServletRequest request; - private IMocksControl repoRequestControl; - private MavenRepositoryRequestInfo repoRequest; - private IMocksControl responseControl; - private DavServletResponse response; - private IMocksControl archivaConfigurationControl; - private ArchivaConfiguration archivaConfiguration; private Configuration config; - private IMocksControl repoContentFactoryControl; - private RepositoryContentFactory repoFactory; @Inject @@ -184,26 +174,19 @@ public class ArchivaDavResourceFactoryTest { super.setUp(); - requestControl = createControl(); - request = requestControl.createMock( DavServletRequest.class ); + request = mock( DavServletRequest.class ); - responseControl = createControl(); - response = responseControl.createMock( DavServletResponse.class ); + response = mock( DavServletResponse.class ); //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); - archivaConfigurationControl = createControl(); - archivaConfiguration = archivaConfigurationControl.createMock( ArchivaConfiguration.class ); + archivaConfiguration = mock( ArchivaConfiguration.class ); config = new Configuration(); - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2, 25 ); - expect (archivaConfiguration.getDefaultLocale()).andReturn( Locale.getDefault() ).anyTimes(); - archivaConfiguration.addListener( EasyMock.anyObject( ) ); - expectLastCall().times(0, 4); + when( archivaConfiguration.getConfiguration() ).thenReturn( config ); + when(archivaConfiguration.getDefaultLocale()).thenReturn( Locale.getDefault() ); + archivaConfiguration.addListener( any() ); archivaConfiguration.save( eq(config)); - expectLastCall().times( 0, 5 ); - archivaConfiguration.save( eq(config), EasyMock.anyString()); - expectLastCall().times( 0, 5 ); - archivaConfigurationControl.replay(); + archivaConfiguration.save( eq(config), anyString()); defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfiguration ); repositoryRegistry.setArchivaConfiguration( archivaConfiguration ); @@ -233,11 +216,9 @@ public class ArchivaDavResourceFactoryTest defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null ); } - repoContentFactoryControl = createControl(); - repoFactory = repoContentFactoryControl.createMock( RepositoryContentFactory.class ); + repoFactory = mock( RepositoryContentFactory.class ); - repoRequestControl = createControl(); - repoRequest = repoRequestControl.createMock( MavenRepositoryRequestInfo.class ); + repoRequest = mock( MavenRepositoryRequestInfo.class ); resourceFactory = new OverridingArchivaDavResourceFactory( applicationContext, archivaConfiguration ); @@ -248,6 +229,12 @@ public class ArchivaDavResourceFactoryTest resourceFactory.setRemoteRepositoryAdmin( remoteRepositoryAdmin ); resourceFactory.setManagedRepositoryAdmin( defaultManagedRepositoryAdmin ); resourceFactory.setRepositoryRegistry( repositoryRegistry ); + verify( archivaConfiguration, atLeast( 2 )).getConfiguration(); + verify( archivaConfiguration, atMost( 25 )).getConfiguration(); + verify( archivaConfiguration, atMost( 4 ) ).addListener( any() ); + verify( archivaConfiguration, atMost( 5 ) ).save( eq(config) ); + verify( archivaConfiguration, atMost( 5 ) ).save( eq(config), anyString() ); + } private ManagedRepository createManagedRepository( String id, String location, String layout ) @@ -349,51 +336,51 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); - - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 ); + reset( archivaConfiguration ); + reset( request ); + reset( repoFactory ); + when( archivaConfiguration.getConfiguration( ) ).thenReturn( config ); - expect( request.getMethod() ).andReturn( "GET" ).times( 3 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 ); + when( request.getPathInfo() ).thenReturn( "org/apache/archiva" ); - expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo ); + when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 ); + when( request.getRemoteAddr( ) ).thenReturn( "http://localhost:8080" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 ); + when( request.getDavSession( ) ).thenReturn( new ArchivaDavSession( ) ); - expect( request.getContextPath() ).andReturn( "" ).times( 2 ); + when( request.getContextPath( ) ).thenReturn( "" ); - expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( true ); + when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( true ); - expect( - repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( + when( + repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( "legacy" ); - expect( repoRequest.toItemSelector( - "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ); + when( repoRequest.toItemSelector( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null ); - expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" - ) ).andReturn( + when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" + ) ).thenReturn( Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString()); - expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo ); + when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo ); - expect( repoRequest.isArchetypeCatalog( - "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ); - archivaConfigurationControl.replay(); - requestControl.replay(); - repoContentFactoryControl.replay(); - repoRequestControl.replay(); + when( repoRequest.isArchetypeCatalog( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false ); resourceFactory.createResource( locator, request, response ); - archivaConfigurationControl.verify(); - requestControl.verify(); - repoContentFactoryControl.verify(); - repoRequestControl.verify(); + verify(archivaConfiguration, times( 3 )).getConfiguration(); + verify( request, times( 3 ) ).getMethod( ); + verify( request, atMost( 2 ) ).getPathInfo( ); + verify(request,times( 2 )).getRemoteAddr(); + verify( request, times( 2 ) ).getDavSession( ); + verify( request, times( 2 ) ).getContextPath( ); + fail( "A DavException with 401 error code should have been thrown." ); } @@ -427,52 +414,51 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); + reset( archivaConfiguration ); + reset( request ); + reset( repoFactory ); - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 ); + when( archivaConfiguration.getConfiguration( ) ).thenReturn( config ); - expect( request.getMethod() ).andReturn( "GET" ).times( 3 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 ); + when( request.getPathInfo() ).thenReturn( "org/apache/archiva" ); - expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo ); + when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo ); - expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo ); + when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 ); + when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 ); + when( request.getDavSession() ).thenReturn( new ArchivaDavSession() ); - expect( request.getContextPath() ).andReturn( "" ).times( 2 ); + when( request.getContextPath() ).thenReturn( "" ); - expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ); + when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false ); - expect( - repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( + when( + repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( "legacy" ); - expect( repoRequest.toItemSelector( - "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ); + when( repoRequest.toItemSelector( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null ); - expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" - ) ).andReturn( + when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" + ) ).thenReturn( Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString()); - expect( repoRequest.isArchetypeCatalog( - "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ); - archivaConfigurationControl.replay(); - requestControl.replay(); - repoContentFactoryControl.replay(); - repoRequestControl.replay(); + when( repoRequest.isArchetypeCatalog( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false ); resourceFactory.createResource( locator, request, response ); - - archivaConfigurationControl.verify(); - requestControl.verify(); - repoContentFactoryControl.verify(); - repoRequestControl.verify(); + verify( archivaConfiguration, times( 3 ) ).getConfiguration( ); + verify( request, times( 3 ) ).getMethod(); + verify( request, atMost( 2 ) ).getPathInfo( ); + verify( request, times( 2 ) ).getRemoteAddr( ); + verify( request, times( 2 ) ).getDavSession( ); + verify( request, times( 2 ) ).getContextPath( ); fail( "A DavException with 401 error code should have been thrown." ); } @@ -511,55 +497,59 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); + reset( archivaConfiguration ); + reset( request ); + reset( repoFactory ); - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 ); + when( archivaConfiguration.getConfiguration() ).thenReturn( config ); - expect( request.getMethod() ).andReturn( "GET" ).times( 5 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 ); + when( request.getPathInfo() ).thenReturn( "org/apache/archiva" ); - expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo ); + when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo ); - expect( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).andReturn( localMirrorRepo ); + when( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).thenReturn( localMirrorRepo ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 4 ); + when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 4 ); + when( request.getDavSession() ).thenReturn( new ArchivaDavSession() ); - expect( request.getContextPath() ).andReturn( "" ).times( 2 ); + when( request.getContextPath() ).thenReturn( "" ); - expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 ); + when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false ); - expect( - repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( - "legacy" ).times( 2 ); + when( + repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( + "legacy" ); - expect( repoRequest.toItemSelector( - "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ).times( 2 ); + when( repoRequest.toItemSelector( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null ); - expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" - ) ).andReturn( + when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" + ) ).thenReturn( Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString() ); - expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" + when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ) - .andReturn( Paths.get( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(), + .thenReturn( Paths.get( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString()); - expect( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 ); - archivaConfigurationControl.replay(); - requestControl.replay(); - repoContentFactoryControl.replay(); - repoRequestControl.replay(); + when( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false ); resourceFactory.createResource( locator, request, response ); - - archivaConfigurationControl.verify(); - requestControl.verify(); - repoContentFactoryControl.verify(); - repoRequestControl.verify(); + verify( archivaConfiguration, times( 3 ) ).getConfiguration( ); + verify( request, times( 5 ) ).getMethod( ); + verify( request, atMost( 2 ) ).getPathInfo( ); + verify( request, times( 4 ) ).getRemoteAddr( ); + verify( request, times( 4 ) ).getDavSession( ); + verify( request, times( 2 ) ).getContextPath( ); + verify( repoRequest, times( 2 ) ).isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ); + verify(repoRequest, times( 2 )).getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ); + verify( repoRequest, times( 2 ) ).toItemSelector( + "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ); + verify( repoRequest, times( 2 ) ).isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ); fail( "A DavException with 404 error code should have been thrown." ); } @@ -586,39 +576,27 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); + reset( request ); - expect( request.getMethod() ).andReturn( "GET" ).times( 4 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 ); + when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" ); - expect( request.getContextPath() ).andReturn( "" ).times( 1 ); + when( request.getContextPath() ).thenReturn( "" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 ); + when( request.getDavSession() ).thenReturn( new ArchivaDavSession() ); - expect( request.getRequestURI() ).andReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" ); + when( request.getRequestURI() ).thenReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" ); response.setHeader( "Pragma", "no-cache" ); - - expectLastCall(); - response.setHeader( "Cache-Control", "no-cache" ); - - expectLastCall(); - response.setDateHeader( eq("Last-Modified"), anyLong() ); - expectLastCall(); - - archivaConfigurationControl.replay(); - repoContentFactoryControl.replay(); - requestControl.replay(); - responseControl.replay(); resourceFactory.createResource( locator, request, response ); + verify( request, times( 4 ) ).getMethod( ); + verify( request, times( 3 ) ).getRemoteAddr( ); + verify( request, times( 1 ) ).getContextPath( ); + verify( request, times( 2 ) ).getDavSession( ); - archivaConfigurationControl.verify(); - repoContentFactoryControl.verify(); - requestControl.verify(); - responseControl.verify(); } catch ( DavException e ) { @@ -640,29 +618,28 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); - - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 ); + reset( archivaConfiguration ); + reset( request ); + reset( repoFactory ); - expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo ); + when( archivaConfiguration.getConfiguration() ).thenReturn( config ); - expect( request.getMethod() ).andReturn( "GET" ).times( 3 ); + when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 ); + when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" ); - expect( request.getContextPath() ).andReturn( "" ).times( 2 ); + when( request.getDavSession() ).thenReturn( new ArchivaDavSession() ); - archivaConfigurationControl.replay(); - repoContentFactoryControl.replay(); - requestControl.replay(); + when( request.getContextPath() ).thenReturn( "" ); resourceFactory.createResource( locator, request, response ); - - archivaConfigurationControl.verify(); - repoContentFactoryControl.verify(); - requestControl.verify(); + verify( archivaConfiguration, times( 2 ) ).getConfiguration( ); + verify( request, times( 3 ) ).getMethod( ); + verify( request, times( 3 ) ).getRemoteAddr( ); + verify( request, times( 2 ) ).getDavSession( ); + verify( request, times( 2 ) ).getContextPath( ); fail( "A 404 error should have been thrown!" ); } @@ -693,29 +670,30 @@ public class ArchivaDavResourceFactoryTest try { - archivaConfigurationControl.reset(); - - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 ); + reset( archivaConfiguration ); + reset( request ); + reset( repoFactory ); - expect( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).andReturn( legacyRepo ); + when( archivaConfiguration.getConfiguration() ).thenReturn( config ); - expect( request.getMethod() ).andReturn( "GET" ).times( 3 ); + when( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).thenReturn( legacyRepo ); - expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 ); + when( request.getMethod() ).thenReturn( "GET" ); - expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 ); + when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" ); - expect( request.getContextPath() ).andReturn( "" ).times( 2 ); + when( request.getDavSession() ).thenReturn( new ArchivaDavSession() ); - archivaConfigurationControl.replay(); - repoContentFactoryControl.replay(); - requestControl.replay(); + when( request.getContextPath() ).thenReturn( "" ); resourceFactory.createResource( locator, request, response ); - archivaConfigurationControl.verify(); - repoContentFactoryControl.verify(); - requestControl.verify(); + verify( archivaConfiguration, + times( 2 ) ).getConfiguration( ); + verify( request, times( 3 ) ).getMethod( ); + verify( request, times( 3 ) ).getRemoteAddr( ); + verify( request, times( 2 ) ).getDavSession( ); + verify( request, times( 2 ) ).getContextPath( ); fail( "A 404 error should have been thrown!" ); } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java index 36e1f1dca..bba326e65 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java @@ -43,13 +43,12 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jackrabbit.webdav.DavSessionProvider; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -72,8 +71,11 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.eq; +import static org.mockito.AdditionalMatchers.not; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + /** * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily @@ -98,12 +100,8 @@ public class RepositoryServletSecurityTest private DavSessionProvider davSessionProvider; - private IMocksControl servletAuthControl; - private ServletAuthenticator servletAuth; - private IMocksControl httpAuthControl; - private HttpAuthenticator httpAuth; private RepositoryServlet servlet; @@ -164,13 +162,9 @@ public class RepositoryServletSecurityTest CacheManager.getInstance().clearAll(); - servletAuthControl = EasyMock.createControl(); - - servletAuth = servletAuthControl.createMock( ServletAuthenticator.class ); - - httpAuthControl = EasyMock.createControl(); + servletAuth = mock( ServletAuthenticator.class ); - httpAuth = httpAuthControl.createMock( HttpAuthenticator.class ); + httpAuth = mock( HttpAuthenticator.class ); davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth ); @@ -257,20 +251,16 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ), - EasyMock.anyObject( AuthenticationResult.class ) ); - EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) ); + when(servletAuth.isAuthenticated( any( ), + any( ) )).thenThrow( new AuthenticationException( "Authentication error" ) ); - servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ); + when(servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT )) + .thenThrow( new UnauthorizedException( "'guest' has no write access to repository" ) ); - EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) ); - - httpAuthControl.replay(); - servletAuthControl.replay(); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); mockHttpServletRequest.setMethod( "PUT" ); @@ -282,9 +272,6 @@ public class RepositoryServletSecurityTest servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); } @@ -293,7 +280,6 @@ public class RepositoryServletSecurityTest public void testPutWithInvalidUserAndGuestHasWriteAccess() throws Exception { - servlet.setDavSessionProvider( davSessionProvider ); ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory(); @@ -304,40 +290,35 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andThrow( - new AuthenticationException( "Authentication error" ) ); - EasyMock.expect( servletAuth.isAuthorized( "guest", "internal", - ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).andReturn( + when( servletAuth.isAuthorized( "guest", "internal", + ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).thenReturn( true ); // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session ); + when( httpAuth.getSecuritySession( any( ) ) ).thenReturn( session ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow( + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenThrow( new AuthenticationException( "Authentication error" ) ); - EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null ); + when( httpAuth.getSessionUser( any( ) ) ).thenReturn( null ); // check if guest has write access - EasyMock.expect( servletAuth.isAuthorized( "guest", "internal", - ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).andReturn( + when( servletAuth.isAuthorized( "guest", "internal", + ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).thenReturn( true ); - httpAuthControl.replay(); - servletAuthControl.replay(); - InputStream is = getClass().getResourceAsStream( "/artifact.jar" ); assertNotNull( "artifact.jar inputstream", is ); @@ -352,9 +333,6 @@ public class RepositoryServletSecurityTest servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() ); } @@ -373,37 +351,34 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andReturn( true ); + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenReturn( true ); // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); - EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn( + when( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).thenReturn( session ); - EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() ); + when( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).thenReturn( new SimpleUser() ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn( + when( servletAuth.isAuthenticated( any( ), eq( result ) ) ).thenReturn( true ); - EasyMock.expect( - servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ), - eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).andThrow( + when( + servletAuth.isAuthorized( any( ), eq( session ), eq( "internal" ), + eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).thenThrow( new UnauthorizedException( "User not authorized" ) ); - httpAuthControl.replay(); - servletAuthControl.replay(); - InputStream is = getClass().getResourceAsStream( "/artifact.jar" ); assertNotNull( "artifact.jar inputstream", is ); @@ -416,10 +391,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); } @@ -447,12 +418,12 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andReturn( true ); + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenReturn( true ); User user = new SimpleUser(); user.setUsername( "admin" ); @@ -460,23 +431,20 @@ public class RepositoryServletSecurityTest // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session ); + when( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).thenReturn( session ); - EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user ); + when( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).thenReturn( user ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn( + when( servletAuth.isAuthenticated( any( ), eq( result ) ) ).thenReturn( true ); - EasyMock.expect( - servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ), - eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).andReturn( true ); - - httpAuthControl.replay(); - servletAuthControl.replay(); + when( + servletAuth.isAuthorized( any( ), eq( session ), eq( "internal" ), + eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).thenReturn( true ); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); mockHttpServletRequest.setMethod( "PUT" ); @@ -487,10 +455,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() ); assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() ); @@ -519,38 +483,31 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andThrow( - new AuthenticationException( "Authentication error" ) ); - EasyMock.expect( servletAuth.isAuthorized( "guest", "internal", - ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).andReturn( + when( servletAuth.isAuthorized( "guest", "internal", + ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).thenReturn( true ); // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( - result ); - - EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session ); + when( httpAuth.getSecuritySession( any( ) ) ).thenReturn( session ); - EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null ); + when( httpAuth.getSessionUser( any( ) ) ).thenReturn( null ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn( + when( servletAuth.isAuthenticated( any( ), eq( result ) ) ).thenReturn( true ); + when( servletAuth.isAuthenticated( any( ), + not(eq(result)) ) ).thenThrow( + new AuthenticationException( "Authentication error" ) ); - EasyMock.expect( - servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ), - eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andReturn( true ); - httpAuthControl.replay(); - servletAuthControl.replay(); - + when( + servletAuth.isAuthorized( any( ), eq( session ), eq( "internal" ), + eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).thenReturn( true ); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); mockHttpServletRequest.setMethod( "GET" ); @@ -559,10 +516,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() ); assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() ); @@ -585,20 +538,17 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andThrow( + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenThrow( new AuthenticationException( "Authentication error" ) ); - EasyMock.expect( servletAuth.isAuthorized( "guest", "internal", - ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).andReturn( + when( servletAuth.isAuthorized( "guest", "internal", + ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).thenReturn( false ); - httpAuthControl.replay(); - servletAuthControl.replay(); - MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); mockHttpServletRequest.setMethod( "GET" ); @@ -607,10 +557,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); } @@ -637,32 +583,29 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andReturn( true ); + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenReturn( true ); // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session ); + when( httpAuth.getSecuritySession( any( ) ) ).thenReturn( session ); - EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() ); + when( httpAuth.getSessionUser( any( ) ) ).thenReturn( new SimpleUser() ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn( + when( servletAuth.isAuthenticated( any( ), eq( result ) ) ).thenReturn( true ); - EasyMock.expect( - servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ), - eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andReturn( true ); - - httpAuthControl.replay(); - servletAuthControl.replay(); + when( + servletAuth.isAuthorized( any( ), eq( session ), eq( "internal" ), + eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).thenReturn( true ); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); @@ -672,10 +615,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() ); assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() ); } @@ -703,34 +642,31 @@ public class RepositoryServletSecurityTest AuthenticationResult result = new AuthenticationResult(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), - anyObject( AuthenticationResult.class ) ) ).andReturn( true ); + when( servletAuth.isAuthenticated( any( ), + any( ) ) ).thenReturn( true ); // ArchivaDavResourceFactory#isAuthorized() SecuritySession session = new DefaultSecuritySession(); - EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), - anyObject( HttpServletResponse.class ) ) ).andReturn( + when( httpAuth.getAuthenticationResult( any( ), + any( ) ) ).thenReturn( result ); - EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session ); + when( httpAuth.getSecuritySession( any( ) ) ).thenReturn( session ); - EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() ); + when( httpAuth.getSessionUser( any( ) ) ).thenReturn( new SimpleUser() ); - EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn( + when( servletAuth.isAuthenticated( any( ), eq( result ) ) ).thenReturn( true ); - EasyMock.expect( - servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ), - eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andThrow( + when( + servletAuth.isAuthorized( any( ), eq( session ), eq( "internal" ), + eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).thenThrow( new UnauthorizedException( "User not authorized to read repository." ) ); - httpAuthControl.replay(); - servletAuthControl.replay(); - MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.addHeader( "User-Agent", "foo" ); mockHttpServletRequest.setMethod( "GET" ); @@ -740,10 +676,6 @@ public class RepositoryServletSecurityTest MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); - - httpAuthControl.verify(); - servletAuthControl.verify(); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); } |