1 package org.apache.maven.archiva.web.action.admin.repositories;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork.Action;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
30 import org.apache.maven.archiva.model.ArchivaProjectModel;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.redback.role.RoleManagerException;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38 import org.easymock.MockControl;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
46 * DeleteManagedRepositoryActionTest
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 public class DeleteManagedRepositoryActionTest
52 extends PlexusInSpringTestCase
54 private DeleteManagedRepositoryAction action;
56 private RoleManager roleManager;
58 private MockControl roleManagerControl;
60 private MockControl archivaConfigurationControl;
62 private ArchivaConfiguration archivaConfiguration;
64 private static final String REPO_ID = "repo-ident";
66 private File location;
69 protected String getPlexusConfigLocation()
71 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
74 protected void setUp()
79 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
81 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
82 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
83 action.setArchivaConfiguration( archivaConfiguration );
85 roleManagerControl = MockControl.createControl( RoleManager.class );
86 roleManager = (RoleManager) roleManagerControl.getMock();
87 action.setRoleManager( roleManager );
88 location = getTestFile( "target/test/location" );
91 public void testSecureActionBundle()
92 throws SecureActionException
94 archivaConfiguration.getConfiguration();
95 archivaConfigurationControl.setReturnValue( new Configuration() );
96 archivaConfigurationControl.replay();
99 SecureActionBundle bundle = action.getSecureActionBundle();
100 assertTrue( bundle.requiresAuthentication() );
101 assertEquals( 1, bundle.getAuthorizationTuples().size() );
104 public void testDeleteRepositoryConfirmation()
107 ManagedRepositoryConfiguration originalRepository = createRepository();
108 Configuration configuration = createConfigurationForEditing( originalRepository );
110 archivaConfiguration.getConfiguration();
111 archivaConfigurationControl.setReturnValue( configuration );
112 archivaConfigurationControl.replay();
114 action.setRepoid( REPO_ID );
117 assertEquals( REPO_ID, action.getRepoid() );
118 ManagedRepositoryConfiguration repository = action.getRepository();
119 assertNotNull( repository );
120 assertRepositoryEquals( repository, createRepository() );
122 String status = action.execute();
123 assertEquals( Action.SUCCESS, status );
124 repository = action.getRepository();
125 assertRepositoryEquals( repository, createRepository() );
126 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
129 public void testDeleteRepositoryKeepContent()
132 prepareRoleManagerMock();
134 Configuration configuration = prepDeletionTest( createRepository(), 4 );
136 String status = action.deleteEntry();
138 assertEquals( Action.SUCCESS, status );
140 assertTrue( configuration.getManagedRepositories().isEmpty() );
142 assertTrue( location.exists() );
145 public void testDeleteRepositoryDeleteContent()
148 prepareRoleManagerMock();
150 Configuration configuration = prepDeletionTest( createRepository(), 4 );
152 String status = action.deleteContents();
154 assertEquals( Action.SUCCESS, status );
156 assertTrue( configuration.getManagedRepositories().isEmpty() );
158 assertFalse( location.exists() );
161 public void testDeleteRepositoryAndAssociatedProxyConnectors()
164 Configuration configuration = prepDeletionTest( createRepository(), 5 );
165 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
166 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
167 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
169 prepareRoleManagerMock();
171 assertEquals( 1, configuration.getProxyConnectors().size() );
173 String status = action.deleteContents();
174 assertEquals( Action.SUCCESS, status );
176 assertTrue( configuration.getManagedRepositories().isEmpty() );
177 assertEquals( 0, configuration.getProxyConnectors().size() );
179 assertFalse( location.exists() );
182 public void testDeleteRepositoryCancelled()
185 ManagedRepositoryConfiguration originalRepository = createRepository();
186 Configuration configuration = prepDeletionTest( originalRepository, 3 );
187 String status = action.execute();
188 assertEquals( Action.SUCCESS, status );
190 ManagedRepositoryConfiguration repository = action.getRepository();
191 assertRepositoryEquals( repository, createRepository() );
192 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
194 assertTrue( location.exists() );
197 public void testDeleteRepositoryAndReposUnderRepoGroup()
200 Configuration configuration = prepDeletionTest( createRepository(), 5 );
201 List<String> repoIds = new ArrayList<String>();
202 repoIds.add( REPO_ID );
203 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
205 prepareRoleManagerMock();
207 assertEquals( 1, configuration.getRepositoryGroups().size() );
209 String status = action.deleteContents();
210 assertEquals( Action.SUCCESS, status );
212 assertTrue( configuration.getManagedRepositories().isEmpty() );
213 assertEquals( 0, ( ( RepositoryGroupConfiguration ) configuration.getRepositoryGroups().get( 0 ) ).getRepositories().size() );
215 assertFalse( location.exists() );
218 public void testDeleteRepositoryLocationReferencedInSysPropertiesError()
221 System.setProperty( "test.property", getTestFile( "target/test/location" ).getAbsolutePath() );
223 prepareRoleManagerMock();
225 Configuration configuration = prepDeletionTest( createRepository(), 4 );
227 String status = action.deleteContents();
229 assertEquals( Action.ERROR, status );
231 assertFalse( configuration.getManagedRepositories().isEmpty() );
233 assertTrue( location.exists() );
235 System.clearProperty( "test.property" );
238 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
239 throws RegistryException, IndeterminateConfigurationException
243 Configuration configuration = createConfigurationForEditing( originalRepository );
245 archivaConfiguration.getConfiguration();
246 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
248 archivaConfiguration.save( configuration );
249 archivaConfigurationControl.replay();
251 action.setRepoid( REPO_ID );
254 assertEquals( REPO_ID, action.getRepoid() );
255 ManagedRepositoryConfiguration repository = action.getRepository();
256 assertNotNull( repository );
257 assertRepositoryEquals( repository, createRepository() );
259 assertTrue( location.exists() );
260 return configuration;
263 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
264 ManagedRepositoryConfiguration actualRepository )
266 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
267 assertEquals( expectedRepository.getId(), actualRepository.getId() );
268 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
269 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
270 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
271 assertEquals( expectedRepository.getName(), actualRepository.getName() );
272 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
273 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
274 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
275 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
276 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
277 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
280 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
282 Configuration configuration = new Configuration();
283 configuration.addManagedRepository( repositoryConfiguration );
284 return configuration;
287 private ManagedRepositoryConfiguration createRepository()
289 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
291 r.setName( "repo name" );
292 r.setLocation( location.getAbsolutePath() );
293 r.setLayout( "default" );
294 r.setRefreshCronExpression( "* 0/5 * * * ?" );
296 r.setRetentionCount( 0 );
297 r.setReleases( true );
298 r.setSnapshots( true );
299 r.setScanned( false );
300 r.setDeleteReleasedSnapshots( false );
304 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
306 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
309 r.setLayout( "default" );
314 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
316 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
317 connector.setSourceRepoId( managedRepoId );
318 connector.setTargetRepoId( remoteRepoId );
323 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
325 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
326 repoGroup.setId( repoGroupId );
327 repoGroup.setRepositories( repoIds );
332 private void prepareRoleManagerMock()
333 throws RoleManagerException
335 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
336 roleManagerControl.setReturnValue( true );
337 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
338 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
339 roleManagerControl.setReturnValue( true );
340 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
341 roleManagerControl.replay();
344 protected ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
346 ArchivaProjectModel projectModel = new ArchivaProjectModel();
347 projectModel.setGroupId( groupId );
348 projectModel.setArtifactId( artifactId );
349 projectModel.setVersion( version );