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 junit.framework.TestCase;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.archiva.configuration.RepositoryScanningConfiguration;
27 import org.apache.archiva.metadata.model.ArtifactMetadata;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
30 import org.apache.archiva.metadata.repository.RepositorySession;
31 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.MockitoAnnotations;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
49 import static org.mockito.Mockito.*;
51 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
52 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-merge.xml" })
53 public class Maven2RepositoryMergerTest
57 private static final String TEST_REPO_ID = "test";
60 private Maven2RepositoryMerger repositoryMerger;
63 @Named("archivaConfiguration#default")
64 ArchivaConfiguration configuration;
66 private MetadataRepository metadataRepository;
68 private static RepositorySessionFactory repositorySessionFactory;
70 private static RepositorySession session;
74 repositorySessionFactory = mock(RepositorySessionFactory.class);
75 session = mock( RepositorySession.class );
79 when( repositorySessionFactory.createSession( ) ).thenReturn( session );
81 catch ( MetadataRepositoryException e )
83 throw new RuntimeException( e );
88 public static RepositorySessionFactory getRepositorySessionFactory() {
89 return repositorySessionFactory;
100 MockitoAnnotations.initMocks( this );
101 metadataRepository = mock( MetadataRepository.class );
102 repositoryMerger.setRepositorySessionFactory( repositorySessionFactory );
106 private List<ArtifactMetadata> getArtifacts()
108 List<ArtifactMetadata> metadata = new ArrayList<>();
109 ArtifactMetadata artifact1 = new ArtifactMetadata();
110 artifact1.setNamespace( "com.example.test" );
111 artifact1.setProject( "test-artifact" );
112 artifact1.setVersion( "1.0-SNAPSHOT" );
113 artifact1.setProjectVersion( "1.0-SNAPSHOT" );
114 artifact1.setId( "test-artifact-1.0-20100308.230825-1.jar" );
116 metadata.add( artifact1 );
121 public void testMerge()
124 String targetRepoPath = "target/test-repository-target";
125 Path mergedArtifact = Paths.get( targetRepoPath,
126 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
128 Path mavenMetadata = Paths.get( targetRepoPath, "com/example/test/test-artifact/maven-metadata.xml" );
130 Path pom = Paths.get( targetRepoPath,
131 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.pom" );
133 for (Path testArtifact : new Path[] { mergedArtifact, mavenMetadata, pom }) {
134 Files.deleteIfExists(testArtifact);
137 assertFalse( "Artifact file exists already", Files.exists(mergedArtifact) );
138 assertFalse( "Metadata file exists already", Files.exists(mavenMetadata) );
139 assertFalse( "Pom File exists already", Files.exists(pom) );
140 Configuration c = new Configuration();
141 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
142 testRepo.setId( TEST_REPO_ID );
143 testRepo.setLocation( "target/test-repository" );
145 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
146 List<String> knownContentConsumers = new ArrayList<>();
147 knownContentConsumers.add( "metadata-updater12" );
148 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
149 c.setRepositoryScanning( repoScanConfig );
151 ManagedRepositoryConfiguration targetRepo = new ManagedRepositoryConfiguration();
152 targetRepo.setId( "target-rep" );
153 targetRepo.setLocation( targetRepoPath );
154 c.addManagedRepository( testRepo );
155 c.addManagedRepository( targetRepo );
156 configuration.save( c );
159 when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(getArtifacts());
160 repositoryMerger.merge(metadataRepository, TEST_REPO_ID, "target-rep");
161 verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);
162 assertTrue( Files.exists(mergedArtifact) );
163 assertTrue( Files.exists(mavenMetadata) );
164 assertTrue( Files.exists(pom) );
168 public void testMergeWithOutConflictArtifacts()
171 String sourceRepoId = "source-repo";
172 ArtifactMetadata artifact1 = new ArtifactMetadata();
173 artifact1.setNamespace( "org.testng" );
174 artifact1.setProject( "testng" );
175 artifact1.setVersion( "5.8" );
176 artifact1.setProjectVersion( "5.8" );
177 artifact1.setId( "testng-5.8-jdk15.jar" );
178 artifact1.setRepositoryId( sourceRepoId );
180 List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
181 sourceRepoArtifactsList.add( artifact1 );
182 List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
184 Configuration c = new Configuration();
185 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
186 testRepo.setId( TEST_REPO_ID );
187 testRepo.setLocation( "target/test-repository" );
189 String sourceRepo = "src/test/resources/test-repository-with-conflict-artifacts";
190 ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
191 testRepoWithConflicts.setId( sourceRepoId );
192 testRepoWithConflicts.setLocation( sourceRepo );
194 RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
195 List<String> knownContentConsumers = new ArrayList<>();
196 knownContentConsumers.add( "metadata-updater" );
197 repoScanConfig.setKnownContentConsumers( knownContentConsumers );
198 c.setRepositoryScanning( repoScanConfig );
200 c.addManagedRepository( testRepo );
201 c.addManagedRepository( testRepoWithConflicts );
202 configuration.save( c );
204 Path targetRepoFile = Paths.get(
205 "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
206 targetRepoFile.toFile().setReadOnly();
208 when(metadataRepository.getArtifacts(session, sourceRepoId)).thenReturn(sourceRepoArtifactsList);
209 when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(targetRepoArtifactsList);
211 assertEquals(1, repositoryMerger.getConflictingArtifacts(metadataRepository, sourceRepoId,
212 TEST_REPO_ID).size());
213 verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);