]> source.dussan.org Git - archiva.git/blob
55504341f570c4ad9b2c2da20df8f8a1ed81b638
[archiva.git] /
1 package org.apache.archiva.web.xmlrpc.services;
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 java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
29 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
30 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.io.filefilter.FileFilterUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.Configuration;
35 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
36 import org.apache.maven.archiva.configuration.FileTypes;
37 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
38 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
39 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
40 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
41 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
42 import org.apache.maven.archiva.database.ArtifactDAO;
43 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
44 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
45 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
46 import org.apache.maven.archiva.model.ArchivaArtifact;
47 import org.apache.maven.archiva.model.ArchivaArtifactModel;
48 import org.apache.maven.archiva.model.ArtifactReference;
49 import org.apache.maven.archiva.repository.RepositoryContentFactory;
50 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
51 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
52 import org.apache.maven.archiva.repository.content.PathParser;
53 import org.apache.maven.archiva.repository.events.RepositoryListener;
54 import org.apache.maven.archiva.repository.layout.LayoutException;
55 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
56 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
57 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
58 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
59 import org.easymock.MockControl;
60 import org.easymock.classextension.MockClassControl;
61
62 /**
63  * AdministrationServiceImplTest
64  * 
65  * @version $Id: AdministrationServiceImplTest.java
66  */
67 public class AdministrationServiceImplTest
68     extends PlexusInSpringTestCase
69 {    
70     private MockControl archivaConfigControl;
71     
72     private ArchivaConfiguration archivaConfig;
73     
74     private MockControl configControl;
75     
76     private Configuration config;
77     
78     private AdministrationServiceImpl service;
79     
80     private MockControl taskSchedulerControl;
81     
82     private ArchivaTaskScheduler taskScheduler;
83     
84     // repository consumers
85     private MockControl repoConsumerUtilsControl;
86     
87     private RepositoryContentConsumers repoConsumersUtil;
88     
89     private MockControl knownContentConsumerControl;
90
91     private MockControl invalidContentConsumerControl;
92
93     private KnownRepositoryContentConsumer indexArtifactConsumer;
94
95     private KnownRepositoryContentConsumer indexPomConsumer;
96
97     private InvalidRepositoryContentConsumer checkPomConsumer;
98
99     private InvalidRepositoryContentConsumer checkMetadataConsumer;
100     
101     // database consumers
102     private MockControl dbConsumersUtilControl;
103     
104     private DatabaseConsumers dbConsumersUtil;
105     
106     private MockControl unprocessedConsumersControl;
107     
108     private DatabaseUnprocessedArtifactConsumer processArtifactConsumer;
109     
110     private DatabaseUnprocessedArtifactConsumer processPomConsumer;
111     
112     // delete artifact    
113     private MockControl repoFactoryControl;
114     
115     private RepositoryContentFactory repositoryFactory;
116     
117     private MockControl artifactDaoControl;
118     
119     private ArtifactDAO artifactDao;
120     
121     private MockControl listenerControl;
122
123     private RepositoryListener listener;
124
125     private DatabaseCleanupConsumer cleanupIndexConsumer;
126
127     private DatabaseCleanupConsumer cleanupDbConsumer;
128
129     private MockControl cleanupConsumersControl;
130         
131     protected void setUp()
132         throws Exception
133     {
134         super.setUp();
135         
136         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
137         archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
138         
139         configControl = MockClassControl.createControl( Configuration.class );
140         config = ( Configuration ) configControl.getMock();      
141         
142         taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
143         taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock();
144         
145         // repo consumers
146         repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class );
147         repoConsumersUtil = ( RepositoryContentConsumers ) repoConsumerUtilsControl.getMock();
148         
149         knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
150         indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
151         indexPomConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
152         
153         invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
154         checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
155         checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
156         
157         // db consumers
158         dbConsumersUtilControl = MockClassControl.createControl( DatabaseConsumers.class );
159         dbConsumersUtil = ( DatabaseConsumers ) dbConsumersUtilControl.getMock();
160                 
161         cleanupConsumersControl = MockControl.createControl( DatabaseCleanupConsumer.class );
162         cleanupIndexConsumer = (DatabaseCleanupConsumer) cleanupConsumersControl.getMock();
163         cleanupDbConsumer = (DatabaseCleanupConsumer) cleanupConsumersControl.getMock();
164                 
165         unprocessedConsumersControl = MockControl.createControl( DatabaseUnprocessedArtifactConsumer.class );
166         processArtifactConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
167         processPomConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
168         
169         // delete artifact
170         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
171         repositoryFactory = ( RepositoryContentFactory ) repoFactoryControl.getMock();
172         
173         artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
174         artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
175                 
176         listenerControl = MockControl.createControl( RepositoryListener.class );
177         listener = (RepositoryListener) listenerControl.getMock();
178         
179         service =
180             new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, dbConsumersUtil, repositoryFactory,
181                                            artifactDao, taskScheduler, Collections.singletonList( listener ) );
182     }
183   
184 /* Tests for database consumers  */
185     
186     public void testGetAllDbConsumers()
187         throws Exception
188     {   
189         recordDbConsumers();
190         
191         dbConsumersUtilControl.replay();
192         cleanupConsumersControl.replay();
193         unprocessedConsumersControl.replay();
194         
195         List<String> dbConsumers = service.getAllDatabaseConsumers();
196         
197         dbConsumersUtilControl.verify();
198         cleanupConsumersControl.verify();
199         unprocessedConsumersControl.verify();
200         
201         assertNotNull( dbConsumers );
202         assertEquals( 4, dbConsumers.size() );
203         assertTrue( dbConsumers.contains( "cleanup-index" ) );
204         assertTrue( dbConsumers.contains( "cleanup-database" ) );
205         assertTrue( dbConsumers.contains( "process-artifact" ) );
206         assertTrue( dbConsumers.contains( "process-pom" ) );
207     }
208     
209     public void testConfigureValidDatabaseConsumer()
210         throws Exception
211     {
212         DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
213         dbScanning.addCleanupConsumer( "cleanup-index" );
214         dbScanning.addCleanupConsumer( "cleanup-database" );
215         dbScanning.addUnprocessedConsumer( "process-artifact" );
216         
217         recordDbConsumers();
218         
219         // test enable "process-pom" db consumer
220         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
221         configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
222         
223         config.setDatabaseScanning( dbScanning );
224         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
225         configControl.setVoidCallable();
226         
227         archivaConfig.save( config );
228         archivaConfigControl.setVoidCallable();
229         
230         dbConsumersUtilControl.replay();
231         cleanupConsumersControl.replay();
232         unprocessedConsumersControl.replay();
233         archivaConfigControl.replay();
234         configControl.replay();
235         
236         try
237         {
238             boolean success = service.configureDatabaseConsumer( "process-pom", true );
239             assertTrue( success );
240         }
241         catch ( Exception e )
242         {
243             fail( "An exception should not have been thrown." );
244         }
245         
246         dbConsumersUtilControl.verify();
247         cleanupConsumersControl.verify();
248         unprocessedConsumersControl.verify();
249         archivaConfigControl.verify();
250         configControl.verify();
251                 
252         // test disable "process-pom" db consumer        
253         dbConsumersUtilControl.reset();
254         cleanupConsumersControl.reset();
255         unprocessedConsumersControl.reset();
256         archivaConfigControl.reset();
257         configControl.reset();
258                 
259         dbScanning.addUnprocessedConsumer( "process-pom" );
260         
261         recordDbConsumers();
262         
263         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
264         configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
265         
266         config.setDatabaseScanning( dbScanning );
267         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
268         configControl.setVoidCallable();
269         
270         archivaConfig.save( config );
271         archivaConfigControl.setVoidCallable();
272         
273         dbConsumersUtilControl.replay();
274         cleanupConsumersControl.replay();
275         unprocessedConsumersControl.replay();
276         archivaConfigControl.replay();
277         configControl.replay();
278         
279         try
280         {
281             boolean success = service.configureDatabaseConsumer( "process-pom", false );
282             assertTrue( success );
283         }
284         catch ( Exception e )
285         {
286             fail( "An exception should not have been thrown." );
287         }
288         
289         dbConsumersUtilControl.verify();
290         cleanupConsumersControl.verify();
291         unprocessedConsumersControl.verify();
292         archivaConfigControl.verify();
293         configControl.verify();
294     }
295     
296     public void testConfigureInvalidDatabaseConsumer()
297         throws Exception
298     {
299         recordDbConsumers();
300         
301         dbConsumersUtilControl.replay();
302         cleanupConsumersControl.replay();
303         unprocessedConsumersControl.replay();
304         
305         try
306         {
307             service.configureDatabaseConsumer( "invalid-consumer", true );
308             fail( "An exception should have been thrown." );
309         }
310         catch ( Exception e )
311         {
312             assertEquals( "Invalid database consumer.", e.getMessage() );
313         }
314         
315         dbConsumersUtilControl.verify();
316         cleanupConsumersControl.verify();
317         unprocessedConsumersControl.verify();
318     }
319         
320 /* Tests for repository consumers  */
321     
322     public void testGetAllRepoConsumers()
323         throws Exception
324     {   
325         recordRepoConsumers();
326         
327         repoConsumerUtilsControl.replay();
328         knownContentConsumerControl.replay();
329         invalidContentConsumerControl.replay();
330                 
331         List<String> repoConsumers = service.getAllRepositoryConsumers(); 
332         
333         repoConsumerUtilsControl.verify();
334         knownContentConsumerControl.verify();
335         invalidContentConsumerControl.verify();
336                         
337         assertNotNull( repoConsumers );
338         assertEquals( 4, repoConsumers.size() );
339         assertTrue( repoConsumers.contains( "index-artifact" ) );
340         assertTrue( repoConsumers.contains( "index-pom" ) );
341         assertTrue( repoConsumers.contains( "check-pom" ) );
342         assertTrue( repoConsumers.contains( "check-metadata" ) );
343     }
344     
345     public void testConfigureValidRepositoryConsumer()
346         throws Exception
347     {   
348         RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
349         repoScanning.addKnownContentConsumer( "index-artifact" );
350         repoScanning.addKnownContentConsumer( "index-pom" );
351         repoScanning.addInvalidContentConsumer( "check-pom" );        
352         
353      // test enable "check-metadata" consumer
354         recordRepoConsumers();
355         
356         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
357         configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
358         
359         config.setRepositoryScanning( repoScanning );                
360         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
361         configControl.setVoidCallable();
362         
363         archivaConfig.save( config );
364         archivaConfigControl.setVoidCallable();
365                 
366         repoConsumerUtilsControl.replay();
367         knownContentConsumerControl.replay();
368         invalidContentConsumerControl.replay();
369         archivaConfigControl.replay();
370         configControl.replay();        
371         
372         try
373         {
374             boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
375             assertTrue( success );
376         }
377         catch ( Exception e )
378         {
379             fail( "An exception should not have been thrown." );
380         }
381         
382         repoConsumerUtilsControl.verify();
383         knownContentConsumerControl.verify();
384         invalidContentConsumerControl.verify();        
385         archivaConfigControl.verify();
386         configControl.verify();
387                 
388      // test disable "check-metadata" consumer 
389         repoConsumerUtilsControl.reset();
390         knownContentConsumerControl.reset();
391         invalidContentConsumerControl.reset();        
392         archivaConfigControl.reset();
393         configControl.reset();
394         
395         repoScanning.addInvalidContentConsumer( "check-metadata" );
396
397         recordRepoConsumers();
398         
399         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
400         configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
401         
402         config.setRepositoryScanning( repoScanning );
403         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
404         configControl.setVoidCallable();
405         
406         archivaConfig.save( config );
407         archivaConfigControl.setVoidCallable();
408                 
409         repoConsumerUtilsControl.replay();
410         knownContentConsumerControl.replay();
411         invalidContentConsumerControl.replay();
412         archivaConfigControl.replay();
413         configControl.replay();
414         
415         try
416         {
417             boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
418             
419             repoConsumerUtilsControl.verify();
420             knownContentConsumerControl.verify();
421             invalidContentConsumerControl.verify();        
422             archivaConfigControl.verify();
423             configControl.verify();
424             
425             assertTrue( success );
426         }
427         catch ( Exception e )
428         {
429             fail( "An excecption should not have been thrown." );
430         }     
431     }
432  
433     
434     public void testConfigureInvalidRepositoryConsumer()
435         throws Exception
436     {
437         recordRepoConsumers();
438         
439         repoConsumerUtilsControl.replay();
440         knownContentConsumerControl.replay();
441         invalidContentConsumerControl.replay();
442         
443         try
444         {
445             service.configureRepositoryConsumer( null, "invalid-consumer", true );
446             fail( "An exception should have been thrown." );
447         }
448         catch ( Exception e )
449         {
450             assertEquals( "Invalid repository consumer.", e.getMessage() );
451         }
452         
453         repoConsumerUtilsControl.verify();
454         knownContentConsumerControl.verify();
455         invalidContentConsumerControl.verify(); 
456     }
457
458 /* Tests for delete artifact  */
459     
460     public void testDeleteM2ArtifactArtifactExists()
461         throws Exception
462     {
463         ManagedRepositoryConfiguration managedRepo = createManagedRepo("default", "default-repo");
464         
465         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
466         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
467         
468         ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
469         repoContent.setRepository( managedRepo );
470         
471         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
472                 
473         List<ArchivaArtifact> artifacts = getArtifacts();
474         
475         artifactDao.queryArtifacts( null );
476         artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
477         artifactDaoControl.setReturnValue( artifacts );
478         
479         listener.deleteArtifact( repoContent, artifacts.get( 0 ) );
480         listenerControl.setVoidCallable( 1 );
481                   
482         archivaConfigControl.replay();
483         configControl.replay();
484         repoFactoryControl.replay();    
485         artifactDaoControl.replay();
486         listenerControl.replay();
487        
488         boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
489         assertTrue( success ); 
490         
491         archivaConfigControl.verify();
492         configControl.verify();
493         repoFactoryControl.verify();
494         artifactDaoControl.verify();
495         listenerControl.verify();
496         
497         assertFalse( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.0" ).exists() );
498         assertTrue( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.1" ).exists() );
499     }
500     
501     public void testDeleteM1ArtifactArtifactExists()
502         throws Exception
503     {
504         MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
505         FileTypes fileTypes = ( FileTypes ) fileTypesControl.getMock();
506         
507         MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
508         PathParser parser = ( PathParser ) pathParserControl.getMock();
509         
510         ManagedRepositoryConfiguration managedRepo = createManagedRepo( "legacy", "legacy-repo" );
511         
512         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
513         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
514                 
515         ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
516         repoContent.setRepository( managedRepo );
517         repoContent.setFileTypes( fileTypes );
518         repoContent.setLegacyPathParser( parser );
519                 
520         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
521         
522         recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
523         
524         List<ArchivaArtifact> artifacts = getArtifacts();
525         
526         artifactDao.queryArtifacts( null );
527         artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
528         artifactDaoControl.setReturnValue( artifacts );
529                 
530         listener.deleteArtifact( repoContent, artifacts.get( 0 ) );
531         listenerControl.setVoidCallable( 1 );
532         
533         archivaConfigControl.replay();
534         configControl.replay();
535         repoFactoryControl.replay();
536         artifactDaoControl.replay();
537         listenerControl.replay();
538         fileTypesControl.replay();
539         pathParserControl.replay();
540        
541         boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
542         assertTrue( success ); 
543         
544         archivaConfigControl.verify();
545         configControl.verify();
546         repoFactoryControl.verify();
547         artifactDaoControl.verify();
548         listenerControl.verify();
549         fileTypesControl.verify();
550         pathParserControl.verify();
551         
552         File repo = new File( managedRepo.getLocation() );
553         assertFalse( new File( repo, "org.apache.archiva/jars/archiva-test-1.0.jar" ).exists() );
554         assertFalse( new File( repo, "org.apache.archiva/poms/archiva-test-1.0.pom" ).exists() );
555         
556         assertTrue( new File( repo, "org.apache.archiva/jars/archiva-test-1.1.jar" ).exists() );
557         assertTrue( new File( repo, "org.apache.archiva/jars/archiva-diff-1.0.jar" ).exists() );
558         assertTrue( new File( repo, "org.apache.archiva/poms/archiva-test-1.1.pom" ).exists() );
559         assertTrue( new File( repo, "org.apache.archiva/poms/archiva-diff-1.0.pom" ).exists() );
560     }
561
562     public void testDeleteArtifactArtifactDoesNotExist()
563         throws Exception
564     {
565         ManagedRepositoryConfiguration managedRepo = createManagedRepo("default", "default-repo");
566         
567         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
568         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
569         
570         ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
571         repoContent.setRepository( managedRepo );
572         
573         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
574         
575         archivaConfigControl.replay();
576         configControl.replay();
577         repoFactoryControl.replay();
578        
579         try
580         {
581             service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
582             fail( "An exception should have been thrown." );
583         }
584         catch ( Exception e )
585         {
586             assertEquals( "Artifact does not exist.", e.getMessage() );
587         }
588         
589         archivaConfigControl.verify();
590         configControl.verify();
591         repoFactoryControl.verify();
592     }
593
594     private ManagedRepositoryConfiguration createManagedRepo(String layout, String directory)
595         throws IOException
596     {
597         File srcDir = new File( getBasedir(), "src/test/repositories/"+ directory );
598
599         File repoDir = getTestFile( "target/test-repos/" + directory );
600
601         FileUtils.deleteDirectory( repoDir );
602
603         FileUtils.copyDirectory( srcDir, repoDir, FileFilterUtils.makeSVNAware( null ) );
604
605         ManagedRepositoryConfiguration managedRepo =
606             createManagedRepo( "internal", layout, "Internal Repository", true, false );
607         managedRepo.setLocation( repoDir.getAbsolutePath() );
608         return managedRepo;
609     }
610     
611     public void testDeleteArtifacRepositoryDoesNotExist()
612         throws Exception
613     {   
614         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
615         configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
616         
617         archivaConfigControl.replay();
618         configControl.replay();
619        
620         try
621         {
622             service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
623             fail( "An exception should have been thrown." );
624         }
625         catch ( Exception e )
626         {
627             assertEquals( "Repository does not exist.", e.getMessage() );
628         }
629         
630         archivaConfigControl.verify();
631         configControl.verify();
632     }
633     
634 /* Tests for repository scanning  */
635     
636     public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
637         throws Exception
638     {        
639         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
640         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
641                                        createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
642         
643         RepositoryTask task = new RepositoryTask();
644         
645         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
646         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false );
647         
648         taskScheduler.queueRepositoryTask( task );
649         taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
650         taskSchedulerControl.setVoidCallable();
651         
652         archivaConfigControl.replay();
653         configControl.replay();
654         taskSchedulerControl.replay();
655
656         try
657         {
658             boolean success = service.executeRepositoryScanner( "internal" );
659             assertTrue( success );
660         }
661         catch ( Exception e )
662         {
663             fail( "An exception should not have been thrown." );
664         }
665         
666         archivaConfigControl.verify();
667         configControl.verify();
668         taskSchedulerControl.verify();
669     }
670     
671     public void testExecuteRepoScannerRepoExistsButBeingScanned()
672         throws Exception
673     {        
674         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
675         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
676                                        createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
677         
678         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
679         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), true);
680         
681         archivaConfigControl.replay();
682         configControl.replay();
683         taskSchedulerControl.replay();
684     
685         try
686         {
687             boolean success = service.executeRepositoryScanner( "internal" );
688             assertFalse( success );
689         }
690         catch ( Exception e )
691         {
692             fail( "An exception should not have been thrown." );
693         }
694         
695         archivaConfigControl.verify();
696         configControl.verify();
697         taskSchedulerControl.verify();
698     }
699     
700     public void testExecuteRepoScannerRepoDoesNotExist()
701         throws Exception
702     {
703         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
704         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
705         
706         archivaConfigControl.replay();
707         configControl.replay();
708        
709         try
710         {
711             service.executeRepositoryScanner( "internal" );
712             fail( "An exception should have been thrown." );
713         }
714         catch ( Exception e )
715         {
716             assertEquals( "Repository does not exist.", e.getMessage() );
717         }
718         
719         archivaConfigControl.verify();
720         configControl.verify();
721     }
722     
723 /* Tests for db scanning  */
724     
725     public void testExecuteDbScannerDbNotBeingScanned()
726         throws Exception
727     {
728         DatabaseTask task = new DatabaseTask();
729         
730         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false );
731                 
732         taskScheduler.queueDatabaseTask( task );
733         taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
734         taskSchedulerControl.setVoidCallable();
735         
736         taskSchedulerControl.replay();
737
738         boolean success = service.executeDatabaseScanner();
739         
740         taskSchedulerControl.verify();        
741         
742         assertTrue( success );
743     }
744     
745     public void testExecuteDbScannerDbIsBeingScanned()
746         throws Exception
747     {        
748         taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), true );
749                 
750         taskSchedulerControl.replay();
751
752         boolean success = service.executeDatabaseScanner();
753         
754         taskSchedulerControl.verify();        
755         
756         assertFalse( success );
757     }
758      
759 /* Tests for querying repositories  */
760     
761     public void testGetAllManagedRepositories()
762         throws Exception
763     {
764         List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();        
765         managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
766         managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
767         
768         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
769         configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
770         
771         archivaConfigControl.replay();
772         configControl.replay();
773         
774         List<ManagedRepository> repos = service.getAllManagedRepositories(); 
775         
776         archivaConfigControl.verify();
777         configControl.verify();
778         
779         assertNotNull( repos );
780         assertEquals( 2, repos.size() );
781                 
782         assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) );
783         assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );
784     }
785
786     public void testGetAllRemoteRepositories()
787         throws Exception
788     {
789         List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>(); 
790         remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2") );
791         remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy") );
792         
793         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
794         configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
795         
796         archivaConfigControl.replay();
797         configControl.replay();
798         
799         List<RemoteRepository> repos = service.getAllRemoteRepositories(); 
800         
801         archivaConfigControl.verify();
802         configControl.verify();
803         
804         assertNotNull( repos );
805         assertEquals( 2, repos.size() );
806          
807         assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) );
808         assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) );        
809     }
810     
811 /* private methods  */
812     
813     private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
814     {
815         assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
816         assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
817         assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
818         assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );       
819     }
820     
821     private RemoteRepositoryConfiguration createRemoteRepository(String id, String name, String layout, String url)
822     {
823         RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
824         remoteConfig.setId( id );
825         remoteConfig.setName( name );
826         remoteConfig.setLayout( layout );
827         remoteConfig.setUrl( url );
828         
829         return remoteConfig;
830     }
831     
832     private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
833     {
834         assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
835         assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
836         assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
837         
838         //TODO enable assert once fixed in AdministrationServiceImpl!
839         //assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl() );
840         assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
841         assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
842     }
843
844     private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
845                                                               boolean hasReleases, boolean hasSnapshots )
846     {
847         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
848         repoConfig.setId( id );
849         repoConfig.setLayout( layout );
850         repoConfig.setName( name );
851         repoConfig.setReleases( hasReleases );
852         repoConfig.setSnapshots( hasSnapshots );
853         
854         return repoConfig;
855     }
856     
857     private void recordRepoConsumers()
858     {
859         List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
860         availableKnownConsumers.add( indexArtifactConsumer );
861         availableKnownConsumers.add( indexPomConsumer );
862         
863         List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
864         availableInvalidConsumers.add( checkPomConsumer );
865         availableInvalidConsumers.add( checkMetadataConsumer );
866         
867         repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(), availableKnownConsumers );
868         knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
869         knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
870         
871         repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(), availableInvalidConsumers );
872         invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
873         invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
874     }
875     
876     private void recordDbConsumers()
877     {
878         List<DatabaseCleanupConsumer> cleanupConsumers = new ArrayList<DatabaseCleanupConsumer>();
879         cleanupConsumers.add( cleanupIndexConsumer );
880         cleanupConsumers.add( cleanupDbConsumer );
881         
882         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
883             new ArrayList<DatabaseUnprocessedArtifactConsumer>();
884         unprocessedConsumers.add( processArtifactConsumer );
885         unprocessedConsumers.add( processPomConsumer );
886         
887         dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableCleanupConsumers(), cleanupConsumers );
888         cleanupConsumersControl.expectAndReturn( cleanupIndexConsumer.getId(), "cleanup-index" );
889         cleanupConsumersControl.expectAndReturn( cleanupDbConsumer.getId(), "cleanup-database" );
890         
891         dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableUnprocessedConsumers(), unprocessedConsumers );
892         unprocessedConsumersControl.expectAndReturn( processArtifactConsumer.getId(), "process-artifact" );
893         unprocessedConsumersControl.expectAndReturn( processPomConsumer.getId(), "process-pom" );
894     }    
895     
896     private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
897                                                    MockControl pathParserControl, PathParser parser )
898         throws LayoutException
899     {
900                 String sep = File.separator;
901                 String ad10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-diff-1.0.pom";
902                 String at10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.0.pom";
903                 String at11p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.1.pom";
904                 String ad10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-diff-1.0.jar";
905                 String at10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.0.jar";
906                 String at11j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.1.jar";
907
908         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10p ), true );
909         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11p ), true );
910         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10p ), true );
911         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10j ), true );
912         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10j ), true );
913         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11j ), true );
914         
915         ArtifactReference aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "pom");
916         pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
917         
918         aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "pom");                
919         pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
920         
921         aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "pom");
922         pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
923         
924         aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "jar");                
925         pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
926         
927         aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "jar");
928         pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
929         
930         aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "jar");
931         pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
932     }
933     
934     private List<ArchivaArtifact> getArtifacts()
935     {
936         List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
937         
938         ArchivaArtifactModel model = new ArchivaArtifactModel();
939         model.setRepositoryId( "internal" );
940         model.setGroupId( "org.apache.archiva" );
941         model.setArtifactId( "archiva-test" );
942         model.setVersion( "1.0" );
943         model.setType( "jar" );
944         
945         ArchivaArtifact artifact = new ArchivaArtifact( model );
946         artifacts.add( artifact );
947         return artifacts;
948     }
949
950     private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
951     {
952         ArtifactReference aRef = new ArtifactReference();
953         aRef.setArtifactId( artifactId );
954         aRef.setGroupId( groupId );
955         aRef.setType( type );
956         aRef.setVersion( version );
957         
958         return aRef;
959     }
960 }