1 package org.apache.archiva.repository.maven.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.Configuration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RepositoryScanningConfiguration;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoAnnotations;
36 import org.springframework.test.context.ContextConfiguration;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.ArrayList;
44 import java.util.List;
46 import static org.mockito.Mockito.*;
48 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
49 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-merge.xml" })
50 public class Maven2RepositoryMergerTest
54 private static final String TEST_REPO_ID = "test";
57 private Maven2RepositoryMerger repositoryMerger;
60 @Named("archivaConfiguration#default")
61 ArchivaConfiguration configuration;
63 private MetadataRepository metadataRepository;
65 private static RepositorySessionFactory repositorySessionFactory;
67 private static RepositorySession session;
71 repositorySessionFactory = mock(RepositorySessionFactory.class);
72 session = mock( RepositorySession.class );
76 when( repositorySessionFactory.createSession( ) ).thenReturn( session );
78 catch ( MetadataRepositoryException e )
80 throw new RuntimeException( e );
85 public static RepositorySessionFactory getRepositorySessionFactory() {
86 return repositorySessionFactory;
97 MockitoAnnotations.initMocks( this );
98 metadataRepository = mock( MetadataRepository.class );
99 repositoryMerger.setRepositorySessionFactory( repositorySessionFactory );
103 private List<ArtifactMetadata> getArtifacts()
105 List<ArtifactMetadata> metadata = new ArrayList<>();
106 ArtifactMetadata artifact1 = new ArtifactMetadata();
107 artifact1.setNamespace( "com.example.test" );
108 artifact1.setProject( "test-artifact" );
109 artifact1.setVersion( "1.0-SNAPSHOT" );
110 artifact1.setProjectVersion( "1.0-SNAPSHOT" );
111 artifact1.setId( "test-artifact-1.0-20100308.230825-1.jar" );
113 metadata.add( artifact1 );
118 public void testMerge()
121 String targetRepoPath = "target/test-repository-target";
122 Path mergedArtifact = Paths.get( targetRepoPath,
123 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
125 Path mavenMetadata = Paths.get( targetRepoPath, "com/example/test/test-artifact/maven-metadata.xml" );
127 Path pom = Paths.get( targetRepoPath,
128 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.pom" );
130 for (Path testArtifact : new Path[] { mergedArtifact, mavenMetadata, pom }) {
131 Files.deleteIfExists(testArtifact);
134 assertFalse( "Artifact file exists already", Files.exists(mergedArtifact) );
135 assertFalse( "Metadata file exists already", Files.exists(mavenMetadata) );
136 assertFalse( "Pom File exists already", Files.exists(pom) );
137 Configuration c = new Configuration();
138 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
139 testRepo.setId( TEST_REPO_ID );
140 testRepo.setLocation( "target/test-repository" );
142 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
143 List<String> knownContentConsumers = new ArrayList<>();
144 knownContentConsumers.add( "metadata-updater12" );
145 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
146 c.setRepositoryScanning( repoScanConfig );
148 ManagedRepositoryConfiguration targetRepo = new ManagedRepositoryConfiguration();
149 targetRepo.setId( "target-rep" );
150 targetRepo.setLocation( targetRepoPath );
151 c.addManagedRepository( testRepo );
152 c.addManagedRepository( targetRepo );
153 configuration.save( c );
156 when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(getArtifacts());
157 repositoryMerger.merge(metadataRepository, TEST_REPO_ID, "target-rep");
158 verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);
159 assertTrue( Files.exists(mergedArtifact) );
160 assertTrue( Files.exists(mavenMetadata) );
161 assertTrue( Files.exists(pom) );
165 public void testMergeWithOutConflictArtifacts()
168 String sourceRepoId = "source-repo";
169 ArtifactMetadata artifact1 = new ArtifactMetadata();
170 artifact1.setNamespace( "org.testng" );
171 artifact1.setProject( "testng" );
172 artifact1.setVersion( "5.8" );
173 artifact1.setProjectVersion( "5.8" );
174 artifact1.setId( "testng-5.8-jdk15.jar" );
175 artifact1.setRepositoryId( sourceRepoId );
177 List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
178 sourceRepoArtifactsList.add( artifact1 );
179 List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
181 Configuration c = new Configuration();
182 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
183 testRepo.setId( TEST_REPO_ID );
184 testRepo.setLocation( "target/test-repository" );
186 String sourceRepo = "src/test/resources/test-repository-with-conflict-artifacts";
187 ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
188 testRepoWithConflicts.setId( sourceRepoId );
189 testRepoWithConflicts.setLocation( sourceRepo );
191 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
192 List<String> knownContentConsumers = new ArrayList<>();
193 knownContentConsumers.add( "metadata-updater" );
194 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
195 c.setRepositoryScanning( repoScanConfig );
197 c.addManagedRepository( testRepo );
198 c.addManagedRepository( testRepoWithConflicts );
199 configuration.save( c );
201 Path targetRepoFile = Paths.get(
202 "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
203 targetRepoFile.toFile().setReadOnly();
205 when(metadataRepository.getArtifacts(session, sourceRepoId)).thenReturn(sourceRepoArtifactsList);
206 when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(targetRepoArtifactsList);
208 assertEquals(1, repositoryMerger.getConflictingArtifacts(metadataRepository, sourceRepoId,
209 TEST_REPO_ID).size());
210 verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);