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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
29 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
30 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.io.filefilter.FileFilterUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.Configuration;
35 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
36 import org.apache.maven.archiva.configuration.FileTypes;
37 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
38 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
39 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
40 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
41 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
42 import org.apache.maven.archiva.database.ArtifactDAO;
43 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
44 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
45 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
46 import org.apache.maven.archiva.model.ArchivaArtifact;
47 import org.apache.maven.archiva.model.ArchivaArtifactModel;
48 import org.apache.maven.archiva.model.ArtifactReference;
49 import org.apache.maven.archiva.repository.RepositoryContentFactory;
50 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
51 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
52 import org.apache.maven.archiva.repository.content.PathParser;
53 import org.apache.maven.archiva.repository.events.RepositoryListener;
54 import org.apache.maven.archiva.repository.layout.LayoutException;
55 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
56 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
57 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
58 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
59 import org.easymock.MockControl;
60 import org.easymock.classextension.MockClassControl;
63 * AdministrationServiceImplTest
65 * @version $Id: AdministrationServiceImplTest.java
67 public class AdministrationServiceImplTest
68 extends PlexusInSpringTestCase
70 private MockControl archivaConfigControl;
72 private ArchivaConfiguration archivaConfig;
74 private MockControl configControl;
76 private Configuration config;
78 private AdministrationServiceImpl service;
80 private MockControl taskSchedulerControl;
82 private ArchivaTaskScheduler taskScheduler;
84 // repository consumers
85 private MockControl repoConsumerUtilsControl;
87 private RepositoryContentConsumers repoConsumersUtil;
89 private MockControl knownContentConsumerControl;
91 private MockControl invalidContentConsumerControl;
93 private KnownRepositoryContentConsumer indexArtifactConsumer;
95 private KnownRepositoryContentConsumer indexPomConsumer;
97 private InvalidRepositoryContentConsumer checkPomConsumer;
99 private InvalidRepositoryContentConsumer checkMetadataConsumer;
101 // database consumers
102 private MockControl dbConsumersUtilControl;
104 private DatabaseConsumers dbConsumersUtil;
106 private MockControl unprocessedConsumersControl;
108 private DatabaseUnprocessedArtifactConsumer processArtifactConsumer;
110 private DatabaseUnprocessedArtifactConsumer processPomConsumer;
113 private MockControl repoFactoryControl;
115 private RepositoryContentFactory repositoryFactory;
117 private MockControl artifactDaoControl;
119 private ArtifactDAO artifactDao;
121 private MockControl listenerControl;
123 private RepositoryListener listener;
125 private DatabaseCleanupConsumer cleanupIndexConsumer;
127 private DatabaseCleanupConsumer cleanupDbConsumer;
129 private MockControl cleanupConsumersControl;
131 protected void setUp()
136 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
137 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
139 configControl = MockClassControl.createControl( Configuration.class );
140 config = ( Configuration ) configControl.getMock();
142 taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
143 taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock();
146 repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class );
147 repoConsumersUtil = ( RepositoryContentConsumers ) repoConsumerUtilsControl.getMock();
149 knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
150 indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
151 indexPomConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
153 invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
154 checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
155 checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
158 dbConsumersUtilControl = MockClassControl.createControl( DatabaseConsumers.class );
159 dbConsumersUtil = ( DatabaseConsumers ) dbConsumersUtilControl.getMock();
161 cleanupConsumersControl = MockControl.createControl( DatabaseCleanupConsumer.class );
162 cleanupIndexConsumer = (DatabaseCleanupConsumer) cleanupConsumersControl.getMock();
163 cleanupDbConsumer = (DatabaseCleanupConsumer) cleanupConsumersControl.getMock();
165 unprocessedConsumersControl = MockControl.createControl( DatabaseUnprocessedArtifactConsumer.class );
166 processArtifactConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
167 processPomConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock();
170 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
171 repositoryFactory = ( RepositoryContentFactory ) repoFactoryControl.getMock();
173 artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
174 artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
176 listenerControl = MockControl.createControl( RepositoryListener.class );
177 listener = (RepositoryListener) listenerControl.getMock();
180 new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, dbConsumersUtil, repositoryFactory,
181 artifactDao, taskScheduler, Collections.singletonList( listener ) );
184 /* Tests for database consumers */
186 public void testGetAllDbConsumers()
191 dbConsumersUtilControl.replay();
192 cleanupConsumersControl.replay();
193 unprocessedConsumersControl.replay();
195 List<String> dbConsumers = service.getAllDatabaseConsumers();
197 dbConsumersUtilControl.verify();
198 cleanupConsumersControl.verify();
199 unprocessedConsumersControl.verify();
201 assertNotNull( dbConsumers );
202 assertEquals( 4, dbConsumers.size() );
203 assertTrue( dbConsumers.contains( "cleanup-index" ) );
204 assertTrue( dbConsumers.contains( "cleanup-database" ) );
205 assertTrue( dbConsumers.contains( "process-artifact" ) );
206 assertTrue( dbConsumers.contains( "process-pom" ) );
209 public void testConfigureValidDatabaseConsumer()
212 DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
213 dbScanning.addCleanupConsumer( "cleanup-index" );
214 dbScanning.addCleanupConsumer( "cleanup-database" );
215 dbScanning.addUnprocessedConsumer( "process-artifact" );
219 // test enable "process-pom" db consumer
220 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
221 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
223 config.setDatabaseScanning( dbScanning );
224 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
225 configControl.setVoidCallable();
227 archivaConfig.save( config );
228 archivaConfigControl.setVoidCallable();
230 dbConsumersUtilControl.replay();
231 cleanupConsumersControl.replay();
232 unprocessedConsumersControl.replay();
233 archivaConfigControl.replay();
234 configControl.replay();
238 boolean success = service.configureDatabaseConsumer( "process-pom", true );
239 assertTrue( success );
241 catch ( Exception e )
243 fail( "An exception should not have been thrown." );
246 dbConsumersUtilControl.verify();
247 cleanupConsumersControl.verify();
248 unprocessedConsumersControl.verify();
249 archivaConfigControl.verify();
250 configControl.verify();
252 // test disable "process-pom" db consumer
253 dbConsumersUtilControl.reset();
254 cleanupConsumersControl.reset();
255 unprocessedConsumersControl.reset();
256 archivaConfigControl.reset();
257 configControl.reset();
259 dbScanning.addUnprocessedConsumer( "process-pom" );
263 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
264 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
266 config.setDatabaseScanning( dbScanning );
267 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
268 configControl.setVoidCallable();
270 archivaConfig.save( config );
271 archivaConfigControl.setVoidCallable();
273 dbConsumersUtilControl.replay();
274 cleanupConsumersControl.replay();
275 unprocessedConsumersControl.replay();
276 archivaConfigControl.replay();
277 configControl.replay();
281 boolean success = service.configureDatabaseConsumer( "process-pom", false );
282 assertTrue( success );
284 catch ( Exception e )
286 fail( "An exception should not have been thrown." );
289 dbConsumersUtilControl.verify();
290 cleanupConsumersControl.verify();
291 unprocessedConsumersControl.verify();
292 archivaConfigControl.verify();
293 configControl.verify();
296 public void testConfigureInvalidDatabaseConsumer()
301 dbConsumersUtilControl.replay();
302 cleanupConsumersControl.replay();
303 unprocessedConsumersControl.replay();
307 service.configureDatabaseConsumer( "invalid-consumer", true );
308 fail( "An exception should have been thrown." );
310 catch ( Exception e )
312 assertEquals( "Invalid database consumer.", e.getMessage() );
315 dbConsumersUtilControl.verify();
316 cleanupConsumersControl.verify();
317 unprocessedConsumersControl.verify();
320 /* Tests for repository consumers */
322 public void testGetAllRepoConsumers()
325 recordRepoConsumers();
327 repoConsumerUtilsControl.replay();
328 knownContentConsumerControl.replay();
329 invalidContentConsumerControl.replay();
331 List<String> repoConsumers = service.getAllRepositoryConsumers();
333 repoConsumerUtilsControl.verify();
334 knownContentConsumerControl.verify();
335 invalidContentConsumerControl.verify();
337 assertNotNull( repoConsumers );
338 assertEquals( 4, repoConsumers.size() );
339 assertTrue( repoConsumers.contains( "index-artifact" ) );
340 assertTrue( repoConsumers.contains( "index-pom" ) );
341 assertTrue( repoConsumers.contains( "check-pom" ) );
342 assertTrue( repoConsumers.contains( "check-metadata" ) );
345 public void testConfigureValidRepositoryConsumer()
348 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
349 repoScanning.addKnownContentConsumer( "index-artifact" );
350 repoScanning.addKnownContentConsumer( "index-pom" );
351 repoScanning.addInvalidContentConsumer( "check-pom" );
353 // test enable "check-metadata" consumer
354 recordRepoConsumers();
356 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
357 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
359 config.setRepositoryScanning( repoScanning );
360 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
361 configControl.setVoidCallable();
363 archivaConfig.save( config );
364 archivaConfigControl.setVoidCallable();
366 repoConsumerUtilsControl.replay();
367 knownContentConsumerControl.replay();
368 invalidContentConsumerControl.replay();
369 archivaConfigControl.replay();
370 configControl.replay();
374 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
375 assertTrue( success );
377 catch ( Exception e )
379 fail( "An exception should not have been thrown." );
382 repoConsumerUtilsControl.verify();
383 knownContentConsumerControl.verify();
384 invalidContentConsumerControl.verify();
385 archivaConfigControl.verify();
386 configControl.verify();
388 // test disable "check-metadata" consumer
389 repoConsumerUtilsControl.reset();
390 knownContentConsumerControl.reset();
391 invalidContentConsumerControl.reset();
392 archivaConfigControl.reset();
393 configControl.reset();
395 repoScanning.addInvalidContentConsumer( "check-metadata" );
397 recordRepoConsumers();
399 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
400 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
402 config.setRepositoryScanning( repoScanning );
403 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
404 configControl.setVoidCallable();
406 archivaConfig.save( config );
407 archivaConfigControl.setVoidCallable();
409 repoConsumerUtilsControl.replay();
410 knownContentConsumerControl.replay();
411 invalidContentConsumerControl.replay();
412 archivaConfigControl.replay();
413 configControl.replay();
417 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
419 repoConsumerUtilsControl.verify();
420 knownContentConsumerControl.verify();
421 invalidContentConsumerControl.verify();
422 archivaConfigControl.verify();
423 configControl.verify();
425 assertTrue( success );
427 catch ( Exception e )
429 fail( "An excecption should not have been thrown." );
434 public void testConfigureInvalidRepositoryConsumer()
437 recordRepoConsumers();
439 repoConsumerUtilsControl.replay();
440 knownContentConsumerControl.replay();
441 invalidContentConsumerControl.replay();
445 service.configureRepositoryConsumer( null, "invalid-consumer", true );
446 fail( "An exception should have been thrown." );
448 catch ( Exception e )
450 assertEquals( "Invalid repository consumer.", e.getMessage() );
453 repoConsumerUtilsControl.verify();
454 knownContentConsumerControl.verify();
455 invalidContentConsumerControl.verify();
458 /* Tests for delete artifact */
460 public void testDeleteM2ArtifactArtifactExists()
463 ManagedRepositoryConfiguration managedRepo = createManagedRepo("default", "default-repo");
465 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
466 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
468 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
469 repoContent.setRepository( managedRepo );
471 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
473 List<ArchivaArtifact> artifacts = getArtifacts();
475 artifactDao.queryArtifacts( null );
476 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
477 artifactDaoControl.setReturnValue( artifacts );
479 listener.deleteArtifact( repoContent, artifacts.get( 0 ) );
480 listenerControl.setVoidCallable( 1 );
482 archivaConfigControl.replay();
483 configControl.replay();
484 repoFactoryControl.replay();
485 artifactDaoControl.replay();
486 listenerControl.replay();
488 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
489 assertTrue( success );
491 archivaConfigControl.verify();
492 configControl.verify();
493 repoFactoryControl.verify();
494 artifactDaoControl.verify();
495 listenerControl.verify();
497 assertFalse( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.0" ).exists() );
498 assertTrue( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.1" ).exists() );
501 public void testDeleteM1ArtifactArtifactExists()
504 MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
505 FileTypes fileTypes = ( FileTypes ) fileTypesControl.getMock();
507 MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
508 PathParser parser = ( PathParser ) pathParserControl.getMock();
510 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "legacy", "legacy-repo" );
512 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
513 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
515 ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
516 repoContent.setRepository( managedRepo );
517 repoContent.setFileTypes( fileTypes );
518 repoContent.setLegacyPathParser( parser );
520 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
522 recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
524 List<ArchivaArtifact> artifacts = getArtifacts();
526 artifactDao.queryArtifacts( null );
527 artifactDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
528 artifactDaoControl.setReturnValue( artifacts );
530 listener.deleteArtifact( repoContent, artifacts.get( 0 ) );
531 listenerControl.setVoidCallable( 1 );
533 archivaConfigControl.replay();
534 configControl.replay();
535 repoFactoryControl.replay();
536 artifactDaoControl.replay();
537 listenerControl.replay();
538 fileTypesControl.replay();
539 pathParserControl.replay();
541 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
542 assertTrue( success );
544 archivaConfigControl.verify();
545 configControl.verify();
546 repoFactoryControl.verify();
547 artifactDaoControl.verify();
548 listenerControl.verify();
549 fileTypesControl.verify();
550 pathParserControl.verify();
552 File repo = new File( managedRepo.getLocation() );
553 assertFalse( new File( repo, "org.apache.archiva/jars/archiva-test-1.0.jar" ).exists() );
554 assertFalse( new File( repo, "org.apache.archiva/poms/archiva-test-1.0.pom" ).exists() );
556 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-test-1.1.jar" ).exists() );
557 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-diff-1.0.jar" ).exists() );
558 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-test-1.1.pom" ).exists() );
559 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-diff-1.0.pom" ).exists() );
562 public void testDeleteArtifactArtifactDoesNotExist()
565 ManagedRepositoryConfiguration managedRepo = createManagedRepo("default", "default-repo");
567 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
568 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
570 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
571 repoContent.setRepository( managedRepo );
573 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
575 archivaConfigControl.replay();
576 configControl.replay();
577 repoFactoryControl.replay();
581 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
582 fail( "An exception should have been thrown." );
584 catch ( Exception e )
586 assertEquals( "Artifact does not exist.", e.getMessage() );
589 archivaConfigControl.verify();
590 configControl.verify();
591 repoFactoryControl.verify();
594 private ManagedRepositoryConfiguration createManagedRepo(String layout, String directory)
597 File srcDir = new File( getBasedir(), "src/test/repositories/"+ directory );
599 File repoDir = getTestFile( "target/test-repos/" + directory );
601 FileUtils.deleteDirectory( repoDir );
603 FileUtils.copyDirectory( srcDir, repoDir, FileFilterUtils.makeSVNAware( null ) );
605 ManagedRepositoryConfiguration managedRepo =
606 createManagedRepo( "internal", layout, "Internal Repository", true, false );
607 managedRepo.setLocation( repoDir.getAbsolutePath() );
611 public void testDeleteArtifacRepositoryDoesNotExist()
614 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
615 configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
617 archivaConfigControl.replay();
618 configControl.replay();
622 service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
623 fail( "An exception should have been thrown." );
625 catch ( Exception e )
627 assertEquals( "Repository does not exist.", e.getMessage() );
630 archivaConfigControl.verify();
631 configControl.verify();
634 /* Tests for repository scanning */
636 public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
639 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
640 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
641 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
643 RepositoryTask task = new RepositoryTask();
645 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
646 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false );
648 taskScheduler.queueRepositoryTask( task );
649 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
650 taskSchedulerControl.setVoidCallable();
652 archivaConfigControl.replay();
653 configControl.replay();
654 taskSchedulerControl.replay();
658 boolean success = service.executeRepositoryScanner( "internal" );
659 assertTrue( success );
661 catch ( Exception e )
663 fail( "An exception should not have been thrown." );
666 archivaConfigControl.verify();
667 configControl.verify();
668 taskSchedulerControl.verify();
671 public void testExecuteRepoScannerRepoExistsButBeingScanned()
674 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
675 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
676 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
678 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), true );
679 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), true);
681 archivaConfigControl.replay();
682 configControl.replay();
683 taskSchedulerControl.replay();
687 boolean success = service.executeRepositoryScanner( "internal" );
688 assertFalse( success );
690 catch ( Exception e )
692 fail( "An exception should not have been thrown." );
695 archivaConfigControl.verify();
696 configControl.verify();
697 taskSchedulerControl.verify();
700 public void testExecuteRepoScannerRepoDoesNotExist()
703 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
704 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
706 archivaConfigControl.replay();
707 configControl.replay();
711 service.executeRepositoryScanner( "internal" );
712 fail( "An exception should have been thrown." );
714 catch ( Exception e )
716 assertEquals( "Repository does not exist.", e.getMessage() );
719 archivaConfigControl.verify();
720 configControl.verify();
723 /* Tests for db scanning */
725 public void testExecuteDbScannerDbNotBeingScanned()
728 DatabaseTask task = new DatabaseTask();
730 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false );
732 taskScheduler.queueDatabaseTask( task );
733 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
734 taskSchedulerControl.setVoidCallable();
736 taskSchedulerControl.replay();
738 boolean success = service.executeDatabaseScanner();
740 taskSchedulerControl.verify();
742 assertTrue( success );
745 public void testExecuteDbScannerDbIsBeingScanned()
748 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), true );
750 taskSchedulerControl.replay();
752 boolean success = service.executeDatabaseScanner();
754 taskSchedulerControl.verify();
756 assertFalse( success );
759 /* Tests for querying repositories */
761 public void testGetAllManagedRepositories()
764 List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();
765 managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
766 managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
768 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
769 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
771 archivaConfigControl.replay();
772 configControl.replay();
774 List<ManagedRepository> repos = service.getAllManagedRepositories();
776 archivaConfigControl.verify();
777 configControl.verify();
779 assertNotNull( repos );
780 assertEquals( 2, repos.size() );
782 assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) );
783 assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );
786 public void testGetAllRemoteRepositories()
789 List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>();
790 remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2") );
791 remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy") );
793 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
794 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
796 archivaConfigControl.replay();
797 configControl.replay();
799 List<RemoteRepository> repos = service.getAllRemoteRepositories();
801 archivaConfigControl.verify();
802 configControl.verify();
804 assertNotNull( repos );
805 assertEquals( 2, repos.size() );
807 assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) );
808 assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) );
811 /* private methods */
813 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
815 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
816 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
817 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
818 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
821 private RemoteRepositoryConfiguration createRemoteRepository(String id, String name, String layout, String url)
823 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
824 remoteConfig.setId( id );
825 remoteConfig.setName( name );
826 remoteConfig.setLayout( layout );
827 remoteConfig.setUrl( url );
832 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
834 assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
835 assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
836 assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
838 //TODO enable assert once fixed in AdministrationServiceImpl!
839 //assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl() );
840 assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
841 assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
844 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
845 boolean hasReleases, boolean hasSnapshots )
847 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
848 repoConfig.setId( id );
849 repoConfig.setLayout( layout );
850 repoConfig.setName( name );
851 repoConfig.setReleases( hasReleases );
852 repoConfig.setSnapshots( hasSnapshots );
857 private void recordRepoConsumers()
859 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
860 availableKnownConsumers.add( indexArtifactConsumer );
861 availableKnownConsumers.add( indexPomConsumer );
863 List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
864 availableInvalidConsumers.add( checkPomConsumer );
865 availableInvalidConsumers.add( checkMetadataConsumer );
867 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(), availableKnownConsumers );
868 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
869 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
871 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(), availableInvalidConsumers );
872 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
873 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
876 private void recordDbConsumers()
878 List<DatabaseCleanupConsumer> cleanupConsumers = new ArrayList<DatabaseCleanupConsumer>();
879 cleanupConsumers.add( cleanupIndexConsumer );
880 cleanupConsumers.add( cleanupDbConsumer );
882 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
883 new ArrayList<DatabaseUnprocessedArtifactConsumer>();
884 unprocessedConsumers.add( processArtifactConsumer );
885 unprocessedConsumers.add( processPomConsumer );
887 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableCleanupConsumers(), cleanupConsumers );
888 cleanupConsumersControl.expectAndReturn( cleanupIndexConsumer.getId(), "cleanup-index" );
889 cleanupConsumersControl.expectAndReturn( cleanupDbConsumer.getId(), "cleanup-database" );
891 dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableUnprocessedConsumers(), unprocessedConsumers );
892 unprocessedConsumersControl.expectAndReturn( processArtifactConsumer.getId(), "process-artifact" );
893 unprocessedConsumersControl.expectAndReturn( processPomConsumer.getId(), "process-pom" );
896 private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
897 MockControl pathParserControl, PathParser parser )
898 throws LayoutException
900 String sep = File.separator;
901 String ad10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-diff-1.0.pom";
902 String at10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.0.pom";
903 String at11p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.1.pom";
904 String ad10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-diff-1.0.jar";
905 String at10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.0.jar";
906 String at11j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.1.jar";
908 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10p ), true );
909 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11p ), true );
910 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10p ), true );
911 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10j ), true );
912 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10j ), true );
913 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11j ), true );
915 ArtifactReference aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "pom");
916 pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
918 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "pom");
919 pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
921 aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "pom");
922 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
924 aRef = createArtifactReference("archiva-diff", "org.apache.archiva", "1.0", "jar");
925 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
927 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.0", "jar");
928 pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
930 aRef = createArtifactReference("archiva-test", "org.apache.archiva", "1.1", "jar");
931 pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
934 private List<ArchivaArtifact> getArtifacts()
936 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
938 ArchivaArtifactModel model = new ArchivaArtifactModel();
939 model.setRepositoryId( "internal" );
940 model.setGroupId( "org.apache.archiva" );
941 model.setArtifactId( "archiva-test" );
942 model.setVersion( "1.0" );
943 model.setType( "jar" );
945 ArchivaArtifact artifact = new ArchivaArtifact( model );
946 artifacts.add( artifact );
950 private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
952 ArtifactReference aRef = new ArtifactReference();
953 aRef.setArtifactId( artifactId );
954 aRef.setGroupId( groupId );
955 aRef.setType( type );
956 aRef.setVersion( version );