import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import com.opensymphony.xwork2.Action;
+import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
+import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
+import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.struts2.StrutsSpringTestCase;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
import org.easymock.MockControl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.Arrays;
public class RepositoriesActionTest
extends StrutsSpringTestCase
{
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
private RepositoriesAction action;
+ ArchivaConfiguration originalArchivaConfiguration;
+
protected void setUp()
throws Exception
{
+
super.setUp();
+
action = (RepositoriesAction) getActionProxy( "/admin/index.action" ).getAction();
+ originalArchivaConfiguration =
+ ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).getArchivaConfiguration();
+ // some other test are modifying archivaConfiguration with a mocked instance : this test need the real one
+ // so use the real one from spring, backup the mock and restore it at the end (tearDown)
+ ArchivaConfiguration real = applicationContext.getBean( ArchivaConfiguration.class );
+ ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration( real );
+ ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( real );
+ ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration( real );
+ }
+
+ @Override
+ protected void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+ ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration(
+ originalArchivaConfiguration );
+ ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
+ originalArchivaConfiguration );
+ ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
+ originalArchivaConfiguration );
}
@Override
public void testGetRepositories()
throws Exception
{
- MockControl control = MockControl.createControl( MetadataRepository.class );
- MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
- control.expectAndReturn( metadataRepository.getMetadataFacets( "internal", RepositoryStatistics.FACET_ID ),
- Arrays.asList( "20091125.123456.678" ) );
- control.expectAndReturn(
- metadataRepository.getMetadataFacet( "internal", RepositoryStatistics.FACET_ID, "20091125.123456.678" ),
- new RepositoryStatistics() );
- control.expectAndReturn( metadataRepository.getMetadataFacets( "snapshots", RepositoryStatistics.FACET_ID ),
- Arrays.asList( "20091112.012345.012" ) );
- control.expectAndReturn(
- metadataRepository.getMetadataFacet( "snapshots", RepositoryStatistics.FACET_ID, "20091112.012345.012" ),
- new RepositoryStatistics() );
- control.replay();
-
- RepositorySession session = mock( RepositorySession.class );
- when( session.getRepository() ).thenReturn( metadataRepository );
- TestRepositorySessionFactory factory =
- applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
- factory.setRepositorySession( session );
-
- ServletRunner sr = new ServletRunner();
- ServletUnitClient sc = sr.newClient();
-
- action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositories.action" ).getRequest() );
-
- action.prepare();
- String result = action.execute();
- assertEquals( Action.SUCCESS, result );
-
- // TODO: for some reason servletunit is not populating the port of the servlet request
- assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
-
- assertNotNull( action.getManagedRepositories() );
- assertNotNull( action.getRemoteRepositories() );
- assertNotNull( action.getRepositoryStatistics() );
-
- assertEquals( 2, action.getManagedRepositories().size() );
- assertEquals( 2, action.getRemoteRepositories().size() );
- assertEquals( 2, action.getRepositoryStatistics().size() );
-
- control.verify();
+ try
+ {
+ MockControl control = MockControl.createControl( MetadataRepository.class );
+ MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
+ control.expectAndReturn( metadataRepository.getMetadataFacets( "internal", RepositoryStatistics.FACET_ID ),
+ Arrays.asList( "20091125.123456.678" ) );
+ control.expectAndReturn(
+ metadataRepository.getMetadataFacet( "internal", RepositoryStatistics.FACET_ID, "20091125.123456.678" ),
+ new RepositoryStatistics() );
+ control.expectAndReturn( metadataRepository.getMetadataFacets( "snapshots", RepositoryStatistics.FACET_ID ),
+ Arrays.asList( "20091112.012345.012" ) );
+ control.expectAndReturn( metadataRepository.getMetadataFacet( "snapshots", RepositoryStatistics.FACET_ID,
+ "20091112.012345.012" ),
+ new RepositoryStatistics() );
+ control.replay();
+
+ RepositorySession session = mock( RepositorySession.class );
+ when( session.getRepository() ).thenReturn( metadataRepository );
+ TestRepositorySessionFactory factory =
+ applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
+ factory.setRepositorySession( session );
+
+ ServletRunner sr = new ServletRunner();
+ ServletUnitClient sc = sr.newClient();
+
+ action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositories.action" ).getRequest() );
+
+ action.prepare();
+ String result = action.execute();
+ assertEquals( Action.SUCCESS, result );
+
+ // TODO: for some reason servletunit is not populating the port of the servlet request
+ assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
+
+ assertNotNull( action.getManagedRepositories() );
+ assertNotNull( action.getRemoteRepositories() );
+ assertNotNull( action.getRepositoryStatistics() );
+
+ assertEquals( 2, action.getManagedRepositories().size() );
+ assertEquals( 2, action.getRemoteRepositories().size() );
+ assertEquals( 2, action.getRepositoryStatistics().size() );
+
+ control.verify();
+ }
+ catch ( Exception e )
+ {
+ log.error( e.getMessage(), e );
+ throw e;
+ }
}
public void testSecureActionBundle()
* under the License.
*/
+import com.opensymphony.xwork2.Action;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
import org.codehaus.redback.integration.interceptor.SecureActionException;
import org.easymock.MockControl;
-import com.opensymphony.xwork2.Action;
-
/**
* SortRepositoriesActionTest
- *
- * @version
*/
-public class SortRepositoriesActionTest
+public class SortRepositoriesActionTest
extends StrutsSpringTestCase
{
private static final String REPO_GROUP_ID = "repo-group-ident";
-
+
private static final String REPO1_ID = "managed-repo-ident-1";
-
+
private static final String REPO2_ID = "managed-repo-ident-2";
-
+
private static final String REPO3_ID = "managed-repo-ident-3";
-
+
private MockControl archivaConfigurationControl;
private ArchivaConfiguration archivaConfiguration;
-
+
+ private ArchivaConfiguration originalArchivaConfiguration;
+
private SortRepositoriesAction action;
@Override
throws Exception
{
super.setUp();
-
- //action = (SortRepositoriesAction) lookup( Action.class.getName(), "sortRepositoriesAction" );
-
action = (SortRepositoriesAction) getActionProxy( "/admin/sortDownRepositoryFromGroup.action" ).getAction();
archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
+ originalArchivaConfiguration = action.archivaConfiguration;
action.setArchivaConfiguration( archivaConfiguration );
}
-
+
+ @Override
+ protected void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+ action.archivaConfiguration = originalArchivaConfiguration;
+ }
+
public void testSecureActionBundle()
throws SecureActionException
{
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( new Configuration() );
archivaConfigurationControl.replay();
-
+
SecureActionBundle bundle = action.getSecureActionBundle();
assertTrue( bundle.requiresAuthentication() );
assertEquals( 1, bundle.getAuthorizationTuples().size() );
}
-
+
public void testSortDownFirstRepository()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 4 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
- RepositoryGroupConfiguration repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
+
+ RepositoryGroupConfiguration repoGroup =
+ (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
java.util.List<String> repositories = repoGroup.getRepositories();
-
+
assertEquals( 3, repositories.size() );
assertEquals( REPO1_ID, repositories.get( 0 ) );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
-
+
// sort down first repo
action.setRepoGroupId( repoGroup.getId() );
action.setTargetRepo( REPO1_ID );
-
+
String result = action.sortDown();
assertEquals( Action.SUCCESS, result );
-
+
repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
repositories = repoGroup.getRepositories();
assertEquals( 3, repositories.size() );
assertEquals( REPO1_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
}
-
+
public void testSortDownLastRepository()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 4 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
- RepositoryGroupConfiguration repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
+
+ RepositoryGroupConfiguration repoGroup =
+ (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
java.util.List<String> repositories = repoGroup.getRepositories();
-
+
assertEquals( 3, repositories.size() );
assertEquals( REPO1_ID, repositories.get( 0 ) );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
-
+
// sort down last repo
action.setRepoGroupId( repoGroup.getId() );
action.setTargetRepo( REPO3_ID );
-
+
String result = action.sortDown();
assertEquals( Action.SUCCESS, result );
-
+
repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
repositories = repoGroup.getRepositories();
assertEquals( 3, repositories.size() );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
}
-
+
public void testSortUpLastRepository()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 4 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
- RepositoryGroupConfiguration repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
+
+ RepositoryGroupConfiguration repoGroup =
+ (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
java.util.List<String> repositories = repoGroup.getRepositories();
-
+
assertEquals( 3, repositories.size() );
assertEquals( REPO1_ID, repositories.get( 0 ) );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
-
+
// sort up last repo
action.setRepoGroupId( repoGroup.getId() );
action.setTargetRepo( REPO3_ID );
-
+
String result = action.sortUp();
assertEquals( Action.SUCCESS, result );
-
+
repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
repositories = repoGroup.getRepositories();
assertEquals( 3, repositories.size() );
assertEquals( REPO3_ID, repositories.get( 1 ) );
assertEquals( REPO2_ID, repositories.get( 2 ) );
}
-
+
public void testSortUpFirstRepository()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 4 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
- RepositoryGroupConfiguration repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
+
+ RepositoryGroupConfiguration repoGroup =
+ (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
java.util.List<String> repositories = repoGroup.getRepositories();
-
+
assertEquals( 3, repositories.size() );
assertEquals( REPO1_ID, repositories.get( 0 ) );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
-
+
// sort up first repo
action.setRepoGroupId( repoGroup.getId() );
action.setTargetRepo( REPO1_ID );
-
+
String result = action.sortUp();
assertEquals( Action.SUCCESS, result );
-
+
repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
repositories = repoGroup.getRepositories();
assertEquals( 3, repositories.size() );
assertEquals( REPO2_ID, repositories.get( 1 ) );
assertEquals( REPO3_ID, repositories.get( 2 ) );
}
-
+
private Configuration createInitialConfiguration()
{
Configuration config = new Configuration();
-
+
RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
repoGroup.setId( REPO_GROUP_ID );
repoGroup.addRepository( REPO1_ID );
repoGroup.addRepository( REPO2_ID );
repoGroup.addRepository( REPO3_ID );
-
+
config.addRepositoryGroup( repoGroup );
-
+
return config;
}
}