1 package org.apache.archiva.stagerepository.merge;
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.codehaus.plexus.spring.PlexusInSpringTestCase;
23 import org.apache.maven.archiva.configuration.Configuration;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
27 import org.apache.maven.archiva.repository.RepositoryContentFactory;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33 import static org.mockito.Mockito.verify;
34 import org.junit.Before;
36 import java.util.List;
37 import java.util.ArrayList;
40 public class Maven2RepositoryMergerTest
41 extends PlexusInSpringTestCase
44 private static final String TEST_REPO_ID = "test";
46 private static final String TARGET_REPOSITORY_ID = "target-repo";
48 private Configuration config;
50 @MockitoAnnotations.Mock
51 private MetadataRepository metadataResolver;
53 private RepositoryContentFactory repositoryFactory;
55 private ArchivaConfiguration configuration;
57 private Maven2RepositoryMerger repositoryMerger;
59 private MetadataRepository metadataRepository;
66 MockitoAnnotations.initMocks( this );
67 metadataRepository = mock( MetadataRepository.class );
68 repositoryMerger = (Maven2RepositoryMerger) lookup( RepositoryMerger.class, "maven2" );
69 repositoryMerger.setMetadataRepository( metadataRepository );
72 private List<ArtifactMetadata> getArtifacts()
74 List<ArtifactMetadata> metadata = new ArrayList<ArtifactMetadata>();
75 ArtifactMetadata artifact1 = new ArtifactMetadata();
76 artifact1.setNamespace( "com.example.test" );
77 artifact1.setProject( "test-artifact" );
78 artifact1.setVersion( "1.0-SNAPSHOT" );
79 artifact1.setProjectVersion( "1.0-SNAPSHOT" );
80 artifact1.setId( "test-artifact-1.0-20100308.230825-1.jar" );
82 metadata.add( artifact1 );
86 public void testMerge()
89 ArchivaConfiguration configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
90 Configuration c = new Configuration();
91 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
92 testRepo.setId( TEST_REPO_ID );
93 testRepo.setLocation( getTestPath( "target/test-repository" ) );
95 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
96 List<String> knownContentConsumers = new ArrayList<String>();
97 knownContentConsumers.add( "metadata-updater12" );
98 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
99 c.setRepositoryScanning( repoScanConfig );
101 ManagedRepositoryConfiguration targetRepo = new ManagedRepositoryConfiguration();
102 targetRepo.setId( "target-rep" );
103 targetRepo.setLocation( getTestPath( "target" ) );
104 c.addManagedRepository( testRepo );
105 c.addManagedRepository( targetRepo );
106 configuration.save( c );
108 when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( getArtifacts() );
109 repositoryMerger.merge( TEST_REPO_ID, "target-rep" );
110 verify( metadataRepository ).getArtifacts( TEST_REPO_ID );
113 public void testMergeWithOutConflictArtifacts()
116 String sourceRepoId = "source-repo";
117 ArtifactMetadata artifact1 = new ArtifactMetadata();
118 artifact1.setNamespace( "org.testng" );
119 artifact1.setProject( "testng" );
120 artifact1.setVersion( "5.8" );
121 artifact1.setProjectVersion( "5.8" );
122 artifact1.setId( "testng-5.8-jdk15.jar" );
123 artifact1.setRepositoryId( sourceRepoId );
125 List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
126 sourceRepoArtifactsList.add( artifact1 );
127 List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
129 ArchivaConfiguration configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
130 Configuration c = new Configuration();
131 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
132 testRepo.setId( TEST_REPO_ID );
133 testRepo.setLocation( getTestPath( "target/test-repository" ) );
135 String sourceRepo = "src/test/resources/test-repository-with-conflict-artifacts";
136 ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
137 testRepoWithConflicts.setId( sourceRepoId );
138 testRepoWithConflicts.setLocation( getTestPath( sourceRepo ) );
140 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
141 List<String> knownContentConsumers = new ArrayList<String>();
142 knownContentConsumers.add( "metadata-updater" );
143 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
144 c.setRepositoryScanning( repoScanConfig );
146 c.addManagedRepository( testRepo );
147 c.addManagedRepository( testRepoWithConflicts );
148 configuration.save( c );
150 File targetRepoFile = new File( getTestPath(
151 "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" ) );
152 targetRepoFile.setReadOnly();
154 when( metadataRepository.getArtifacts( sourceRepoId ) ).thenReturn( sourceRepoArtifactsList );
155 when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( targetRepoArtifactsList );
157 assertEquals( 1, repositoryMerger.getConflictsartifacts( sourceRepoId, TEST_REPO_ID ).size() );
158 verify( metadataRepository ).getArtifacts( TEST_REPO_ID );