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.commons.io.FileUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.codehaus.plexus.PlexusTestCase;
29 import org.codehaus.plexus.redback.role.RoleManager;
30 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
31 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
32 import org.codehaus.plexus.registry.RegistryException;
33 import org.easymock.MockControl;
36 import java.util.Collections;
39 * Test the repositories action returns the correct data.
41 public class ConfigureRepositoryActionTest
42 extends PlexusTestCase
44 private ConfigureRepositoryAction action;
46 private RoleManager roleManager;
48 private MockControl roleManagerControl;
50 private MockControl archivaConfigurationControl;
52 private ArchivaConfiguration archivaConfiguration;
54 private static final String REPO_ID = "repo-ident";
56 private File location;
58 protected void setUp()
63 // TODO: purely to quiet logging - shouldn't be needed
64 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
65 System.setProperty( "appserver.base", appserverBase );
66 action = (ConfigureRepositoryAction) lookup( Action.class.getName(), "configureRepositoryAction" );
68 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
70 action.setArchivaConfiguration( archivaConfiguration );
72 roleManagerControl = MockControl.createControl( RoleManager.class );
73 roleManager = (RoleManager) roleManagerControl.getMock();
74 action.setRoleManager( roleManager );
75 location = getTestFile( "location" );
78 public void testSecureActionBundle()
79 throws SecureActionException
81 archivaConfiguration.getConfiguration();
82 archivaConfigurationControl.setReturnValue( new Configuration() );
83 archivaConfigurationControl.replay();
86 SecureActionBundle bundle = action.getSecureActionBundle();
87 assertTrue( bundle.requiresAuthentication() );
88 assertEquals( 1, bundle.getAuthorizationTuples().size() );
91 public void testAddRepositoryInitialPage()
94 archivaConfiguration.getConfiguration();
95 archivaConfigurationControl.setReturnValue( new Configuration() );
96 archivaConfigurationControl.replay();
99 assertNull( action.getRepoid() );
100 assertNull( action.getMode() );
101 ManagedRepositoryConfiguration configuration = action.getRepository();
102 assertNotNull( configuration );
103 assertNull( configuration.getId() );
104 // check all booleans are false
105 assertFalse( configuration.isDeleteReleasedSnapshots() );
106 assertFalse( configuration.isIndexed() );
107 assertFalse( configuration.isReleases() );
108 assertFalse( configuration.isSnapshots() );
110 String status = action.add();
111 assertEquals( Action.INPUT, status );
114 assertFalse( configuration.isDeleteReleasedSnapshots() );
115 assertTrue( configuration.isIndexed() );
116 assertTrue( configuration.isReleases() );
117 assertFalse( configuration.isSnapshots() );
120 public void testAddRepository()
123 FileUtils.deleteDirectory( location );
125 // TODO: should be in the business model
126 roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
127 roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
129 roleManagerControl.replay();
131 Configuration configuration = new Configuration();
132 archivaConfiguration.getConfiguration();
133 archivaConfigurationControl.setReturnValue( configuration );
134 archivaConfiguration.getConfiguration();
135 archivaConfigurationControl.setReturnValue( configuration );
137 archivaConfiguration.save( configuration );
139 archivaConfigurationControl.replay();
142 action.setMode( "add" );
143 ManagedRepositoryConfiguration repository = action.getRepository();
144 populateRepository( repository );
146 assertFalse( location.exists() );
147 String status = action.save();
148 assertEquals( Action.SUCCESS, status );
149 assertTrue( location.exists() );
151 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
153 roleManagerControl.verify();
154 archivaConfigurationControl.verify();
157 public void testEditRepositoryInitialPage()
160 Configuration configuration = createConfigurationForEditing( createRepository() );
162 archivaConfiguration.getConfiguration();
163 archivaConfigurationControl.setReturnValue( configuration );
164 archivaConfigurationControl.replay();
166 action.setRepoid( REPO_ID );
169 assertEquals( REPO_ID, action.getRepoid() );
170 assertNull( action.getMode() );
171 ManagedRepositoryConfiguration repository = action.getRepository();
172 assertNotNull( repository );
173 assertRepositoryEquals( repository, createRepository() );
175 String status = action.edit();
176 assertEquals( Action.INPUT, status );
177 repository = action.getRepository();
178 assertRepositoryEquals( repository, createRepository() );
181 public void testEditRepository()
184 // TODO: should be in the business model
185 roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
186 roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
188 roleManagerControl.replay();
190 Configuration configuration = createConfigurationForEditing( createRepository() );
191 archivaConfiguration.getConfiguration();
192 archivaConfigurationControl.setReturnValue( configuration );
193 archivaConfiguration.getConfiguration();
194 archivaConfigurationControl.setReturnValue( configuration );
196 archivaConfiguration.save( configuration );
198 archivaConfigurationControl.replay();
201 action.setMode( "edit" );
202 ManagedRepositoryConfiguration repository = action.getRepository();
203 populateRepository( repository );
204 repository.setName( "new repo name" );
206 String status = action.save();
207 assertEquals( Action.SUCCESS, status );
209 ManagedRepositoryConfiguration newRepository = createRepository();
210 newRepository.setName( "new repo name" );
211 assertRepositoryEquals( repository, newRepository );
212 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
214 roleManagerControl.verify();
215 archivaConfigurationControl.verify();
218 public void testDeleteRepositoryConfirmation()
220 ManagedRepositoryConfiguration originalRepository = createRepository();
221 Configuration configuration = createConfigurationForEditing( originalRepository );
223 archivaConfiguration.getConfiguration();
224 archivaConfigurationControl.setReturnValue( configuration );
225 archivaConfigurationControl.replay();
227 action.setRepoid( REPO_ID );
230 assertEquals( REPO_ID, action.getRepoid() );
231 assertNull( action.getMode() );
232 ManagedRepositoryConfiguration repository = action.getRepository();
233 assertNotNull( repository );
234 assertRepositoryEquals( repository, createRepository() );
236 String status = action.confirm();
237 assertEquals( Action.INPUT, status );
238 repository = action.getRepository();
239 assertRepositoryEquals( repository, createRepository() );
240 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
243 public void testDeleteRepositoryKeepContent()
244 throws RegistryException, IndeterminateConfigurationException
246 Configuration configuration = executeDeletionTest( "delete-entry", createRepository() );
248 assertTrue( configuration.getManagedRepositories().isEmpty() );
250 assertTrue( location.exists() );
253 public void testDeleteRepositoryDeleteContent()
254 throws RegistryException, IndeterminateConfigurationException
256 Configuration configuration = executeDeletionTest( "delete-contents", createRepository() );
258 assertTrue( configuration.getManagedRepositories().isEmpty() );
260 assertFalse( location.exists() );
263 public void testDeleteRepositoryCancelled()
264 throws RegistryException, IndeterminateConfigurationException
266 ManagedRepositoryConfiguration originalRepository = createRepository();
267 Configuration configuration = executeDeletionTest( "unmodified", originalRepository );
269 ManagedRepositoryConfiguration repository = action.getRepository();
270 assertRepositoryEquals( repository, createRepository() );
271 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
273 assertTrue( location.exists() );
276 private Configuration executeDeletionTest( String mode, ManagedRepositoryConfiguration originalRepository )
277 throws RegistryException, IndeterminateConfigurationException
281 Configuration configuration = createConfigurationForEditing( originalRepository );
283 archivaConfiguration.getConfiguration();
284 archivaConfigurationControl.setReturnValue( configuration );
285 archivaConfiguration.getConfiguration();
286 archivaConfigurationControl.setReturnValue( configuration );
288 archivaConfiguration.save( configuration );
289 archivaConfigurationControl.replay();
291 action.setRepoid( REPO_ID );
292 action.setMode( mode );
295 assertEquals( REPO_ID, action.getRepoid() );
296 assertEquals( mode, action.getMode() );
297 ManagedRepositoryConfiguration repository = action.getRepository();
298 assertNotNull( repository );
299 assertRepositoryEquals( repository, createRepository() );
301 assertTrue( location.exists() );
302 String status = action.delete();
303 assertEquals( Action.SUCCESS, status );
304 return configuration;
307 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
308 ManagedRepositoryConfiguration actualRepository )
310 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
311 assertEquals( expectedRepository.getId(), actualRepository.getId() );
312 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
313 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
314 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
315 assertEquals( expectedRepository.getName(), actualRepository.getName() );
316 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
317 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
318 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
319 assertEquals( expectedRepository.isIndexed(), actualRepository.isIndexed() );
320 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
321 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
324 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
326 Configuration configuration = new Configuration();
327 configuration.addManagedRepository( repositoryConfiguration );
328 return configuration;
331 private ManagedRepositoryConfiguration createRepository()
333 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
335 populateRepository( r );
339 private void populateRepository( ManagedRepositoryConfiguration repository )
341 repository.setId( REPO_ID );
342 repository.setName( "repo name" );
343 repository.setLocation( location.getAbsolutePath() );
344 repository.setLayout( "default" );
345 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
346 repository.setDaysOlder( 31 );
347 repository.setRetentionCount( 20 );
348 repository.setReleases( true );
349 repository.setSnapshots( true );
350 repository.setIndexed( true );
351 repository.setDeleteReleasedSnapshots( true );
354 // TODO: test errors during add, other actions
355 // TODO: what if there are proxy connectors attached to a deleted repository?
356 // TODO: what about removing proxied content if a proxy is removed?