]> source.dussan.org Git - archiva.git/blob
c5e062932f9f2f5b593fa01abaee6e033fcbb7e1
[archiva.git] /
1 package org.apache.archiva.web.action.admin.repositories;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import com.opensymphony.xwork2.Action;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
26 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
27 import org.apache.archiva.audit.AuditListener;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.Configuration;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.metadata.repository.MetadataRepository;
32 import org.apache.archiva.metadata.repository.RepositorySession;
33 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
36 import org.apache.archiva.redback.role.RoleManager;
37 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
38 import org.apache.archiva.scheduler.repository.RepositoryTask;
39 import org.apache.archiva.security.common.ArchivaRoleConstants;
40 import org.apache.archiva.web.validator.utils.ValidatorUtil;
41 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
42 import org.apache.commons.io.FileUtils;
43 import org.easymock.MockControl;
44 import org.easymock.classextension.MockClassControl;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
55
56 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60
61 /**
62  * EditManagedRepositoryActionTest
63  *
64  *
65  */
66 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
67 public class EditManagedRepositoryActionTest
68     extends AbstractManagedRepositoryActionTest
69 {
70     private EditManagedRepositoryAction action;
71
72     private RoleManager roleManager;
73
74     private MockControl roleManagerControl;
75
76     private MockControl archivaConfigurationControl;
77
78     private ArchivaConfiguration archivaConfiguration;
79
80     private org.apache.archiva.redback.components.registry.Registry registry;
81
82     private MockControl registryControl;
83
84     private MetadataRepository metadataRepository;
85
86     private MockControl repositoryTaskSchedulerControl;
87
88     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
89
90     @Override
91     @Before
92     public void setUp()
93         throws Exception
94     {
95         super.setUp();
96
97         action = new EditManagedRepositoryAction();
98
99         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
100         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
101
102         roleManagerControl = MockControl.createControl( RoleManager.class );
103         roleManager = (RoleManager) roleManagerControl.getMock();
104
105         registryControl = MockControl.createControl( org.apache.archiva.redback.components.registry.Registry.class );
106         registry = (org.apache.archiva.redback.components.registry.Registry) registryControl.getMock();
107
108         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
109         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
110
111         location = new File( System.getProperty( "basedir", "target/test/location" ) );
112
113         metadataRepository = mock( MetadataRepository.class );
114         RepositorySession repositorySession = mock( RepositorySession.class );
115         when( repositorySession.getRepository() ).thenReturn( metadataRepository );
116         TestRepositorySessionFactory factory = applicationContext.getBean( TestRepositorySessionFactory.class );
117         factory.setRepositorySession( repositorySession );
118
119         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
120         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
121         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
122             repositoryTaskScheduler );
123         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositorySessionFactory( factory );
124
125         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
126
127         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners(
128             new ArrayList<AuditListener>( 0 ) );
129
130         DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
131         defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
132         defaultRepositoryCommonValidator.setRegistry( registry );
133
134         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
135             defaultRepositoryCommonValidator );
136
137         action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
138
139         action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
140
141     }
142
143     @Test
144     public void testSecureActionBundle()
145         throws SecureActionException, RepositoryAdminException
146     {
147         archivaConfiguration.getConfiguration();
148         archivaConfigurationControl.setReturnValue( new Configuration() );
149         archivaConfigurationControl.replay();
150
151         action.prepare();
152         SecureActionBundle bundle = action.getSecureActionBundle();
153         assertTrue( bundle.requiresAuthentication() );
154         assertEquals( 1, bundle.getAuthorizationTuples().size() );
155     }
156
157     @Test
158     public void testEditRepositoryInitialPage()
159         throws Exception
160     {
161         Configuration configuration = createConfigurationForEditing( createRepository() );
162
163         archivaConfiguration.getConfiguration();
164         archivaConfigurationControl.setReturnValue( configuration );
165         Configuration stageRepoConfiguration = new Configuration();
166         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
167         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
168
169         archivaConfigurationControl.replay();
170
171         action.setRepoid( REPO_ID );
172
173         action.prepare();
174         assertEquals( REPO_ID, action.getRepoid() );
175         ManagedRepository repository = action.getRepository();
176         assertNotNull( repository );
177
178         ManagedRepository newRepository = createRepository();
179         assertRepositoryEquals( repository, newRepository );
180         assertEquals( repository.getLocation(), newRepository.getLocation() );
181
182         String status = action.input();
183         assertEquals( Action.INPUT, status );
184         repository = action.getRepository();
185         assertRepositoryEquals( repository, createRepository() );
186     }
187
188     @Test
189     public void testEditRepository()
190         throws Exception
191     {
192         String stageRepoId = REPO_ID + "-stage";
193
194         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
195         roleManagerControl.setReturnValue( false );
196         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
197         roleManagerControl.setVoidCallable();
198
199         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
200         roleManagerControl.setReturnValue( false );
201         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
202         roleManagerControl.setVoidCallable();
203
204         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, stageRepoId );
205         roleManagerControl.setReturnValue( false );
206         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, stageRepoId );
207         roleManagerControl.setVoidCallable();
208
209         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, stageRepoId );
210         roleManagerControl.setReturnValue( false );
211         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, stageRepoId );
212         roleManagerControl.setVoidCallable();
213
214         roleManagerControl.replay();
215
216         registry.getString( "appserver.base", "${appserver.base}" );
217         registryControl.setReturnValue( "target/test", 1, 3 );
218         registry.getString( "appserver.home", "${appserver.home}" );
219         registryControl.setReturnValue( "target/test", 1, 3 );
220
221         registryControl.replay();
222
223         RepositoryTask task = new RepositoryTask();
224         task.setRepositoryId( REPO_ID );
225         repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
226         repositoryTaskSchedulerControl.setReturnValue( false );
227         repositoryTaskScheduler.queueTask( task );
228         repositoryTaskSchedulerControl.setVoidCallable();
229
230         RepositoryTask stageTask = new RepositoryTask();
231         stageTask.setRepositoryId( stageRepoId );
232         repositoryTaskScheduler.isProcessingRepositoryTask( stageRepoId );
233         repositoryTaskSchedulerControl.setReturnValue( false );
234         repositoryTaskScheduler.queueTask( stageTask );
235         repositoryTaskSchedulerControl.setVoidCallable();
236
237         repositoryTaskSchedulerControl.replay();
238
239         Configuration configuration = createConfigurationForEditing( createRepository() );
240         archivaConfiguration.getConfiguration();
241         archivaConfigurationControl.setReturnValue( configuration );
242         archivaConfigurationControl.setReturnValue( configuration );
243         archivaConfigurationControl.setReturnValue( configuration );
244
245         archivaConfigurationControl.setReturnValue( configuration );
246         archivaConfigurationControl.setReturnValue( configuration );
247
248         Configuration stageRepoConfiguration = new Configuration();
249         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
250         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
251         archivaConfigurationControl.setReturnValue( configuration );
252         archivaConfigurationControl.setReturnValue( configuration );
253
254         archivaConfiguration.save( configuration );
255         archivaConfiguration.save( configuration );
256
257         archivaConfiguration.save( configuration );
258
259         archivaConfigurationControl.replay();
260
261         action.setRepoid( REPO_ID );
262         action.prepare();
263         assertEquals( REPO_ID, action.getRepoid() );
264         ManagedRepository repository = action.getRepository();
265         populateRepository( repository );
266         repository.setName( "new repo name" );
267
268         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
269         RepositoryStatisticsManager repositoryStatisticsManager =
270             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
271         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
272             repositoryStatisticsManager );
273         repositoryStatisticsManager.deleteStatistics( metadataRepository , REPO_ID );
274         repositoryStatisticsManagerControl.setVoidCallable();
275         repositoryStatisticsManagerControl.replay();
276
277         new File( "target/test/" + REPO_ID + "-stage" ).mkdirs();
278
279         repository.setLocation( System.getProperty( "basedir" ) + "/target/test/" + REPO_ID );
280
281         action.setRepository( repository );
282         action.setStageNeeded( true );
283         String status = action.commit();
284         assertEquals( Action.SUCCESS, status );
285
286         ManagedRepository newRepository = createRepository();
287         newRepository.setName( "new repo name" );
288         assertRepositoryEquals( repository, newRepository );
289         //assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
290         //assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
291
292         roleManagerControl.verify();
293         //archivaConfigurationControl.verify();
294         repositoryStatisticsManagerControl.verify();
295         registryControl.verify();
296     }
297
298     @Test
299     public void testEditRepositoryLocationChanged()
300         throws Exception
301     {
302         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
303         roleManagerControl.setReturnValue( false );
304         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
305         roleManagerControl.setVoidCallable();
306
307         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
308         roleManagerControl.setReturnValue( false );
309         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
310         roleManagerControl.setVoidCallable();
311
312         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID + "-stage" );
313         roleManagerControl.setReturnValue( false );
314         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID + "-stage" );
315         roleManagerControl.setVoidCallable();
316
317         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID + "-stage" );
318         roleManagerControl.setReturnValue( false );
319         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID + "-stage" );
320         roleManagerControl.setVoidCallable();
321
322         roleManagerControl.replay();
323
324         registry.getString( "appserver.base" );
325         registryControl.setReturnValue( "target/test", 1, 3 );
326
327         registry.getString( "appserver.base", "${appserver.base}" );
328         registryControl.setReturnValue( "target/test", 1, 3 );
329         registry.getString( "appserver.home", "${appserver.home}" );
330         registryControl.setReturnValue( "target/test", 1, 3 );
331
332         registryControl.replay();
333
334         RepositoryTask task = new RepositoryTask();
335         task.setRepositoryId( REPO_ID );
336         repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
337         repositoryTaskSchedulerControl.setReturnValue( false );
338         repositoryTaskScheduler.queueTask( task );
339         repositoryTaskSchedulerControl.setVoidCallable();
340
341         repositoryTaskSchedulerControl.replay();
342
343         Configuration configuration = createConfigurationForEditing( createRepository() );
344         archivaConfiguration.getConfiguration();
345
346         archivaConfigurationControl.setReturnValue( configuration );
347         archivaConfigurationControl.setReturnValue( configuration );
348         archivaConfigurationControl.setReturnValue( configuration );
349         archivaConfigurationControl.setReturnValue( configuration );
350
351         archivaConfigurationControl.setReturnValue( configuration );
352
353         archivaConfigurationControl.setReturnValue( buildEasyConfiguration() );
354
355         Configuration stageRepoConfiguration = buildEasyConfiguration();
356         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
357         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
358
359         archivaConfigurationControl.setReturnValue( configuration );
360         archivaConfigurationControl.setReturnValue( configuration );
361
362         archivaConfiguration.save( configuration );
363         configuration.addManagedRepository( stageRepoConfiguration.getManagedRepositories().get( 0 ) );
364         archivaConfiguration.save( configuration );
365         archivaConfiguration.save( configuration );
366
367         archivaConfigurationControl.replay();
368
369         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
370         RepositoryStatisticsManager repositoryStatisticsManager =
371             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
372         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
373             repositoryStatisticsManager );
374         repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
375         repositoryStatisticsManagerControl.replay();
376
377         new File( "target/test/location/" + REPO_ID + "-stage" ).mkdirs();
378
379         action.setStageNeeded( true );
380         action.setRepoid( REPO_ID );
381         action.prepare();
382         assertEquals( REPO_ID, action.getRepoid() );
383
384         ManagedRepository repository = new ManagedRepository();
385         populateRepository( repository );
386         File testFile = new File( "target/test/location/new" );
387         FileUtils.deleteDirectory( testFile );
388         repository.setLocation( "${appserver.base}/location/new" );
389         action.setRepository( repository );
390         String status = action.commit();
391         assertEquals( Action.SUCCESS, status );
392         //assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
393         //assertEquals( testFile.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
394
395         roleManagerControl.verify();
396         //archivaConfigurationControl.verify();
397         repositoryStatisticsManagerControl.verify();
398         registryControl.verify();
399     }
400
401     @Test
402     public void testStruts2ValidationFrameworkWithNullInputs()
403         throws Exception
404     {
405         // prep
406         // 0 is the default value for primitive int; null for objects
407         ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null, 1, 1 );
408         action.setRepository( managedRepositoryConfiguration );
409
410         // test
411         actionValidatorManager.validate( action, EMPTY_STRING );
412
413         // verify
414         assertTrue( action.hasFieldErrors() );
415
416         Map<String, List<String>> fieldErrors = action.getFieldErrors();
417
418         // make an expected field error object
419         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
420
421         // populate
422         List<String> expectedErrorMessages = new ArrayList<String>();
423         expectedErrorMessages.add( "You must enter a repository identifier." );
424         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
425
426         expectedErrorMessages = new ArrayList<String>();
427         expectedErrorMessages.add( "You must enter a directory." );
428         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
429
430         expectedErrorMessages = new ArrayList<String>();
431         expectedErrorMessages.add( "You must enter a repository name." );
432         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
433
434         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
435     }
436
437     @Test
438     public void testStruts2ValidationFrameworkWithBlankInputs()
439         throws Exception
440     {
441         // prep
442         // 0 is the default value for primitive int
443         ManagedRepository managedRepositoryConfiguration =
444             createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, 1, 1 );
445         action.setRepository( managedRepositoryConfiguration );
446
447         // test
448         actionValidatorManager.validate( action, EMPTY_STRING );
449
450         // verify
451         assertTrue( action.hasFieldErrors() );
452
453         Map<String, List<String>> fieldErrors = action.getFieldErrors();
454
455         // make an expected field error object
456         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
457
458         // populate
459         List<String> expectedErrorMessages = new ArrayList<String>();
460         expectedErrorMessages.add( "You must enter a repository identifier." );
461         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
462
463         expectedErrorMessages = new ArrayList<String>();
464         expectedErrorMessages.add( "You must enter a directory." );
465         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
466
467         expectedErrorMessages = new ArrayList<String>();
468         expectedErrorMessages.add( "You must enter a repository name." );
469         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
470
471         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
472     }
473
474     @Test
475     public void testStruts2ValidationFrameworkWithInvalidInputs()
476         throws Exception
477     {
478         // prep
479         ManagedRepository managedRepositoryConfiguration =
480             createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
481                                      REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
482                                      REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
483         action.setRepository( managedRepositoryConfiguration );
484
485         // test
486         actionValidatorManager.validate( action, EMPTY_STRING );
487
488         // verify
489         assertTrue( action.hasFieldErrors() );
490
491         Map<String, List<String>> fieldErrors = action.getFieldErrors();
492
493         // make an expected field error object
494         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
495
496         // populate
497         List<String> expectedErrorMessages = new ArrayList<String>();
498         expectedErrorMessages.add(
499             "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
500         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
501
502         expectedErrorMessages = new ArrayList<String>();
503         expectedErrorMessages.add(
504             "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
505         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
506
507         expectedErrorMessages = new ArrayList<String>();
508         expectedErrorMessages.add(
509             "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'),  underscores(_), dots(.), and dashes(-)." );
510         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
511
512         expectedErrorMessages = new ArrayList<String>();
513         expectedErrorMessages.add(
514             "Index directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
515         expectedFieldErrors.put( "repository.indexDirectory", expectedErrorMessages );
516
517         expectedErrorMessages = new ArrayList<String>();
518         expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
519         expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
520
521         expectedErrorMessages = new ArrayList<String>();
522         expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
523         expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
524
525         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
526     }
527
528     @Test
529     public void testStruts2ValidationFrameworkWithValidInputs()
530         throws Exception
531     {
532         // prep
533         ManagedRepository managedRepositoryConfiguration =
534             createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
535                                      REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
536                                      REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
537         action.setRepository( managedRepositoryConfiguration );
538
539         // test
540         actionValidatorManager.validate( action, EMPTY_STRING );
541
542         // verify
543         assertFalse( action.hasFieldErrors() );
544     }
545
546     private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
547     {
548         assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
549         assertEquals( expectedRepository.getId(), actualRepository.getId() );
550         assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
551         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
552         assertEquals( expectedRepository.getName(), actualRepository.getName() );
553         assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
554         assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
555         assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
556         assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
557         assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
558         assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
559     }
560
561     private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
562         throws Exception
563     {
564         Configuration configuration = buildEasyConfiguration();
565
566         ManagedRepositoryConfiguration managedRepositoryConfiguration = new ManagedRepositoryConfiguration();
567
568         managedRepositoryConfiguration.setDaysOlder( repositoryConfiguration.getDaysOlder() );
569         managedRepositoryConfiguration.setIndexDir( repositoryConfiguration.getIndexDirectory() );
570         managedRepositoryConfiguration.setRetentionCount( repositoryConfiguration.getRetentionCount() );
571         managedRepositoryConfiguration.setBlockRedeployments( repositoryConfiguration.isBlockRedeployments() );
572         managedRepositoryConfiguration.setDeleteReleasedSnapshots(
573             repositoryConfiguration.isDeleteReleasedSnapshots() );
574         managedRepositoryConfiguration.setLocation( repositoryConfiguration.getLocation() );
575         managedRepositoryConfiguration.setRefreshCronExpression( repositoryConfiguration.getCronExpression() );
576         managedRepositoryConfiguration.setReleases( repositoryConfiguration.isReleases() );
577         managedRepositoryConfiguration.setScanned( repositoryConfiguration.isScanned() );
578         managedRepositoryConfiguration.setId( repositoryConfiguration.getId() );
579         managedRepositoryConfiguration.setName( repositoryConfiguration.getName() );
580         managedRepositoryConfiguration.setLayout( repositoryConfiguration.getLayout() );
581
582         configuration.addManagedRepository( managedRepositoryConfiguration );
583         return configuration;
584     }
585
586     // easy configuration for hashCode/equals
587     private Configuration buildEasyConfiguration()
588     {
589         return new Configuration()
590         {
591             @Override
592             public int hashCode()
593             {
594                 return getManagedRepositories().size();
595             }
596
597             @Override
598             public boolean equals( Object o )
599             {
600                 return true;
601             }
602         };
603     }
604
605     private ManagedRepository createRepository()
606         throws IOException
607     {
608         ManagedRepository r = new ManagedRepository();
609         r.setLocation( System.getProperty( "basedir" ) + "/target/repo-" + REPO_ID );
610         r.setIndexDirectory( System.getProperty( "basedir" ) + "/target/repo-" + REPO_ID + "-index" );
611         r.setId( REPO_ID );
612         populateRepository( r );
613         return r;
614     }
615
616     private ManagedRepositoryConfiguration createStagingRepository()
617         throws IOException
618     {
619         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
620         r.setId( REPO_ID + "-stage" );
621         r.setLocation( System.getProperty( "basedir" ) + "/target/" + REPO_ID + "-stage" );
622         populateStagingRepository( r );
623         return r;
624     }
625
626     private void populateStagingRepository( ManagedRepositoryConfiguration repository )
627         throws IOException
628     {
629         repository.setId( REPO_ID + "-stage" );
630         repository.setName( "repo name" );
631         repository.setLocation( "${appserver.base}/location" );
632         repository.setLayout( "default" );
633         repository.setRefreshCronExpression( "* 0/5 * * * ?" );
634         repository.setDaysOlder( 31 );
635         repository.setRetentionCount( 20 );
636         repository.setReleases( true );
637         repository.setSnapshots( true );
638         repository.setScanned( false );
639         repository.setDeleteReleasedSnapshots( true );
640     }
641 }