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.model.ArchivaProjectModel;
30 import org.apache.maven.archiva.security.ArchivaRoleConstants;
31 import org.codehaus.plexus.redback.role.RoleManager;
32 import org.codehaus.plexus.redback.role.RoleManagerException;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
35 import org.codehaus.plexus.registry.RegistryException;
36 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
37 import org.easymock.MockControl;
40 import java.util.Collections;
43 * DeleteManagedRepositoryActionTest
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48 public class DeleteManagedRepositoryActionTest
49 extends PlexusInSpringTestCase
51 private DeleteManagedRepositoryAction action;
53 private RoleManager roleManager;
55 private MockControl roleManagerControl;
57 private MockControl archivaConfigurationControl;
59 private ArchivaConfiguration archivaConfiguration;
61 private static final String REPO_ID = "repo-ident";
63 private File location;
66 protected String getPlexusConfigLocation()
68 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
71 protected void setUp()
76 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
78 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
79 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
80 action.setArchivaConfiguration( archivaConfiguration );
82 roleManagerControl = MockControl.createControl( RoleManager.class );
83 roleManager = (RoleManager) roleManagerControl.getMock();
84 action.setRoleManager( roleManager );
85 location = getTestFile( "target/test/location" );
88 public void testSecureActionBundle()
89 throws SecureActionException
91 archivaConfiguration.getConfiguration();
92 archivaConfigurationControl.setReturnValue( new Configuration() );
93 archivaConfigurationControl.replay();
96 SecureActionBundle bundle = action.getSecureActionBundle();
97 assertTrue( bundle.requiresAuthentication() );
98 assertEquals( 1, bundle.getAuthorizationTuples().size() );
101 public void testDeleteRepositoryConfirmation()
104 ManagedRepositoryConfiguration originalRepository = createRepository();
105 Configuration configuration = createConfigurationForEditing( originalRepository );
107 archivaConfiguration.getConfiguration();
108 archivaConfigurationControl.setReturnValue( configuration );
109 archivaConfigurationControl.replay();
111 action.setRepoid( REPO_ID );
114 assertEquals( REPO_ID, action.getRepoid() );
115 ManagedRepositoryConfiguration repository = action.getRepository();
116 assertNotNull( repository );
117 assertRepositoryEquals( repository, createRepository() );
119 String status = action.execute();
120 assertEquals( Action.SUCCESS, status );
121 repository = action.getRepository();
122 assertRepositoryEquals( repository, createRepository() );
123 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
126 public void testDeleteRepositoryKeepContent()
129 prepareRoleManagerMock();
131 Configuration configuration = prepDeletionTest( createRepository(), 3 );
133 String status = action.deleteEntry();
135 assertEquals( Action.SUCCESS, status );
137 assertTrue( configuration.getManagedRepositories().isEmpty() );
139 assertTrue( location.exists() );
142 public void testDeleteRepositoryDeleteContent()
145 prepareRoleManagerMock();
147 Configuration configuration = prepDeletionTest( createRepository(), 3 );
149 String status = action.deleteContents();
151 assertEquals( Action.SUCCESS, status );
153 assertTrue( configuration.getManagedRepositories().isEmpty() );
155 assertFalse( location.exists() );
158 public void testDeleteRepositoryAndAssociatedProxyConnectors()
161 Configuration configuration = prepDeletionTest( createRepository(), 4 );
162 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
163 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
164 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
166 prepareRoleManagerMock();
168 assertEquals( 1, configuration.getProxyConnectors().size() );
170 String status = action.deleteContents();
171 assertEquals( Action.SUCCESS, status );
173 assertTrue( configuration.getManagedRepositories().isEmpty() );
174 assertEquals( 0, configuration.getProxyConnectors().size() );
176 assertFalse( location.exists() );
179 public void testDeleteRepositoryCancelled()
182 ManagedRepositoryConfiguration originalRepository = createRepository();
183 Configuration configuration = prepDeletionTest( originalRepository, 3 );
184 String status = action.execute();
185 assertEquals( Action.SUCCESS, status );
187 ManagedRepositoryConfiguration repository = action.getRepository();
188 assertRepositoryEquals( repository, createRepository() );
189 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
191 assertTrue( location.exists() );
194 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
195 throws RegistryException, IndeterminateConfigurationException
199 Configuration configuration = createConfigurationForEditing( originalRepository );
201 archivaConfiguration.getConfiguration();
202 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
204 archivaConfiguration.save( configuration );
205 archivaConfigurationControl.replay();
207 action.setRepoid( REPO_ID );
210 assertEquals( REPO_ID, action.getRepoid() );
211 ManagedRepositoryConfiguration repository = action.getRepository();
212 assertNotNull( repository );
213 assertRepositoryEquals( repository, createRepository() );
215 assertTrue( location.exists() );
216 return configuration;
219 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
220 ManagedRepositoryConfiguration actualRepository )
222 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
223 assertEquals( expectedRepository.getId(), actualRepository.getId() );
224 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
225 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
226 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
227 assertEquals( expectedRepository.getName(), actualRepository.getName() );
228 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
229 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
230 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
231 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
232 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
233 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
236 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
238 Configuration configuration = new Configuration();
239 configuration.addManagedRepository( repositoryConfiguration );
240 return configuration;
243 private ManagedRepositoryConfiguration createRepository()
245 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
247 r.setName( "repo name" );
248 r.setLocation( location.getAbsolutePath() );
249 r.setLayout( "default" );
250 r.setRefreshCronExpression( "* 0/5 * * * ?" );
252 r.setRetentionCount( 0 );
253 r.setReleases( true );
254 r.setSnapshots( true );
255 r.setScanned( false );
256 r.setDeleteReleasedSnapshots( false );
260 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
262 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
265 r.setLayout( "default" );
270 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
272 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
273 connector.setSourceRepoId( managedRepoId );
274 connector.setTargetRepoId( remoteRepoId );
279 private void prepareRoleManagerMock()
280 throws RoleManagerException
282 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
283 roleManagerControl.setReturnValue( true );
284 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
285 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
286 roleManagerControl.setReturnValue( true );
287 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
288 roleManagerControl.replay();
291 protected ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
293 ArchivaProjectModel projectModel = new ArchivaProjectModel();
294 projectModel.setGroupId( groupId );
295 projectModel.setArtifactId( artifactId );
296 projectModel.setVersion( version );