1 package org.apache.archiva.web.xmlrpc.services;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.util.ArrayList;
24 import java.util.List;
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;
58 * AdministrationServiceImplTest
60 * @version $Id: AdministrationServiceImplTest.java
62 public class AdministrationServiceImplTest
63 extends PlexusInSpringTestCase
65 private MockControl archivaConfigControl;
67 private ArchivaConfiguration archivaConfig;
69 private MockControl configControl;
71 private Configuration config;
73 private AdministrationServiceImpl service;
75 private MockControl taskSchedulerControl;
77 private ArchivaTaskScheduler taskScheduler;
79 // repository consumers
80 private MockControl repoConsumerUtilsControl;
82 private RepositoryContentConsumers repoConsumersUtil;
84 private MockControl knownContentConsumerControl;
86 private MockControl invalidContentConsumerControl;
88 private KnownRepositoryContentConsumer indexArtifactConsumer;
90 private KnownRepositoryContentConsumer indexPomConsumer;
92 private InvalidRepositoryContentConsumer checkPomConsumer;
94 private InvalidRepositoryContentConsumer checkMetadataConsumer;
97 private MockControl dbConsumersUtilControl;
99 private DatabaseConsumers dbConsumersUtil;
101 private MockControl unprocessedConsumersControl;
103 private MockControl cleanupConsumersControl;
105 private DatabaseUnprocessedArtifactConsumer processArtifactConsumer;
107 private DatabaseUnprocessedArtifactConsumer processPomConsumer;
109 private DatabaseCleanupConsumer cleanupIndexConsumer;
111 private DatabaseCleanupConsumer cleanupDbConsumer;
114 private MockControl repoFactoryControl;
116 private RepositoryContentFactory repositoryFactory;
118 private MockControl artifactDaoControl;
120 private ArtifactDAO artifactDao;
122 private MockControl cleanupControl;
124 private DatabaseCleanupConsumer cleanupConsumer;
126 protected void setUp()
131 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
132 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
134 configControl = MockClassControl.createControl( Configuration.class );
135 config = ( Configuration ) configControl.getMock();
137 taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
138 taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock();
141 repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class );
142 repoConsumersUtil = ( RepositoryContentConsumers ) repoConsumerUtilsControl.getMock();
144 knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
145 indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
146 indexPomConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
148 invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
149 checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
150 checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
153 dbConsumersUtilControl = MockClassControl.createControl( DatabaseConsumers.class );
154 dbConsumersUtil = ( DatabaseConsumers ) dbConsumersUtilControl.getMock();
156 cleanupConsumersControl = MockControl.createControl( DatabaseCleanupConsumer.class );
157 cleanupIndexConsumer = ( DatabaseCleanupConsumer ) cleanupConsumersControl.getMock();
158 cleanupDbConsumer = ( DatabaseCleanupConsumer ) cleanupConsumersControl.getMock();
160 unprocessedConsumersControl = MockControl.createControl( DatabaseUnprocessedArtifactConsumer.class );
161 processArtifactConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
162 processPomConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
165 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
166 repositoryFactory = ( RepositoryContentFactory ) repoFactoryControl.getMock();
168 artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
169 artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
171 cleanupControl = MockClassControl.createControl( DatabaseCleanupConsumer.class );
172 cleanupConsumer = ( DatabaseCleanupConsumer ) cleanupControl.getMock();
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 );
185 /* Tests for database consumers */
187 public void testGetAllDbConsumers()
192 dbConsumersUtilControl.replay();
193 cleanupConsumersControl.replay();
194 unprocessedConsumersControl.replay();
196 List<String> dbConsumers = service.getAllDatabaseConsumers();
198 dbConsumersUtilControl.verify();
199 cleanupConsumersControl.verify();
200 unprocessedConsumersControl.verify();
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" ) );
210 public void testConfigureValidDatabaseConsumer()
213 DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
214 dbScanning.addCleanupConsumer( "cleanup-index" );
215 dbScanning.addCleanupConsumer( "cleanup-database" );
216 dbScanning.addUnprocessedConsumer( "process-artifact" );
220 // test enable "process-pom" db consumer
221 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
222 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
224 config.setDatabaseScanning( dbScanning );
225 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
226 configControl.setVoidCallable();
228 archivaConfig.save( config );
229 archivaConfigControl.setVoidCallable();
231 dbConsumersUtilControl.replay();
232 cleanupConsumersControl.replay();
233 unprocessedConsumersControl.replay();
234 archivaConfigControl.replay();
235 configControl.replay();
239 boolean success = service.configureDatabaseConsumer( "process-pom", true );
240 assertTrue( success );
242 catch ( Exception e )
244 fail( "An exception should not have been thrown." );
247 dbConsumersUtilControl.verify();
248 cleanupConsumersControl.verify();
249 unprocessedConsumersControl.verify();
250 archivaConfigControl.verify();
251 configControl.verify();
253 // test disable "process-pom" db consumer
254 dbConsumersUtilControl.reset();
255 cleanupConsumersControl.reset();
256 unprocessedConsumersControl.reset();
257 archivaConfigControl.reset();
258 configControl.reset();
260 dbScanning.addUnprocessedConsumer( "process-pom" );
264 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
265 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
267 config.setDatabaseScanning( dbScanning );
268 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
269 configControl.setVoidCallable();
271 archivaConfig.save( config );
272 archivaConfigControl.setVoidCallable();
274 dbConsumersUtilControl.replay();
275 cleanupConsumersControl.replay();
276 unprocessedConsumersControl.replay();
277 archivaConfigControl.replay();
278 configControl.replay();
282 boolean success = service.configureDatabaseConsumer( "process-pom", false );
283 assertTrue( success );
285 catch ( Exception e )
287 fail( "An exception should not have been thrown." );
290 dbConsumersUtilControl.verify();
291 cleanupConsumersControl.verify();
292 unprocessedConsumersControl.verify();
293 archivaConfigControl.verify();
294 configControl.verify();
297 public void testConfigureInvalidDatabaseConsumer()
302 dbConsumersUtilControl.replay();
303 cleanupConsumersControl.replay();
304 unprocessedConsumersControl.replay();
308 service.configureDatabaseConsumer( "invalid-consumer", true );
309 fail( "An exception should have been thrown." );
311 catch ( Exception e )
313 assertEquals( "Invalid database consumer.", e.getMessage() );
316 dbConsumersUtilControl.verify();
317 cleanupConsumersControl.verify();
318 unprocessedConsumersControl.verify();
321 /* Tests for repository consumers */
323 public void testGetAllRepoConsumers()
326 recordRepoConsumers();
328 repoConsumerUtilsControl.replay();
329 knownContentConsumerControl.replay();
330 invalidContentConsumerControl.replay();
332 List<String> repoConsumers = service.getAllRepositoryConsumers();
334 repoConsumerUtilsControl.verify();
335 knownContentConsumerControl.verify();
336 invalidContentConsumerControl.verify();
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" ) );
346 public void testConfigureValidRepositoryConsumer()
349 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
350 repoScanning.addKnownContentConsumer( "index-artifact" );
351 repoScanning.addKnownContentConsumer( "index-pom" );
352 repoScanning.addInvalidContentConsumer( "check-pom" );
354 // test enable "check-metadata" consumer
355 recordRepoConsumers();
357 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
358 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
360 config.setRepositoryScanning( repoScanning );
361 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
362 configControl.setVoidCallable();
364 archivaConfig.save( config );
365 archivaConfigControl.setVoidCallable();
367 repoConsumerUtilsControl.replay();
368 knownContentConsumerControl.replay();
369 invalidContentConsumerControl.replay();
370 archivaConfigControl.replay();
371 configControl.replay();
375 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
376 assertTrue( success );
378 catch ( Exception e )
380 fail( "An exception should not have been thrown." );
383 repoConsumerUtilsControl.verify();
384 knownContentConsumerControl.verify();
385 invalidContentConsumerControl.verify();
386 archivaConfigControl.verify();
387 configControl.verify();
389 // test disable "check-metadata" consumer
390 repoConsumerUtilsControl.reset();
391 knownContentConsumerControl.reset();
392 invalidContentConsumerControl.reset();
393 archivaConfigControl.reset();
394 configControl.reset();
396 repoScanning.addInvalidContentConsumer( "check-metadata" );
398 recordRepoConsumers();
400 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
401 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
403 config.setRepositoryScanning( repoScanning );
404 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
405 configControl.setVoidCallable();
407 archivaConfig.save( config );
408 archivaConfigControl.setVoidCallable();
410 repoConsumerUtilsControl.replay();
411 knownContentConsumerControl.replay();
412 invalidContentConsumerControl.replay();
413 archivaConfigControl.replay();
414 configControl.replay();
418 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
420 repoConsumerUtilsControl.verify();
421 knownContentConsumerControl.verify();
422 invalidContentConsumerControl.verify();
423 archivaConfigControl.verify();
424 configControl.verify();
426 assertTrue( success );
428 catch ( Exception e )
430 fail( "An excecption should not have been thrown." );
435 public void testConfigureInvalidRepositoryConsumer()
438 recordRepoConsumers();
440 repoConsumerUtilsControl.replay();
441 knownContentConsumerControl.replay();
442 invalidContentConsumerControl.replay();
446 service.configureRepositoryConsumer( null, "invalid-consumer", true );
447 fail( "An exception should have been thrown." );
449 catch ( Exception e )
451 assertEquals( "Invalid repository consumer.", e.getMessage() );
454 repoConsumerUtilsControl.verify();
455 knownContentConsumerControl.verify();
456 invalidContentConsumerControl.verify();
459 /* Tests for delete artifact */
461 public void testDeleteM2ArtifactArtifactExists()
464 File file = new File( getBasedir(), "/target/test-classes/default-repo/" );
465 assertTrue( file.exists() );
467 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "default", "Internal Repository", true, false );
468 managedRepo.setLocation( file.getAbsolutePath() );
470 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
471 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
473 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
474 repoContent.setRepository( managedRepo );
476 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
478 List<ArchivaArtifact> artifacts = getArtifacts();
480 artifactDao.queryArtifacts( null );
481 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
482 artifactDaoControl.setReturnValue( artifacts );
484 cleanupConsumer.processArchivaArtifact( artifacts.get( 0 ) );
485 cleanupControl.setVoidCallable( 2 );
487 archivaConfigControl.replay();
488 configControl.replay();
489 repoFactoryControl.replay();
490 artifactDaoControl.replay();
491 cleanupControl.replay();
495 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
496 assertTrue( success );
498 catch ( Exception e )
500 fail( "An exception should not have been thrown." );
503 archivaConfigControl.verify();
504 configControl.verify();
505 repoFactoryControl.verify();
506 artifactDaoControl.verify();
507 cleanupControl.verify();
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() );
513 public void testDeleteM1ArtifactArtifactExists()
516 MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
517 FileTypes fileTypes = ( FileTypes ) fileTypesControl.getMock();
519 MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
520 PathParser parser = ( PathParser ) pathParserControl.getMock();
522 File file = new File( getBasedir(), "/target/test-classes/legacy-repo/" );
523 assertTrue( file.exists() );
525 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "legacy", "Internal Repository", true, false );
526 managedRepo.setLocation( file.getAbsolutePath() );
528 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
529 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
531 ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
532 repoContent.setRepository( managedRepo );
533 repoContent.setFileTypes( fileTypes );
534 repoContent.setLegacyPathParser( parser );
536 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
538 recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
540 List<ArchivaArtifact> artifacts = getArtifacts();
542 artifactDao.queryArtifacts( null );
543 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
544 artifactDaoControl.setReturnValue( artifacts );
546 cleanupConsumer.processArchivaArtifact( artifacts.get( 0 ) );
547 cleanupControl.setVoidCallable( 2 );
549 archivaConfigControl.replay();
550 configControl.replay();
551 repoFactoryControl.replay();
552 artifactDaoControl.replay();
553 cleanupControl.replay();
554 fileTypesControl.replay();
555 pathParserControl.replay();
559 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
560 assertTrue( success );
562 catch ( Exception e )
564 fail( "An exception should not have been thrown." );
567 archivaConfigControl.verify();
568 configControl.verify();
569 repoFactoryControl.verify();
570 artifactDaoControl.verify();
571 cleanupControl.verify();
572 fileTypesControl.verify();
573 pathParserControl.verify();
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() );
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() );
584 public void testDeleteArtifactArtifactDoesNotExist()
587 File file = new File( getBasedir(), "/target/test-classes/default-repo/" );
588 assertTrue( file.exists() );
590 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "default", "Internal Repository", true, false );
591 managedRepo.setLocation( file.getAbsolutePath() );
593 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
594 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
596 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
597 repoContent.setRepository( managedRepo );
599 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
601 archivaConfigControl.replay();
602 configControl.replay();
603 repoFactoryControl.replay();
607 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
608 fail( "An exception should have been thrown." );
610 catch ( Exception e )
612 assertEquals( "Artifact does not exist.", e.getMessage() );
615 archivaConfigControl.verify();
616 configControl.verify();
617 repoFactoryControl.verify();
620 public void testDeleteArtifacRepositoryDoesNotExist()
623 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
624 configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
626 archivaConfigControl.replay();
627 configControl.replay();
631 service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
632 fail( "An exception should have been thrown." );
634 catch ( Exception e )
636 assertEquals( "Repository does not exist.", e.getMessage() );
639 archivaConfigControl.verify();
640 configControl.verify();
643 /* Tests for repository scanning */
645 public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
648 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
649 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
650 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
652 RepositoryTask task = new RepositoryTask();
654 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
655 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false );
657 taskScheduler.queueRepositoryTask( task );
658 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
659 taskSchedulerControl.setVoidCallable();
661 archivaConfigControl.replay();
662 configControl.replay();
663 taskSchedulerControl.replay();
667 boolean success = service.executeRepositoryScanner( "internal" );
668 assertTrue( success );
670 catch ( Exception e )
672 fail( "An exception should not have been thrown." );
675 archivaConfigControl.verify();
676 configControl.verify();
677 taskSchedulerControl.verify();
680 public void testExecuteRepoScannerRepoExistsButBeingScanned()
683 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
684 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
685 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
687 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
688 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), true);
690 archivaConfigControl.replay();
691 configControl.replay();
692 taskSchedulerControl.replay();
696 boolean success = service.executeRepositoryScanner( "internal" );
697 assertFalse( success );
699 catch ( Exception e )
701 fail( "An exception should not have been thrown." );
704 archivaConfigControl.verify();
705 configControl.verify();
706 taskSchedulerControl.verify();
709 public void testExecuteRepoScannerRepoDoesNotExist()
712 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
713 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
715 archivaConfigControl.replay();
716 configControl.replay();
720 service.executeRepositoryScanner( "internal" );
721 fail( "An exception should have been thrown." );
723 catch ( Exception e )
725 assertEquals( "Repository does not exist.", e.getMessage() );
728 archivaConfigControl.verify();
729 configControl.verify();
732 /* Tests for db scanning */
734 public void testExecuteDbScannerDbNotBeingScanned()
737 DatabaseTask task = new DatabaseTask();
739 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false );
741 taskScheduler.queueDatabaseTask( task );
742 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
743 taskSchedulerControl.setVoidCallable();
745 taskSchedulerControl.replay();
747 boolean success = service.executeDatabaseScanner();
749 taskSchedulerControl.verify();
751 assertTrue( success );
754 public void testExecuteDbScannerDbIsBeingScanned()
757 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), true );
759 taskSchedulerControl.replay();
761 boolean success = service.executeDatabaseScanner();
763 taskSchedulerControl.verify();
765 assertFalse( success );
768 /* Tests for querying repositories */
770 public void testGetAllManagedRepositories()
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 ) );
777 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
778 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
780 archivaConfigControl.replay();
781 configControl.replay();
783 List<ManagedRepository> repos = service.getAllManagedRepositories();
785 archivaConfigControl.verify();
786 configControl.verify();
788 assertNotNull( repos );
789 assertEquals( 2, repos.size() );
791 assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) );
792 assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );
795 public void testGetAllRemoteRepositories()
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") );
802 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
803 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
805 archivaConfigControl.replay();
806 configControl.replay();
808 List<RemoteRepository> repos = service.getAllRemoteRepositories();
810 archivaConfigControl.verify();
811 configControl.verify();
813 assertNotNull( repos );
814 assertEquals( 2, repos.size() );
816 assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) );
817 assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) );
820 /* private methods */
822 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
824 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
825 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
826 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
827 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
830 private RemoteRepositoryConfiguration createRemoteRepository(String id, String name, String layout, String url)
832 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
833 remoteConfig.setId( id );
834 remoteConfig.setName( name );
835 remoteConfig.setLayout( layout );
836 remoteConfig.setUrl( url );
841 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
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() );
851 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
852 boolean hasReleases, boolean hasSnapshots )
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 );
864 private void recordRepoConsumers()
866 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
867 availableKnownConsumers.add( indexArtifactConsumer );
868 availableKnownConsumers.add( indexPomConsumer );
870 List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
871 availableInvalidConsumers.add( checkPomConsumer );
872 availableInvalidConsumers.add( checkMetadataConsumer );
874 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(), availableKnownConsumers );
875 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
876 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
878 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(), availableInvalidConsumers );
879 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
880 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
883 private void recordDbConsumers()
885 List<DatabaseCleanupConsumer> cleanupConsumers = new ArrayList<DatabaseCleanupConsumer>();
886 cleanupConsumers.add( cleanupIndexConsumer );
887 cleanupConsumers.add( cleanupDbConsumer );
889 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
890 new ArrayList<DatabaseUnprocessedArtifactConsumer>();
891 unprocessedConsumers.add( processArtifactConsumer );
892 unprocessedConsumers.add( processPomConsumer );
894 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableCleanupConsumers(), cleanupConsumers );
895 cleanupConsumersControl.expectAndReturn( cleanupIndexConsumer.getId(), "cleanup-index" );
896 cleanupConsumersControl.expectAndReturn( cleanupDbConsumer.getId(), "cleanup-database" );
898 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableUnprocessedConsumers(), unprocessedConsumers );
899 unprocessedConsumersControl.expectAndReturn( processArtifactConsumer.getId(), "process-artifact" );
900 unprocessedConsumersControl.expectAndReturn( processPomConsumer.getId(), "process-pom" );
903 private List<ArchivaArtifact> getArtifacts()
905 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
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" );
914 ArchivaArtifact artifact = new ArchivaArtifact( model );
915 artifacts.add( artifact );
919 private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
920 MockControl pathParserControl, PathParser parser )
921 throws LayoutException
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 );
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 );
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 );
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 );
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 );
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 );
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 );
949 private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
951 ArtifactReference aRef = new ArtifactReference();
952 aRef.setArtifactId( artifactId );
953 aRef.setGroupId( groupId );
954 aRef.setType( type );
955 aRef.setVersion( version );