[MRM-462] add tests for RepositoriesAction

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@570337 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2007-08-28 06:53:41 +00:00
parent 9eaa9af1d6
commit 2fc7c72cf5
4 changed files with 222 additions and 7 deletions

View File

@ -21,7 +21,6 @@
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.Preparable;
import org.apache.commons.collections.Transformer;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
@ -56,11 +55,6 @@ public class RepositoriesAction
extends PlexusActionSupport
implements SecureAction, ServletRequestAware, Preparable
{
/**
* @plexus.requirement role-hint="adminrepoconfig"
*/
private Transformer repoConfigToAdmin;
/**
* @plexus.requirement
*/
@ -77,11 +71,14 @@ public class RepositoriesAction
*/
private ArchivaDAO dao;
/**
* Used to construct the repository WebDAV URL in the repository action.
*/
private String baseUrl;
public void setServletRequest( HttpServletRequest request )
{
// TODO! what is this?
// TODO: is there a better way to do this?
this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
}

View File

@ -0,0 +1,84 @@
package org.apache.maven.archiva.web.action.admin.repositories;
import junit.framework.Assert;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.database.RepositoryDAO;
import org.apache.maven.archiva.database.RepositoryProblemDAO;
import org.apache.maven.archiva.database.SimpleConstraint;
import org.apache.maven.archiva.model.RepositoryContentStatistics;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/*
* 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.
*/
/**
* Stub class for Archiva DAO to avoid having to set up a database for tests.
*
* @todo a mock would be better, but that won't play nicely with Plexus injection.
*/
public class ArchivaDAOStub
implements ArchivaDAO
{
private ArchivaConfiguration configuration;
public List query( SimpleConstraint constraint )
{
Assert.assertEquals( RepositoryContentStatistics.class, constraint.getResultClass() );
List<RepositoryContentStatistics> stats = new ArrayList<RepositoryContentStatistics>();
for ( String repo : configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() )
{
RepositoryContentStatistics statistics = new RepositoryContentStatistics();
statistics.setRepositoryId( repo );
stats.add( statistics );
}
return stats;
}
public Object save( Serializable obj )
{
throw new UnsupportedOperationException( "query not implemented for stub" );
}
public ArtifactDAO getArtifactDAO()
{
throw new UnsupportedOperationException( "query not implemented for stub" );
}
public ProjectModelDAO getProjectModelDAO()
{
throw new UnsupportedOperationException( "query not implemented for stub" );
}
public RepositoryDAO getRepositoryDAO()
{
throw new UnsupportedOperationException( "query not implemented for stub" );
}
public RepositoryProblemDAO getRepositoryProblemDAO()
{
throw new UnsupportedOperationException( "query not implemented for stub" );
}
}

View File

@ -0,0 +1,77 @@
package org.apache.maven.archiva.web.action.admin.repositories;
/*
* 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 com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import com.opensymphony.xwork.Action;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
/**
* Test the repositories action returns the correct data.
*/
public class RepositoriesActionTest
extends PlexusTestCase
{
private RepositoriesAction action;
protected void setUp()
throws Exception
{
super.setUp();
// TODO: purely to quiet logging - shouldn't be needed
String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
System.setProperty( "appserver.base", appserverBase );
action = (RepositoriesAction) lookup( Action.class.getName(), "repositoriesAction" );
}
public void testGetRepositories()
throws Exception
{
ServletRunner sr = new ServletRunner();
ServletUnitClient sc = sr.newClient();
action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositories.action" ).getRequest() );
action.prepare();
action.execute();
// 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() );
}
public void testSecureActionBundle()
throws SecureActionException
{
SecureActionBundle bundle = action.getSecureActionBundle();
assertTrue( bundle.requiresAuthentication() );
assertEquals( 1, bundle.getAuthorizationTuples().size() );
}
}

View File

@ -0,0 +1,57 @@
<!--
~ 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.
-->
<plexus>
<components>
<component>
<role>org.codehaus.plexus.logging.LoggerManager</role>
<implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
<lifecycle-handler>basic</lifecycle-handler>
</component>
<component>
<role>com.opensymphony.xwork.Action</role>
<role-hint>repositoriesAction</role-hint>
<implementation>org.apache.maven.archiva.web.action.admin.repositories.RepositoriesAction</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
<description>Shows the Repositories Tab for the administrator.</description>
<requirements>
<requirement>
<role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
<field-name>archivaConfiguration</field-name>
</requirement>
<requirement>
<role>org.apache.maven.archiva.database.ArchivaDAO</role>
<role-hint>stub</role-hint>
<field-name>dao</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.apache.maven.archiva.database.ArchivaDAO</role>
<role-hint>stub</role-hint>
<implementation>org.apache.maven.archiva.web.action.admin.repositories.ArchivaDAOStub</implementation>
<requirements>
<requirement>
<role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
<field-name>configuration</field-name>
</requirement>
</requirements>
</component>
</components>
</plexus>