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.Date;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.apache.archiva.audit.AuditEvent;
32 import org.apache.archiva.audit.AuditListener;
33 import org.apache.archiva.metadata.model.ArtifactMetadata;
34 import org.apache.archiva.metadata.repository.MetadataRepository;
35 import org.apache.archiva.metadata.repository.filter.Filter;
36 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
37 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
38 import org.apache.archiva.repository.events.RepositoryListener;
39 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
40 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
41 import org.apache.archiva.scheduler.repository.RepositoryTask;
42 import org.apache.archiva.stagerepository.merge.RepositoryMerger;
43 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
44 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
45 import org.apache.commons.io.FileUtils;
46 import org.apache.commons.io.filefilter.FileFilterUtils;
47 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
48 import org.apache.maven.archiva.configuration.Configuration;
49 import org.apache.maven.archiva.configuration.FileTypes;
50 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
51 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
52 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
53 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
54 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
55 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
56 import org.apache.maven.archiva.model.ArtifactReference;
57 import org.apache.maven.archiva.repository.RepositoryContentFactory;
58 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
59 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
60 import org.apache.maven.archiva.repository.content.PathParser;
61 import org.apache.maven.archiva.repository.layout.LayoutException;
62 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
63 import org.easymock.MockControl;
64 import org.easymock.classextension.MockClassControl;
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();
178 new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory, metadataRepository,
179 repositoryTaskScheduler, Collections.singletonList( listener ),
180 repositoryStatisticsManager, repositoryMerger, auditListener );
183 /* Tests for repository consumers */
185 public void testGetAllRepoConsumers()
188 recordRepoConsumers();
190 repoConsumerUtilsControl.replay();
191 knownContentConsumerControl.replay();
192 invalidContentConsumerControl.replay();
194 List<String> repoConsumers = service.getAllRepositoryConsumers();
196 repoConsumerUtilsControl.verify();
197 knownContentConsumerControl.verify();
198 invalidContentConsumerControl.verify();
200 assertNotNull( repoConsumers );
201 assertEquals( 4, repoConsumers.size() );
202 assertTrue( repoConsumers.contains( "index-artifact" ) );
203 assertTrue( repoConsumers.contains( "index-pom" ) );
204 assertTrue( repoConsumers.contains( "check-pom" ) );
205 assertTrue( repoConsumers.contains( "check-metadata" ) );
208 public void testConfigureValidRepositoryConsumer()
211 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
212 repoScanning.addKnownContentConsumer( "index-artifact" );
213 repoScanning.addKnownContentConsumer( "index-pom" );
214 repoScanning.addInvalidContentConsumer( "check-pom" );
216 // test enable "check-metadata" consumer
217 recordRepoConsumers();
219 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
220 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
222 config.setRepositoryScanning( repoScanning );
223 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
224 configControl.setVoidCallable();
226 archivaConfig.save( config );
227 archivaConfigControl.setVoidCallable();
229 repoConsumerUtilsControl.replay();
230 knownContentConsumerControl.replay();
231 invalidContentConsumerControl.replay();
232 archivaConfigControl.replay();
233 configControl.replay();
237 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
238 assertTrue( success );
240 catch ( Exception e )
242 fail( "An exception should not have been thrown." );
245 repoConsumerUtilsControl.verify();
246 knownContentConsumerControl.verify();
247 invalidContentConsumerControl.verify();
248 archivaConfigControl.verify();
249 configControl.verify();
251 // test disable "check-metadata" consumer
252 repoConsumerUtilsControl.reset();
253 knownContentConsumerControl.reset();
254 invalidContentConsumerControl.reset();
255 archivaConfigControl.reset();
256 configControl.reset();
258 repoScanning.addInvalidContentConsumer( "check-metadata" );
260 recordRepoConsumers();
262 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
263 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
265 config.setRepositoryScanning( repoScanning );
266 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
267 configControl.setVoidCallable();
269 archivaConfig.save( config );
270 archivaConfigControl.setVoidCallable();
272 repoConsumerUtilsControl.replay();
273 knownContentConsumerControl.replay();
274 invalidContentConsumerControl.replay();
275 archivaConfigControl.replay();
276 configControl.replay();
280 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
282 repoConsumerUtilsControl.verify();
283 knownContentConsumerControl.verify();
284 invalidContentConsumerControl.verify();
285 archivaConfigControl.verify();
286 configControl.verify();
288 assertTrue( success );
290 catch ( Exception e )
292 fail( "An excecption should not have been thrown." );
296 public void testConfigureInvalidRepositoryConsumer()
299 recordRepoConsumers();
301 repoConsumerUtilsControl.replay();
302 knownContentConsumerControl.replay();
303 invalidContentConsumerControl.replay();
307 service.configureRepositoryConsumer( null, "invalid-consumer", true );
308 fail( "An exception should have been thrown." );
310 catch ( Exception e )
312 assertEquals( "Invalid repository consumer.", e.getMessage() );
315 repoConsumerUtilsControl.verify();
316 knownContentConsumerControl.verify();
317 invalidContentConsumerControl.verify();
320 /* Tests for delete artifact */
322 public void testDeleteM2ArtifactArtifactExists()
325 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
327 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
328 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
330 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
331 repoContent.setRepository( managedRepo );
333 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
335 List<ArtifactMetadata> artifacts = getArtifacts();
336 ArtifactMetadata artifact = artifacts.get( 0 );
338 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( repoContent.getId(),
339 artifact.getNamespace(),
340 artifact.getProject(),
341 artifact.getVersion() ), artifacts );
342 metadataRepository.deleteArtifact( 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() ), artifacts );
398 metadataRepository.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
399 artifact.getVersion(), artifact.getId() );
401 listener.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
402 artifact.getVersion(), artifact.getId() );
403 listenerControl.setVoidCallable( 1 );
405 archivaConfigControl.replay();
406 configControl.replay();
407 repoFactoryControl.replay();
408 metadataRepositoryControl.replay();
409 listenerControl.replay();
410 fileTypesControl.replay();
411 pathParserControl.replay();
413 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
414 assertTrue( success );
416 archivaConfigControl.verify();
417 configControl.verify();
418 repoFactoryControl.verify();
419 metadataRepositoryControl.verify();
420 listenerControl.verify();
421 fileTypesControl.verify();
422 pathParserControl.verify();
424 File repo = new File( managedRepo.getLocation() );
425 assertFalse( new File( repo, "org.apache.archiva/jars/archiva-test-1.0.jar" ).exists() );
426 assertFalse( new File( repo, "org.apache.archiva/poms/archiva-test-1.0.pom" ).exists() );
428 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-test-1.1.jar" ).exists() );
429 assertTrue( new File( repo, "org.apache.archiva/jars/archiva-diff-1.0.jar" ).exists() );
430 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-test-1.1.pom" ).exists() );
431 assertTrue( new File( repo, "org.apache.archiva/poms/archiva-diff-1.0.pom" ).exists() );
434 public void testDeleteArtifactArtifactDoesNotExist()
437 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
439 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
440 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
442 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
443 repoContent.setRepository( managedRepo );
445 repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
447 archivaConfigControl.replay();
448 configControl.replay();
449 repoFactoryControl.replay();
453 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
454 fail( "An exception should have been thrown." );
456 catch ( Exception e )
458 assertEquals( "Artifact does not exist.", e.getMessage() );
461 archivaConfigControl.verify();
462 configControl.verify();
463 repoFactoryControl.verify();
466 private ManagedRepositoryConfiguration createManagedRepo( String layout, String directory )
469 File srcDir = new File( getBasedir(), "src/test/repositories/" + directory );
471 File repoDir = getTestFile( "target/test-repos/" + directory );
473 FileUtils.deleteDirectory( repoDir );
475 FileUtils.copyDirectory( srcDir, repoDir, FileFilterUtils.makeSVNAware( null ) );
477 ManagedRepositoryConfiguration managedRepo =
478 createManagedRepo( "internal", layout, "Internal Repository", true, false );
479 managedRepo.setLocation( repoDir.getAbsolutePath() );
483 public void testDeleteArtifacRepositoryDoesNotExist()
486 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
487 configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
489 archivaConfigControl.replay();
490 configControl.replay();
494 service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
495 fail( "An exception should have been thrown." );
497 catch ( Exception e )
499 assertEquals( "Repository does not exist.", e.getMessage() );
502 archivaConfigControl.verify();
503 configControl.verify();
506 /* Tests for repository scanning */
508 public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
511 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
512 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
513 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
515 RepositoryTask task = new RepositoryTask();
517 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "internal" ),
520 repositoryTaskScheduler.queueTask( task );
521 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
522 repositoryTaskSchedulerControl.setVoidCallable();
524 archivaConfigControl.replay();
525 configControl.replay();
526 repositoryTaskSchedulerControl.replay();
530 boolean success = service.executeRepositoryScanner( "internal" );
531 assertTrue( success );
533 catch ( Exception e )
535 fail( "An exception should not have been thrown." );
538 archivaConfigControl.verify();
539 configControl.verify();
540 repositoryTaskSchedulerControl.verify();
543 public void testExecuteRepoScannerRepoExistsButBeingScanned()
546 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
547 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
548 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
550 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "internal" ),
553 archivaConfigControl.replay();
554 configControl.replay();
555 repositoryTaskSchedulerControl.replay();
559 boolean success = service.executeRepositoryScanner( "internal" );
560 assertFalse( success );
562 catch ( Exception e )
564 fail( "An exception should not have been thrown." );
567 archivaConfigControl.verify();
568 configControl.verify();
569 repositoryTaskSchedulerControl.verify();
572 public void testExecuteRepoScannerRepoDoesNotExist()
575 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
576 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
578 archivaConfigControl.replay();
579 configControl.replay();
583 service.executeRepositoryScanner( "internal" );
584 fail( "An exception should have been thrown." );
586 catch ( Exception e )
588 assertEquals( "Repository does not exist.", e.getMessage() );
591 archivaConfigControl.verify();
592 configControl.verify();
595 /* Tests for querying repositories */
597 public void testGetAllManagedRepositories()
600 List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();
601 managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
602 managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
604 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
605 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
607 archivaConfigControl.replay();
608 configControl.replay();
610 List<ManagedRepository> repos = service.getAllManagedRepositories();
612 archivaConfigControl.verify();
613 configControl.verify();
615 assertNotNull( repos );
616 assertEquals( 2, repos.size() );
618 assertManagedRepo( repos.get( 0 ), managedRepos.get( 0 ) );
619 assertManagedRepo( repos.get( 1 ), managedRepos.get( 1 ) );
622 public void testGetAllRemoteRepositories()
625 List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>();
626 remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default",
627 "http://repo1.maven.org/maven2" ) );
628 remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy" ) );
630 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
631 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
633 archivaConfigControl.replay();
634 configControl.replay();
636 List<RemoteRepository> repos = service.getAllRemoteRepositories();
638 archivaConfigControl.verify();
639 configControl.verify();
641 assertNotNull( repos );
642 assertEquals( 2, repos.size() );
644 assertRemoteRepo( repos.get( 0 ), remoteRepos.get( 0 ) );
645 assertRemoteRepo( repos.get( 1 ), remoteRepos.get( 1 ) );
650 public void testMergeRepositoryWithInvalidRepository()
653 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
654 configControl.expectAndReturn( config.findManagedRepositoryById( "invalid" ), null );
656 archivaConfigControl.replay();
657 configControl.replay();
661 service.merge( "invalid", true );
663 catch ( Exception e )
665 assertEquals( "Repository Id : invalid not found.", e.getMessage() );
668 archivaConfigControl.verify();
669 configControl.verify();
672 public void testMergeWithNoStagingRepository()
675 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
676 configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ),
677 createManagedRepo( "repo", "default", "repo", true, false ) );
678 configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), null );
680 archivaConfigControl.replay();
681 configControl.replay();
685 service.merge( "repo", true );
687 catch ( Exception e )
689 assertEquals( "Staging Id : repo-stage not found.", e.getMessage() );
692 archivaConfigControl.verify();
693 configControl.verify();
696 public void testMergeRepositoriesAndScan()
699 List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
701 ArtifactMetadata artifact = new ArtifactMetadata();
702 artifact.setId( "artifact" );
703 artifact.setFileLastModified( System.currentTimeMillis() );
705 sources.add( artifact );
707 ManagedRepositoryConfiguration merge = createManagedRepo( "merge", "default", "merge", true, true );
708 merge.setLocation( "target/test-repository/merge" );
709 ManagedRepositoryConfiguration staging = createStagingRepo( merge );
711 RepositoryTask task = new RepositoryTask();
712 task.setScanAll( true );
714 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
715 configControl.expectAndReturn( config.findManagedRepositoryById( "merge" ), merge );
716 configControl.expectAndReturn( config.findManagedRepositoryById( "merge-stage" ), staging );
718 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
719 repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
722 repositoryMerger.merge( staging.getId(), merge.getId() );
723 repositoryMergerControl.setMatcher( MockControl.ALWAYS_MATCHER );
724 repositoryMergerControl.setVoidCallable();
725 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "merge" ),
728 // scanning after merge
729 repositoryTaskScheduler.queueTask( task );
730 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
731 repositoryTaskSchedulerControl.setVoidCallable();
734 metadataRepository.addMetadataFacet( merge.getId(), createAuditEvent( merge ) );
735 metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
736 metadataRepositoryControl.setVoidCallable();
738 archivaConfigControl.replay();
739 metadataRepositoryControl.replay();
740 configControl.replay();
741 repositoryMergerControl.replay();
742 repositoryTaskSchedulerControl.replay();
744 boolean a = service.merge( "merge", false );
747 archivaConfigControl.verify();
748 configControl.verify();
749 configControl.verify();
750 metadataRepositoryControl.verify();
751 repositoryMergerControl.verify();
752 repositoryTaskSchedulerControl.verify();
755 public void testMergeRepositoriesWithConflictsAndScan()
758 List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
759 ArtifactMetadata one = new ArtifactMetadata();
761 one.setVersion( "1.0" );
763 ArtifactMetadata two = new ArtifactMetadata();
765 two.setVersion( "1.0-SNAPSHOT" );
770 List<ArtifactMetadata> conflicts = new ArrayList<ArtifactMetadata>();
771 conflicts.add( one );
773 sources.removeAll( conflicts );
775 Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sources );
777 RepositoryTask task = new RepositoryTask();
778 task.setScanAll( true );
780 ManagedRepositoryConfiguration repo = createManagedRepo( "repo", "default", "repo", true, true );
781 repo.setLocation( "target/test-repository/one" );
782 ManagedRepositoryConfiguration staging = createStagingRepo( repo );
784 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
785 configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ), repo );
786 configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), staging );
788 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
789 repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
792 repositoryMerger.merge( staging.getId(), repo.getId(), artifactsWithOutConflicts );
793 repositoryMergerControl.setMatcher( MockControl.ALWAYS_MATCHER );
794 repositoryMergerControl.setVoidCallable();
795 repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "repo" ),
797 repositoryTaskScheduler.queueTask( task );
798 repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
799 repositoryTaskSchedulerControl.setVoidCallable();
802 metadataRepository.addMetadataFacet( repo.getId(), createAuditEvent( repo ) );
803 metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
804 metadataRepositoryControl.setVoidCallable();
806 archivaConfigControl.replay();
807 metadataRepositoryControl.replay();
808 configControl.replay();
809 repositoryMergerControl.replay();
810 repositoryTaskSchedulerControl.replay();
812 boolean a = service.merge( "repo", true );
815 archivaConfigControl.verify();
816 configControl.verify();
817 configControl.verify();
818 metadataRepositoryControl.verify();
819 repositoryMergerControl.verify();
820 repositoryTaskSchedulerControl.verify();
823 public void testAddManagedRepository()
826 String projId = "org.apache.archiva";
827 String repoId = projId + ".releases";
828 String layout = "default";
829 String name = projId + " Releases";
830 String releaseLocation = "target/test-repository/" + projId + ".releases";
831 String stageLocation = releaseLocation + "-stage";
833 ManagedRepositoryConfiguration managedRepo = createManagedRepo( "repo1", "default", "repo", true, false );
834 RemoteRepositoryConfiguration remoteRepo =
835 createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2" );
836 List<String> repositories = new ArrayList<String>();
837 repositories.add( managedRepo.getName() );
838 RepositoryGroupConfiguration repoGroup = createRepoGroupConfig( "repoGroup", repositories );
839 Map<String, ManagedRepositoryConfiguration> managedRepoMap =
840 new HashMap<String, ManagedRepositoryConfiguration>();
841 Map<String, RemoteRepositoryConfiguration> remoteRepoMap = new HashMap<String, RemoteRepositoryConfiguration>();
842 Map<String, RepositoryGroupConfiguration> repoGroupMap = new HashMap<String, RepositoryGroupConfiguration>();
843 managedRepoMap.put( "repo1", managedRepo );
844 remoteRepoMap.put( "repo1", remoteRepo );
845 repoGroupMap.put( "repo1", repoGroup );
847 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
849 configControl.expectAndReturn( config.getManagedRepositoriesAsMap(), managedRepoMap );
850 configControl.expectAndReturn( config.getRemoteRepositoriesAsMap(), remoteRepoMap );
851 configControl.expectAndReturn( config.getRepositoryGroupsAsMap(), repoGroupMap );
852 config.addManagedRepository( managedRepo );
853 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
854 configControl.setVoidCallable();
855 config.addManagedRepository( managedRepo );
856 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
857 configControl.setVoidCallable();
858 archivaConfig.save( config );
859 archivaConfigControl.setVoidCallable();
861 archivaConfigControl.replay();
862 configControl.replay();
863 assertFalse( new File( releaseLocation ).isDirectory() );
864 assertFalse( new File( stageLocation ).isDirectory() );
866 service.addManagedRepository( repoId, layout, name, releaseLocation, true, true, false, true,
868 assertTrue( success );
869 assertTrue( new File( releaseLocation ).isDirectory() );
870 assertTrue( new File( stageLocation ).isDirectory() );
871 new File( releaseLocation ).delete();
872 new File( stageLocation ).delete();
875 /* private methods */
877 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
879 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
880 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
881 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
882 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
885 private RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String layout, String url )
887 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
888 remoteConfig.setId( id );
889 remoteConfig.setName( name );
890 remoteConfig.setLayout( layout );
891 remoteConfig.setUrl( url );
896 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
898 assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
899 assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
900 assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
902 // TODO enable assert once fixed in AdministrationServiceImpl!
903 // assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl()
905 assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
906 assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
909 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
910 boolean hasReleases, boolean hasSnapshots )
912 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
913 repoConfig.setId( id );
914 repoConfig.setLayout( layout );
915 repoConfig.setName( name );
916 repoConfig.setReleases( hasReleases );
917 repoConfig.setSnapshots( hasSnapshots );
922 private ManagedRepositoryConfiguration createStagingRepo( ManagedRepositoryConfiguration repoConfig )
924 ManagedRepositoryConfiguration stagingRepo = new ManagedRepositoryConfiguration();
925 stagingRepo.setId( repoConfig.getId() + STAGE );
926 stagingRepo.setLayout( repoConfig.getLayout() );
927 stagingRepo.setName( repoConfig + STAGE );
928 stagingRepo.setReleases( repoConfig.isReleases() );
929 stagingRepo.setSnapshots( repoConfig.isSnapshots() );
930 stagingRepo.setLocation( repoConfig.getLocation() );
935 private AuditEvent createAuditEvent( ManagedRepositoryConfiguration repoConfig )
937 AuditEvent auditEvent = new AuditEvent();
939 auditEvent.setAction( AuditEvent.MERGE_REPO_REMOTE );
940 auditEvent.setRepositoryId( repoConfig.getId() );
941 auditEvent.setResource( repoConfig.getLocation() );
942 auditEvent.setTimestamp( new Date() );
947 private void recordRepoConsumers()
949 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
950 availableKnownConsumers.add( indexArtifactConsumer );
951 availableKnownConsumers.add( indexPomConsumer );
953 List<InvalidRepositoryContentConsumer> availableInvalidConsumers =
954 new ArrayList<InvalidRepositoryContentConsumer>();
955 availableInvalidConsumers.add( checkPomConsumer );
956 availableInvalidConsumers.add( checkMetadataConsumer );
958 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(),
959 availableKnownConsumers );
960 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
961 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
963 repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(),
964 availableInvalidConsumers );
965 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
966 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
969 private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
970 MockControl pathParserControl, PathParser parser )
971 throws LayoutException
973 String sep = File.separator;
974 String ad10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-diff-1.0.pom";
975 String at10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.0.pom";
976 String at11p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.1.pom";
977 String ad10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-diff-1.0.jar";
978 String at10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.0.jar";
979 String at11j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.1.jar";
981 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10p ), true );
982 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11p ), true );
983 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10p ), true );
984 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10j ), true );
985 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10j ), true );
986 fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11j ), true );
988 ArtifactReference aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "pom" );
989 pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
991 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "pom" );
992 pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
994 aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "pom" );
995 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
997 aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "jar" );
998 pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
1000 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "jar" );
1001 pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
1003 aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "jar" );
1004 pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
1007 private List<ArtifactMetadata> getArtifacts()
1009 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1011 ArtifactMetadata artifact = new ArtifactMetadata();
1012 artifact.setId( "archiva-test-1.0.jar" );
1013 artifact.setProject( "archiva-test" );
1014 artifact.setVersion( "1.0" );
1015 artifact.setProjectVersion( "1.0" );
1016 artifact.setNamespace( "org.apache.archiva" );
1017 artifact.setRepositoryId( "internal" );
1018 artifacts.add( artifact );
1022 private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
1024 ArtifactReference aRef = new ArtifactReference();
1025 aRef.setArtifactId( artifactId );
1026 aRef.setGroupId( groupId );
1027 aRef.setType( type );
1028 aRef.setVersion( version );
1033 private RepositoryGroupConfiguration createRepoGroupConfig( String id, List<String> repositories )
1035 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
1036 repoGroup.setId( id );
1037 repoGroup.setRepositories( repositories );