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( archivaConfig, repoConsumersUtil, dbConsumersUtil,
175 repositoryFactory, artifactDao, cleanupConsumer, cleanupConsumer, taskScheduler );
178 /* Tests for database consumers */
180 public void testGetAllDbConsumers()
185 dbConsumersUtilControl.replay();
186 cleanupConsumersControl.replay();
187 unprocessedConsumersControl.replay();
189 List<String> dbConsumers = service.getAllDatabaseConsumers();
191 dbConsumersUtilControl.verify();
192 cleanupConsumersControl.verify();
193 unprocessedConsumersControl.verify();
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" ) );
203 public void testConfigureValidDatabaseConsumer()
206 DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
207 dbScanning.addCleanupConsumer( "cleanup-index" );
208 dbScanning.addCleanupConsumer( "cleanup-database" );
209 dbScanning.addUnprocessedConsumer( "process-artifact" );
213 // test enable "process-pom" db consumer
214 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
215 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
217 config.setDatabaseScanning( dbScanning );
218 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
219 configControl.setVoidCallable();
221 archivaConfig.save( config );
222 archivaConfigControl.setVoidCallable();
224 dbConsumersUtilControl.replay();
225 cleanupConsumersControl.replay();
226 unprocessedConsumersControl.replay();
227 archivaConfigControl.replay();
228 configControl.replay();
232 boolean success = service.configureDatabaseConsumer( "process-pom", true );
233 assertTrue( success );
235 catch ( Exception e )
237 fail( "An exception should not have been thrown." );
240 dbConsumersUtilControl.verify();
241 cleanupConsumersControl.verify();
242 unprocessedConsumersControl.verify();
243 archivaConfigControl.verify();
244 configControl.verify();
246 // test disable "process-pom" db consumer
247 dbConsumersUtilControl.reset();
248 cleanupConsumersControl.reset();
249 unprocessedConsumersControl.reset();
250 archivaConfigControl.reset();
251 configControl.reset();
253 dbScanning.addUnprocessedConsumer( "process-pom" );
257 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
258 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
260 config.setDatabaseScanning( dbScanning );
261 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
262 configControl.setVoidCallable();
264 archivaConfig.save( config );
265 archivaConfigControl.setVoidCallable();
267 dbConsumersUtilControl.replay();
268 cleanupConsumersControl.replay();
269 unprocessedConsumersControl.replay();
270 archivaConfigControl.replay();
271 configControl.replay();
275 boolean success = service.configureDatabaseConsumer( "process-pom", false );
276 assertTrue( success );
278 catch ( Exception e )
280 fail( "An exception should not have been thrown." );
283 dbConsumersUtilControl.verify();
284 cleanupConsumersControl.verify();
285 unprocessedConsumersControl.verify();
286 archivaConfigControl.verify();
287 configControl.verify();
290 public void testConfigureInvalidDatabaseConsumer()
295 dbConsumersUtilControl.replay();
296 cleanupConsumersControl.replay();
297 unprocessedConsumersControl.replay();
301 service.configureDatabaseConsumer( "invalid-consumer", true );
302 fail( "An exception should have been thrown." );
304 catch ( Exception e )
306 assertEquals( "Invalid database consumer.", e.getMessage() );
309 dbConsumersUtilControl.verify();
310 cleanupConsumersControl.verify();
311 unprocessedConsumersControl.verify();
314 /* Tests for repository consumers */
316 public void testGetAllRepoConsumers()
319 recordRepoConsumers();
321 repoConsumerUtilsControl.replay();
322 knownContentConsumerControl.replay();
323 invalidContentConsumerControl.replay();
325 List<String> repoConsumers = service.getAllRepositoryConsumers();
327 repoConsumerUtilsControl.verify();
328 knownContentConsumerControl.verify();
329 invalidContentConsumerControl.verify();
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" ) );
339 public void testConfigureValidRepositoryConsumer()
342 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
343 repoScanning.addKnownContentConsumer( "index-artifact" );
344 repoScanning.addKnownContentConsumer( "index-pom" );
345 repoScanning.addInvalidContentConsumer( "check-pom" );
347 // test enable "check-metadata" consumer
348 recordRepoConsumers();
350 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
351 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
353 config.setRepositoryScanning( repoScanning );
354 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
355 configControl.setVoidCallable();
357 archivaConfig.save( config );
358 archivaConfigControl.setVoidCallable();
360 repoConsumerUtilsControl.replay();
361 knownContentConsumerControl.replay();
362 invalidContentConsumerControl.replay();
363 archivaConfigControl.replay();
364 configControl.replay();
368 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
369 assertTrue( success );
371 catch ( Exception e )
373 fail( "An exception should not have been thrown." );
376 repoConsumerUtilsControl.verify();
377 knownContentConsumerControl.verify();
378 invalidContentConsumerControl.verify();
379 archivaConfigControl.verify();
380 configControl.verify();
382 // test disable "check-metadata" consumer
383 repoConsumerUtilsControl.reset();
384 knownContentConsumerControl.reset();
385 invalidContentConsumerControl.reset();
386 archivaConfigControl.reset();
387 configControl.reset();
389 repoScanning.addInvalidContentConsumer( "check-metadata" );
391 recordRepoConsumers();
393 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
394 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
396 config.setRepositoryScanning( repoScanning );
397 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
398 configControl.setVoidCallable();
400 archivaConfig.save( config );
401 archivaConfigControl.setVoidCallable();
403 repoConsumerUtilsControl.replay();
404 knownContentConsumerControl.replay();
405 invalidContentConsumerControl.replay();
406 archivaConfigControl.replay();
407 configControl.replay();
411 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
413 repoConsumerUtilsControl.verify();
414 knownContentConsumerControl.verify();
415 invalidContentConsumerControl.verify();
416 archivaConfigControl.verify();
417 configControl.verify();
419 assertTrue( success );
421 catch ( Exception e )
423 fail( "An excecption should not have been thrown." );
428 public void testConfigureInvalidRepositoryConsumer()
431 recordRepoConsumers();
433 repoConsumerUtilsControl.replay();
434 knownContentConsumerControl.replay();
435 invalidContentConsumerControl.replay();
439 service.configureRepositoryConsumer( null, "invalid-consumer", true );
440 fail( "An exception should have been thrown." );
442 catch ( Exception e )
444 assertEquals( "Invalid repository consumer.", e.getMessage() );
447 repoConsumerUtilsControl.verify();
448 knownContentConsumerControl.verify();
449 invalidContentConsumerControl.verify();
452 /* Tests for delete artifact */
454 public void testDeleteM2ArtifactArtifactExists()
457 File file = new File( getBasedir(), "/target/test-classes/default-repo/" );
458 assertTrue( file.exists() );
460 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "default", "Internal Repository", true, false );
461 managedRepo.setLocation( file.getAbsolutePath() );
463 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
464 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
466 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
467 repoContent.setRepository( managedRepo );
469 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
471 List<ArchivaArtifact> artifacts = getArtifacts();
473 artifactDao.queryArtifacts( null );
474 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
475 artifactDaoControl.setReturnValue( artifacts );
477 cleanupConsumer.processArchivaArtifact( artifacts.get( 0 ) );
478 cleanupControl.setVoidCallable( 2 );
480 archivaConfigControl.replay();
481 configControl.replay();
482 repoFactoryControl.replay();
483 artifactDaoControl.replay();
484 cleanupControl.replay();
488 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
489 assertTrue( success );
491 catch ( Exception e )
493 fail( "An exception should not have been thrown." );
496 archivaConfigControl.verify();
497 configControl.verify();
498 repoFactoryControl.verify();
499 artifactDaoControl.verify();
500 cleanupControl.verify();
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() );
506 public void testDeleteM1ArtifactArtifactExists()
509 MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
510 FileTypes fileTypes = ( FileTypes ) fileTypesControl.getMock();
512 MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
513 PathParser parser = ( PathParser ) pathParserControl.getMock();
515 File file = new File( getBasedir(), "/target/test-classes/legacy-repo/" );
516 assertTrue( file.exists() );
518 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "legacy", "Internal Repository", true, false );
519 managedRepo.setLocation( file.getAbsolutePath() );
521 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
522 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
524 ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
525 repoContent.setRepository( managedRepo );
526 repoContent.setFileTypes( fileTypes );
527 repoContent.setLegacyPathParser( parser );
529 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
531 recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
533 List<ArchivaArtifact> artifacts = getArtifacts();
535 artifactDao.queryArtifacts( null );
536 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
537 artifactDaoControl.setReturnValue( artifacts );
539 cleanupConsumer.processArchivaArtifact( artifacts.get( 0 ) );
540 cleanupControl.setVoidCallable( 2 );
542 archivaConfigControl.replay();
543 configControl.replay();
544 repoFactoryControl.replay();
545 artifactDaoControl.replay();
546 cleanupControl.replay();
547 fileTypesControl.replay();
548 pathParserControl.replay();
552 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
553 assertTrue( success );
555 catch ( Exception e )
557 fail( "An exception should not have been thrown." );
560 archivaConfigControl.verify();
561 configControl.verify();
562 repoFactoryControl.verify();
563 artifactDaoControl.verify();
564 cleanupControl.verify();
565 fileTypesControl.verify();
566 pathParserControl.verify();
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() );
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() );
577 public void testDeleteArtifactArtifactDoesNotExist()
580 File file = new File( getBasedir(), "/target/test-classes/default-repo/" );
581 assertTrue( file.exists() );
583 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", "default", "Internal Repository", true, false );
584 managedRepo.setLocation( file.getAbsolutePath() );
586 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
587 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
589 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
590 repoContent.setRepository( managedRepo );
592 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
594 archivaConfigControl.replay();
595 configControl.replay();
596 repoFactoryControl.replay();
600 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
601 fail( "An exception should have been thrown." );
603 catch ( Exception e )
605 assertEquals( "Artifact does not exist.", e.getMessage() );
608 archivaConfigControl.verify();
609 configControl.verify();
610 repoFactoryControl.verify();
613 public void testDeleteArtifacRepositoryDoesNotExist()
616 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
617 configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
619 archivaConfigControl.replay();
620 configControl.replay();
624 service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
625 fail( "An exception should have been thrown." );
627 catch ( Exception e )
629 assertEquals( "Repository does not exist.", e.getMessage() );
632 archivaConfigControl.verify();
633 configControl.verify();
636 /* Tests for repository scanning */
638 public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
641 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
642 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
643 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
645 RepositoryTask task = new RepositoryTask();
647 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
648 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false );
650 taskScheduler.queueRepositoryTask( task );
651 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
652 taskSchedulerControl.setVoidCallable();
654 archivaConfigControl.replay();
655 configControl.replay();
656 taskSchedulerControl.replay();
660 boolean success = service.executeRepositoryScanner( "internal" );
661 assertTrue( success );
663 catch ( Exception e )
665 fail( "An exception should not have been thrown." );
668 archivaConfigControl.verify();
669 configControl.verify();
670 taskSchedulerControl.verify();
673 public void testExecuteRepoScannerRepoExistsButBeingScanned()
676 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
677 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
678 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
680 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
681 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), true);
683 archivaConfigControl.replay();
684 configControl.replay();
685 taskSchedulerControl.replay();
689 boolean success = service.executeRepositoryScanner( "internal" );
690 assertFalse( success );
692 catch ( Exception e )
694 fail( "An exception should not have been thrown." );
697 archivaConfigControl.verify();
698 configControl.verify();
699 taskSchedulerControl.verify();
702 public void testExecuteRepoScannerRepoDoesNotExist()
705 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
706 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
708 archivaConfigControl.replay();
709 configControl.replay();
713 service.executeRepositoryScanner( "internal" );
714 fail( "An exception should have been thrown." );
716 catch ( Exception e )
718 assertEquals( "Repository does not exist.", e.getMessage() );
721 archivaConfigControl.verify();
722 configControl.verify();
725 /* Tests for db scanning */
727 public void testExecuteDbScannerDbNotBeingScanned()
730 DatabaseTask task = new DatabaseTask();
732 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false );
734 taskScheduler.queueDatabaseTask( task );
735 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
736 taskSchedulerControl.setVoidCallable();
738 taskSchedulerControl.replay();
740 boolean success = service.executeDatabaseScanner();
742 taskSchedulerControl.verify();
744 assertTrue( success );
747 public void testExecuteDbScannerDbIsBeingScanned()
750 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), true );
752 taskSchedulerControl.replay();
754 boolean success = service.executeDatabaseScanner();
756 taskSchedulerControl.verify();
758 assertFalse( success );
761 /* Tests for querying repositories */
763 public void testGetAllManagedRepositories()
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 ) );
770 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
771 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
773 archivaConfigControl.replay();
774 configControl.replay();
776 List<ManagedRepository> repos = service.getAllManagedRepositories();
778 archivaConfigControl.verify();
779 configControl.verify();
781 assertNotNull( repos );
782 assertEquals( 2, repos.size() );
784 assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) );
785 assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );
788 public void testGetAllRemoteRepositories()
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") );
795 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
796 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
798 archivaConfigControl.replay();
799 configControl.replay();
801 List<RemoteRepository> repos = service.getAllRemoteRepositories();
803 archivaConfigControl.verify();
804 configControl.verify();
806 assertNotNull( repos );
807 assertEquals( 2, repos.size() );
809 assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) );
810 assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) );
813 /* private methods */
815 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
817 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
818 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
819 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
820 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
823 private RemoteRepositoryConfiguration createRemoteRepository(String id, String name, String layout, String url)
825 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
826 remoteConfig.setId( id );
827 remoteConfig.setName( name );
828 remoteConfig.setLayout( layout );
829 remoteConfig.setUrl( url );
834 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
836 assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
837 assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
838 assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
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() );
846 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
847 boolean hasReleases, boolean hasSnapshots )
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 );
859 private void recordRepoConsumers()
861 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
862 availableKnownConsumers.add( indexArtifactConsumer );
863 availableKnownConsumers.add( indexPomConsumer );
865 List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
866 availableInvalidConsumers.add( checkPomConsumer );
867 availableInvalidConsumers.add( checkMetadataConsumer );
869 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(), availableKnownConsumers );
870 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
871 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
873 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(), availableInvalidConsumers );
874 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
875 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
878 private void recordDbConsumers()
880 List<DatabaseCleanupConsumer> cleanupConsumers = new ArrayList<DatabaseCleanupConsumer>();
881 cleanupConsumers.add( cleanupIndexConsumer );
882 cleanupConsumers.add( cleanupDbConsumer );
884 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
885 new ArrayList<DatabaseUnprocessedArtifactConsumer>();
886 unprocessedConsumers.add( processArtifactConsumer );
887 unprocessedConsumers.add( processPomConsumer );
889 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableCleanupConsumers(), cleanupConsumers );
890 cleanupConsumersControl.expectAndReturn( cleanupIndexConsumer.getId(), "cleanup-index" );
891 cleanupConsumersControl.expectAndReturn( cleanupDbConsumer.getId(), "cleanup-database" );
893 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableUnprocessedConsumers(), unprocessedConsumers );
894 unprocessedConsumersControl.expectAndReturn( processArtifactConsumer.getId(), "process-artifact" );
895 unprocessedConsumersControl.expectAndReturn( processPomConsumer.getId(), "process-pom" );
898 private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
899 MockControl pathParserControl, PathParser parser )
900 throws LayoutException
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";
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 );
917 ArtifactReference aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "pom");
918 pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
920 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "pom");
921 pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
923 aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "pom");
924 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
926 aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "jar");
927 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
929 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "jar");
930 pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
932 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "jar");
933 pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
936 private List<ArchivaArtifact> getArtifacts()
938 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
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" );
947 ArchivaArtifact artifact = new ArchivaArtifact( model );
948 artifacts.add( artifact );
952 private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
954 ArtifactReference aRef = new ArtifactReference();
955 aRef.setArtifactId( artifactId );
956 aRef.setGroupId( groupId );
957 aRef.setType( type );
958 aRef.setVersion( version );