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
22 import org.apache.archiva.audit.AuditEvent;
23 import org.apache.archiva.audit.AuditListener;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.filter.Filter;
27 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
28 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
29 import org.apache.archiva.repository.events.RepositoryListener;
30 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
31 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
32 import org.apache.archiva.scheduler.repository.RepositoryTask;
33 import org.apache.archiva.stagerepository.merge.RepositoryMerger;
34 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
35 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.io.filefilter.FileFilterUtils;
38 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
39 import org.apache.maven.archiva.configuration.Configuration;
40 import org.apache.maven.archiva.configuration.FileTypes;
41 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
42 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
43 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
44 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
45 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
46 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
47 import org.apache.maven.archiva.model.ArtifactReference;
48 import org.apache.maven.archiva.repository.RepositoryContentFactory;
49 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
50 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
51 import org.apache.maven.archiva.repository.content.PathParser;
52 import org.apache.maven.archiva.repository.layout.LayoutException;
53 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
54 import org.easymock.MockControl;
55 import org.easymock.classextension.MockClassControl;
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.Collections;
61 import java.util.Date;
62 import java.util.HashMap;
63 import java.util.List;
67 * AdministrationServiceImplTest
69 * @version $Id: AdministrationServiceImplTest.java
71 public class AdministrationServiceImplTest
72 extends PlexusInSpringTestCase
74 private MockControl archivaConfigControl;
76 private ArchivaConfiguration archivaConfig;
78 private MockControl configControl;
80 private Configuration config;
82 private AdministrationServiceImpl service;
84 private MockControl repositoryTaskSchedulerControl;
86 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
88 // repository consumers
89 private MockControl repoConsumerUtilsControl;
91 private RepositoryContentConsumers repoConsumersUtil;
93 private MockControl knownContentConsumerControl;
95 private MockControl invalidContentConsumerControl;
97 private KnownRepositoryContentConsumer indexArtifactConsumer;
99 private KnownRepositoryContentConsumer indexPomConsumer;
101 private InvalidRepositoryContentConsumer checkPomConsumer;
103 private InvalidRepositoryContentConsumer checkMetadataConsumer;
106 private MockControl repoFactoryControl;
108 private RepositoryContentFactory repositoryFactory;
110 private MockControl listenerControl;
112 private RepositoryListener listener;
114 private MockControl metadataRepositoryControl;
116 private MetadataRepository metadataRepository;
118 private MockControl repositoryStatisticsManagerControl;
120 private RepositoryStatisticsManager repositoryStatisticsManager;
122 private MockControl repositoryMergerControl;
124 private RepositoryMerger repositoryMerger;
126 private MockControl auditListenerControl;
128 private AuditListener auditListener;
130 private static final String STAGE = "-stage";
132 protected void setUp()
137 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
138 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
140 configControl = MockClassControl.createControl( Configuration.class );
141 config = (Configuration) configControl.getMock();
143 repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
144 repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
147 repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class );
148 repoConsumersUtil = (RepositoryContentConsumers) repoConsumerUtilsControl.getMock();
150 knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
151 indexArtifactConsumer = (KnownRepositoryContentConsumer) knownContentConsumerControl.getMock();
152 indexPomConsumer = (KnownRepositoryContentConsumer) knownContentConsumerControl.getMock();
154 invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
155 checkPomConsumer = (InvalidRepositoryContentConsumer) invalidContentConsumerControl.getMock();
156 checkMetadataConsumer = (InvalidRepositoryContentConsumer) invalidContentConsumerControl.getMock();
159 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
160 repositoryFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
162 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
163 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
165 listenerControl = MockControl.createControl( RepositoryListener.class );
166 listener = (RepositoryListener) listenerControl.getMock();
168 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
169 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
171 repositoryMergerControl = MockControl.createControl( RepositoryMerger.class );
172 repositoryMerger = (RepositoryMerger) repositoryMergerControl.getMock();
174 auditListenerControl = MockControl.createControl( AuditListener.class );
175 auditListener = (AuditListener) auditListenerControl.getMock();
177 service = new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory,
178 metadataRepository, repositoryTaskScheduler, Collections.singletonList(
179 listener ), repositoryStatisticsManager, repositoryMerger, auditListener );
182 /* Tests for repository consumers */
184 public void testGetAllRepoConsumers()
187 recordRepoConsumers();
189 repoConsumerUtilsControl.replay();
190 knownContentConsumerControl.replay();
191 invalidContentConsumerControl.replay();
193 List<String> repoConsumers = service.getAllRepositoryConsumers();
195 repoConsumerUtilsControl.verify();
196 knownContentConsumerControl.verify();
197 invalidContentConsumerControl.verify();
199 assertNotNull( repoConsumers );
200 assertEquals( 4, repoConsumers.size() );
201 assertTrue( repoConsumers.contains( "index-artifact" ) );
202 assertTrue( repoConsumers.contains( "index-pom" ) );
203 assertTrue( repoConsumers.contains( "check-pom" ) );
204 assertTrue( repoConsumers.contains( "check-metadata" ) );
207 public void testConfigureValidRepositoryConsumer()
210 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
211 repoScanning.addKnownContentConsumer( "index-artifact" );
212 repoScanning.addKnownContentConsumer( "index-pom" );
213 repoScanning.addInvalidContentConsumer( "check-pom" );
215 // test enable "check-metadata" consumer
216 recordRepoConsumers();
218 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
219 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
221 config.setRepositoryScanning( repoScanning );
222 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
223 configControl.setVoidCallable();
225 archivaConfig.save( config );
226 archivaConfigControl.setVoidCallable();
228 repoConsumerUtilsControl.replay();
229 knownContentConsumerControl.replay();
230 invalidContentConsumerControl.replay();
231 archivaConfigControl.replay();
232 configControl.replay();
236 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
237 assertTrue( success );
239 catch ( Exception e )
241 fail( "An exception should not have been thrown." );
244 repoConsumerUtilsControl.verify();
245 knownContentConsumerControl.verify();
246 invalidContentConsumerControl.verify();
247 archivaConfigControl.verify();
248 configControl.verify();
250 // test disable "check-metadata" consumer
251 repoConsumerUtilsControl.reset();
252 knownContentConsumerControl.reset();
253 invalidContentConsumerControl.reset();
254 archivaConfigControl.reset();
255 configControl.reset();
257 repoScanning.addInvalidContentConsumer( "check-metadata" );
259 recordRepoConsumers();
261 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
262 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
264 config.setRepositoryScanning( repoScanning );
265 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
266 configControl.setVoidCallable();
268 archivaConfig.save( config );
269 archivaConfigControl.setVoidCallable();
271 repoConsumerUtilsControl.replay();
272 knownContentConsumerControl.replay();
273 invalidContentConsumerControl.replay();
274 archivaConfigControl.replay();
275 configControl.replay();
279 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
281 repoConsumerUtilsControl.verify();
282 knownContentConsumerControl.verify();
283 invalidContentConsumerControl.verify();
284 archivaConfigControl.verify();
285 configControl.verify();
287 assertTrue( success );
289 catch ( Exception e )
291 fail( "An excecption should not have been thrown." );
295 public void testConfigureInvalidRepositoryConsumer()
298 recordRepoConsumers();
300 repoConsumerUtilsControl.replay();
301 knownContentConsumerControl.replay();
302 invalidContentConsumerControl.replay();
306 service.configureRepositoryConsumer( null, "invalid-consumer", true );
307 fail( "An exception should have been thrown." );
309 catch ( Exception e )
311 assertEquals( "Invalid repository consumer.", e.getMessage() );
314 repoConsumerUtilsControl.verify();
315 knownContentConsumerControl.verify();
316 invalidContentConsumerControl.verify();
319 /* Tests for delete artifact */
321 public void testDeleteM2ArtifactArtifactExists()
324 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
326 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
327 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
329 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
330 repoContent.setRepository( managedRepo );
332 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
334 List<ArtifactMetadata> artifacts = getArtifacts();
335 ArtifactMetadata artifact = artifacts.get( 0 );
337 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( repoContent.getId(),
338 artifact.getNamespace(),
339 artifact.getProject(),
340 artifact.getVersion() ),
342 metadataRepository.removeArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
343 artifact.getVersion(), artifact.getId() );
345 listener.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
346 artifact.getVersion(), artifact.getId() );
347 listenerControl.setVoidCallable( 1 );
349 archivaConfigControl.replay();
350 configControl.replay();
351 repoFactoryControl.replay();
352 metadataRepositoryControl.replay();
353 listenerControl.replay();
355 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
356 assertTrue( success );
358 archivaConfigControl.verify();
359 configControl.verify();
360 repoFactoryControl.verify();
361 metadataRepositoryControl.verify();
362 listenerControl.verify();
364 assertFalse( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.0" ).exists() );
365 assertTrue( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.1" ).exists() );
368 public void testDeleteM1ArtifactArtifactExists()
371 MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
372 FileTypes fileTypes = (FileTypes) fileTypesControl.getMock();
374 MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
375 PathParser parser = (PathParser) pathParserControl.getMock();
377 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "legacy", "legacy-repo" );
379 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
380 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
382 ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
383 repoContent.setRepository( managedRepo );
384 repoContent.setFileTypes( fileTypes );
385 repoContent.setLegacyPathParser( parser );
387 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
389 recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
391 List<ArtifactMetadata> artifacts = getArtifacts();
392 ArtifactMetadata artifact = artifacts.get( 0 );
394 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( repoContent.getId(),
395 artifact.getNamespace(),
396 artifact.getProject(),
397 artifact.getVersion() ),
399 metadataRepository.removeArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
400 artifact.getVersion(), artifact.getId() );
402 listener.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
403 artifact.getVersion(), artifact.getId() );
404 listenerControl.setVoidCallable( 1 );
406 archivaConfigControl.replay();
407 configControl.replay();
408 repoFactoryControl.replay();
409 metadataRepositoryControl.replay();
410 listenerControl.replay();
411 fileTypesControl.replay();
412 pathParserControl.replay();
414 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
415 assertTrue( success );
417 archivaConfigControl.verify();
418 configControl.verify();
419 repoFactoryControl.verify();
420 metadataRepositoryControl.verify();
421 listenerControl.verify();
422 fileTypesControl.verify();
423 pathParserControl.verify();
425 File repo = new File( managedRepo.getLocation() );
426 assertFalse( new File( repo, "org.apache.archiva/jars/archiva-test-1.0.jar" ).exists() );
427 assertFalse( new File( repo, "org.apache.archiva/poms/archiva-test-1.0.pom" ).exists() );
429 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-test-1.1.jar" ).exists() );
430 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-diff-1.0.jar" ).exists() );
431 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-test-1.1.pom" ).exists() );
432 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-diff-1.0.pom" ).exists() );
435 public void testDeleteArtifactArtifactDoesNotExist()
438 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
440 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
441 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
443 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
444 repoContent.setRepository( managedRepo );
446 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
448 archivaConfigControl.replay();
449 configControl.replay();
450 repoFactoryControl.replay();
454 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
455 fail( "An exception should have been thrown." );
457 catch ( Exception e )
459 assertEquals( "Artifact does not exist.", e.getMessage() );
462 archivaConfigControl.verify();
463 configControl.verify();
464 repoFactoryControl.verify();
467 private ManagedRepositoryConfiguration createManagedRepo( String layout, String directory )
470 File srcDir = new File( getBasedir(), "src/test/repositories/" + directory );
472 File repoDir = getTestFile( "target/test-repos/" + directory );
474 FileUtils.deleteDirectory( repoDir );
476 FileUtils.copyDirectory( srcDir, repoDir, FileFilterUtils.makeSVNAware( null ) );
478 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "internal", layout, "Internal Repository", true,
480 managedRepo.setLocation( repoDir.getAbsolutePath() );
484 public void testDeleteArtifacRepositoryDoesNotExist()
487 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
488 configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
490 archivaConfigControl.replay();
491 configControl.replay();
495 service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
496 fail( "An exception should have been thrown." );
498 catch ( Exception e )
500 assertEquals( "Repository does not exist.", e.getMessage() );
503 archivaConfigControl.verify();
504 configControl.verify();
507 /* Tests for repository scanning */
509 public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
512 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
513 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), createManagedRepo( "internal",
515 "Internal Repository",
519 RepositoryTask task = new RepositoryTask();
521 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask(
522 "internal" ), false );
524 repositoryTaskScheduler.queueTask( task );
525 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
526 repositoryTaskSchedulerControl.setVoidCallable();
528 archivaConfigControl.replay();
529 configControl.replay();
530 repositoryTaskSchedulerControl.replay();
534 boolean success = service.executeRepositoryScanner( "internal" );
535 assertTrue( success );
537 catch ( Exception e )
539 fail( "An exception should not have been thrown." );
542 archivaConfigControl.verify();
543 configControl.verify();
544 repositoryTaskSchedulerControl.verify();
547 public void testExecuteRepoScannerRepoExistsButBeingScanned()
550 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
551 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), createManagedRepo( "internal",
553 "Internal Repository",
557 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask(
558 "internal" ), true );
560 archivaConfigControl.replay();
561 configControl.replay();
562 repositoryTaskSchedulerControl.replay();
566 boolean success = service.executeRepositoryScanner( "internal" );
567 assertFalse( success );
569 catch ( Exception e )
571 fail( "An exception should not have been thrown." );
574 archivaConfigControl.verify();
575 configControl.verify();
576 repositoryTaskSchedulerControl.verify();
579 public void testExecuteRepoScannerRepoDoesNotExist()
582 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
583 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
585 archivaConfigControl.replay();
586 configControl.replay();
590 service.executeRepositoryScanner( "internal" );
591 fail( "An exception should have been thrown." );
593 catch ( Exception e )
595 assertEquals( "Repository does not exist.", e.getMessage() );
598 archivaConfigControl.verify();
599 configControl.verify();
602 /* Tests for querying repositories */
604 public void testGetAllManagedRepositories()
607 List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();
608 managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
609 managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
611 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
612 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
614 archivaConfigControl.replay();
615 configControl.replay();
617 List<ManagedRepository> repos = service.getAllManagedRepositories();
619 archivaConfigControl.verify();
620 configControl.verify();
622 assertNotNull( repos );
623 assertEquals( 2, repos.size() );
625 assertManagedRepo( repos.get( 0 ), managedRepos.get( 0 ) );
626 assertManagedRepo( repos.get( 1 ), managedRepos.get( 1 ) );
629 public void testGetAllRemoteRepositories()
632 List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>();
633 remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default",
634 "http://repo1.maven.org/maven2" ) );
635 remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy",
636 "http://dummy.com/dummy" ) );
638 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
639 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
641 archivaConfigControl.replay();
642 configControl.replay();
644 List<RemoteRepository> repos = service.getAllRemoteRepositories();
646 archivaConfigControl.verify();
647 configControl.verify();
649 assertNotNull( repos );
650 assertEquals( 2, repos.size() );
652 assertRemoteRepo( repos.get( 0 ), remoteRepos.get( 0 ) );
653 assertRemoteRepo( repos.get( 1 ), remoteRepos.get( 1 ) );
658 public void testMergeRepositoryWithInvalidRepository()
661 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
662 configControl.expectAndReturn( config.findManagedRepositoryById( "invalid" ), null );
664 archivaConfigControl.replay();
665 configControl.replay();
669 service.merge( "invalid", true );
671 catch ( Exception e )
673 assertEquals( "Repository Id : invalid not found.", e.getMessage() );
676 archivaConfigControl.verify();
677 configControl.verify();
680 public void testMergeWithNoStagingRepository()
683 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
684 configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ), createManagedRepo( "repo", "default",
687 configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), null );
689 archivaConfigControl.replay();
690 configControl.replay();
694 service.merge( "repo", true );
696 catch ( Exception e )
698 assertEquals( "Staging Id : repo-stage not found.", e.getMessage() );
701 archivaConfigControl.verify();
702 configControl.verify();
705 public void testMergeRepositoriesAndScan()
708 List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
710 ArtifactMetadata artifact = new ArtifactMetadata();
711 artifact.setId( "artifact" );
712 artifact.setFileLastModified( System.currentTimeMillis() );
714 sources.add( artifact );
716 ManagedRepositoryConfiguration merge = createManagedRepo( "merge", "default", "merge", true, true );
717 merge.setLocation( "target/test-repository/merge" );
718 ManagedRepositoryConfiguration staging = createStagingRepo( merge );
720 RepositoryTask task = new RepositoryTask();
721 task.setScanAll( true );
723 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
724 configControl.expectAndReturn( config.findManagedRepositoryById( "merge" ), merge );
725 configControl.expectAndReturn( config.findManagedRepositoryById( "merge-stage" ), staging );
727 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
728 repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
731 repositoryMerger.merge( staging.getId(), merge.getId() );
732 repositoryMergerControl.setVoidCallable();
733 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "merge" ),
736 // scanning after merge
737 repositoryTaskScheduler.queueTask( task );
738 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
739 repositoryTaskSchedulerControl.setVoidCallable();
742 metadataRepository.addMetadataFacet( merge.getId(), createAuditEvent( merge ) );
743 metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
744 metadataRepositoryControl.setVoidCallable();
746 archivaConfigControl.replay();
747 metadataRepositoryControl.replay();
748 configControl.replay();
749 repositoryMergerControl.replay();
750 repositoryTaskSchedulerControl.replay();
752 boolean a = service.merge( "merge", false );
755 archivaConfigControl.verify();
756 configControl.verify();
757 configControl.verify();
758 metadataRepositoryControl.verify();
759 repositoryMergerControl.verify();
760 repositoryTaskSchedulerControl.verify();
763 public void testMergeRepositoriesWithConflictsAndScan()
766 List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
767 ArtifactMetadata one = new ArtifactMetadata();
769 one.setVersion( "1.0" );
771 ArtifactMetadata two = new ArtifactMetadata();
773 two.setVersion( "1.0-SNAPSHOT" );
778 List<ArtifactMetadata> conflicts = new ArrayList<ArtifactMetadata>();
779 conflicts.add( one );
781 sources.removeAll( conflicts );
783 Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sources );
785 RepositoryTask task = new RepositoryTask();
786 task.setScanAll( true );
788 ManagedRepositoryConfiguration repo = createManagedRepo( "repo", "default", "repo", true, true );
789 repo.setLocation( "target/test-repository/one" );
790 ManagedRepositoryConfiguration staging = createStagingRepo( repo );
792 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
793 configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ), repo );
794 configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), staging );
796 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
797 repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
800 repositoryMerger.merge( staging.getId(), repo.getId(), artifactsWithOutConflicts );
801 repositoryMergerControl.setMatcher( MockControl.ALWAYS_MATCHER );
802 repositoryMergerControl.setVoidCallable();
803 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "repo" ),
805 repositoryTaskScheduler.queueTask( task );
806 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
807 repositoryTaskSchedulerControl.setVoidCallable();
810 metadataRepository.addMetadataFacet( repo.getId(), createAuditEvent( repo ) );
811 metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
812 metadataRepositoryControl.setVoidCallable();
814 archivaConfigControl.replay();
815 metadataRepositoryControl.replay();
816 configControl.replay();
817 repositoryMergerControl.replay();
818 repositoryTaskSchedulerControl.replay();
820 boolean a = service.merge( "repo", true );
823 archivaConfigControl.verify();
824 configControl.verify();
825 configControl.verify();
826 metadataRepositoryControl.verify();
827 repositoryMergerControl.verify();
828 repositoryTaskSchedulerControl.verify();
831 public void testAddManagedRepository()
834 String projId = "org.apache.archiva";
835 String repoId = projId + ".releases";
836 String layout = "default";
837 String name = projId + " Releases";
838 String releaseLocation = "target/test-repository/" + projId + ".releases";
839 String stageLocation = releaseLocation + "-stage";
841 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "repo1", "default", "repo", true, false );
842 RemoteRepositoryConfiguration remoteRepo = createRemoteRepository( "central", "Central Repository", "default",
843 "http://repo1.maven.org/maven2" );
844 List<String> repositories = new ArrayList<String>();
845 repositories.add( managedRepo.getName() );
846 RepositoryGroupConfiguration repoGroup = createRepoGroupConfig( "repoGroup", repositories );
847 Map<String, ManagedRepositoryConfiguration> managedRepoMap =
848 new HashMap<String, ManagedRepositoryConfiguration>();
849 Map<String, RemoteRepositoryConfiguration> remoteRepoMap = new HashMap<String, RemoteRepositoryConfiguration>();
850 Map<String, RepositoryGroupConfiguration> repoGroupMap = new HashMap<String, RepositoryGroupConfiguration>();
851 managedRepoMap.put( "repo1", managedRepo );
852 remoteRepoMap.put( "repo1", remoteRepo );
853 repoGroupMap.put( "repo1", repoGroup );
855 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
857 configControl.expectAndReturn( config.getManagedRepositoriesAsMap(), managedRepoMap );
858 configControl.expectAndReturn( config.getRemoteRepositoriesAsMap(), remoteRepoMap );
859 configControl.expectAndReturn( config.getRepositoryGroupsAsMap(), repoGroupMap );
860 config.addManagedRepository( managedRepo );
861 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
862 configControl.setVoidCallable();
863 config.addManagedRepository( managedRepo );
864 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
865 configControl.setVoidCallable();
866 archivaConfig.save( config );
867 archivaConfigControl.setVoidCallable();
869 archivaConfigControl.replay();
870 configControl.replay();
871 assertFalse( new File( releaseLocation ).isDirectory() );
872 assertFalse( new File( stageLocation ).isDirectory() );
873 boolean success = service.addManagedRepository( repoId, layout, name, releaseLocation, true, true, false, true,
875 assertTrue( success );
876 assertTrue( new File( releaseLocation ).isDirectory() );
877 assertTrue( new File( stageLocation ).isDirectory() );
878 new File( releaseLocation ).delete();
879 new File( stageLocation ).delete();
882 /* private methods */
884 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
886 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
887 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
888 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
889 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
892 private RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String layout, String url )
894 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
895 remoteConfig.setId( id );
896 remoteConfig.setName( name );
897 remoteConfig.setLayout( layout );
898 remoteConfig.setUrl( url );
903 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
905 assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
906 assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
907 assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
909 // TODO enable assert once fixed in AdministrationServiceImpl!
910 // assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl()
912 assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
913 assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
916 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
917 boolean hasReleases, boolean hasSnapshots )
919 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
920 repoConfig.setId( id );
921 repoConfig.setLayout( layout );
922 repoConfig.setName( name );
923 repoConfig.setReleases( hasReleases );
924 repoConfig.setSnapshots( hasSnapshots );
929 private ManagedRepositoryConfiguration createStagingRepo( ManagedRepositoryConfiguration repoConfig )
931 ManagedRepositoryConfiguration stagingRepo = new ManagedRepositoryConfiguration();
932 stagingRepo.setId( repoConfig.getId() + STAGE );
933 stagingRepo.setLayout( repoConfig.getLayout() );
934 stagingRepo.setName( repoConfig + STAGE );
935 stagingRepo.setReleases( repoConfig.isReleases() );
936 stagingRepo.setSnapshots( repoConfig.isSnapshots() );
937 stagingRepo.setLocation( repoConfig.getLocation() );
942 private AuditEvent createAuditEvent( ManagedRepositoryConfiguration repoConfig )
944 AuditEvent auditEvent = new AuditEvent();
946 auditEvent.setAction( AuditEvent.MERGE_REPO_REMOTE );
947 auditEvent.setRepositoryId( repoConfig.getId() );
948 auditEvent.setResource( repoConfig.getLocation() );
949 auditEvent.setTimestamp( new Date() );
954 private void recordRepoConsumers()
956 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
957 availableKnownConsumers.add( indexArtifactConsumer );
958 availableKnownConsumers.add( indexPomConsumer );
960 List<InvalidRepositoryContentConsumer> availableInvalidConsumers =
961 new ArrayList<InvalidRepositoryContentConsumer>();
962 availableInvalidConsumers.add( checkPomConsumer );
963 availableInvalidConsumers.add( checkMetadataConsumer );
965 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(),
966 availableKnownConsumers );
967 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
968 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
970 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(),
971 availableInvalidConsumers );
972 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
973 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
976 private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
977 MockControl pathParserControl, PathParser parser )
978 throws LayoutException
980 String sep = File.separator;
981 String ad10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-diff-1.0.pom";
982 String at10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.0.pom";
983 String at11p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.1.pom";
984 String ad10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-diff-1.0.jar";
985 String at10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.0.jar";
986 String at11j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.1.jar";
988 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10p ), true );
989 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11p ), true );
990 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10p ), true );
991 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10j ), true );
992 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10j ), true );
993 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11j ), true );
995 ArtifactReference aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "pom" );
996 pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
998 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "pom" );
999 pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
1001 aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "pom" );
1002 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
1004 aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "jar" );
1005 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
1007 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "jar" );
1008 pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
1010 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "jar" );
1011 pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
1014 private List<ArtifactMetadata> getArtifacts()
1016 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1018 ArtifactMetadata artifact = new ArtifactMetadata();
1019 artifact.setId( "archiva-test-1.0.jar" );
1020 artifact.setProject( "archiva-test" );
1021 artifact.setVersion( "1.0" );
1022 artifact.setProjectVersion( "1.0" );
1023 artifact.setNamespace( "org.apache.archiva" );
1024 artifact.setRepositoryId( "internal" );
1025 artifacts.add( artifact );
1029 private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
1031 ArtifactReference aRef = new ArtifactReference();
1032 aRef.setArtifactId( artifactId );
1033 aRef.setGroupId( groupId );
1034 aRef.setType( type );
1035 aRef.setVersion( version );
1040 private RepositoryGroupConfiguration createRepoGroupConfig( String id, List<String> repositories )
1042 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
1043 repoGroup.setId( id );
1044 repoGroup.setRepositories( repositories );