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