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