]> source.dussan.org Git - archiva.git/blob
fe17790325a55e128c3bfa2f961772c6c8d8aacc
[archiva.git] /
1 package org.apache.archiva.repository.maven.merge;
2
3 /*
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
11  *
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
18  * under the License.
19  */
20
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;
37
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;
45
46 import static org.mockito.Mockito.*;
47
48 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
49 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-merge.xml" })
50 public class Maven2RepositoryMergerTest
51     extends TestCase
52 {
53
54     private static final String TEST_REPO_ID = "test";
55
56     @Inject
57     private Maven2RepositoryMerger repositoryMerger;
58
59     @Inject
60     @Named("archivaConfiguration#default")
61     ArchivaConfiguration configuration;
62
63     private MetadataRepository metadataRepository;
64
65     private static RepositorySessionFactory repositorySessionFactory;
66
67     private static RepositorySession session;
68
69     static
70     {
71         repositorySessionFactory = mock(RepositorySessionFactory.class);
72         session = mock( RepositorySession.class );
73
74         try
75         {
76             when( repositorySessionFactory.createSession( ) ).thenReturn( session );
77         }
78         catch ( MetadataRepositoryException e )
79         {
80             throw new RuntimeException( e );
81         }
82
83     }
84
85     public static RepositorySessionFactory getRepositorySessionFactory() {
86         return repositorySessionFactory;
87     }
88
89
90
91     @Before
92     @Override
93     public void setUp()
94         throws Exception
95     {
96         super.setUp();
97         MockitoAnnotations.initMocks( this );
98         metadataRepository = mock( MetadataRepository.class );
99         repositoryMerger.setRepositorySessionFactory( repositorySessionFactory );
100
101     }
102
103     private List<ArtifactMetadata> getArtifacts()
104     {
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" );
112
113         metadata.add( artifact1 );
114         return metadata;
115     }
116
117     @Test
118     public void testMerge()
119         throws Exception
120     {
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" );
124
125         Path mavenMetadata = Paths.get( targetRepoPath, "com/example/test/test-artifact/maven-metadata.xml" );
126
127         Path pom = Paths.get( targetRepoPath,
128                              "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.pom" );
129
130         for (Path testArtifact : new Path[] { mergedArtifact, mavenMetadata, pom }) {
131             Files.deleteIfExists(testArtifact);
132         }
133
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" );
141
142         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
143         List<String> knownContentConsumers = new ArrayList<>();
144         knownContentConsumers.add( "metadata-updater12" );
145         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
146         c.setRepositoryScanning( repoScanConfig );
147
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 );
154
155
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) );
162     }
163
164     @Test
165     public void testMergeWithOutConflictArtifacts()
166         throws Exception
167     {
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 );
176
177         List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
178         sourceRepoArtifactsList.add( artifact1 );
179         List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
180
181         Configuration c = new Configuration();
182         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
183         testRepo.setId( TEST_REPO_ID );
184         testRepo.setLocation( "target/test-repository" );
185
186         String sourceRepo = "src/test/resources/test-repository-with-conflict-artifacts";
187         ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
188         testRepoWithConflicts.setId( sourceRepoId );
189         testRepoWithConflicts.setLocation( sourceRepo );
190
191         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
192         List<String> knownContentConsumers = new ArrayList<>();
193         knownContentConsumers.add( "metadata-updater" );
194         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
195         c.setRepositoryScanning( repoScanConfig );
196
197         c.addManagedRepository( testRepo );
198         c.addManagedRepository( testRepoWithConflicts );
199         configuration.save( c );
200
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();
204
205             when(metadataRepository.getArtifacts(session, sourceRepoId)).thenReturn(sourceRepoArtifactsList);
206             when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(targetRepoArtifactsList);
207
208             assertEquals(1, repositoryMerger.getConflictingArtifacts(metadataRepository, sourceRepoId,
209                     TEST_REPO_ID).size());
210             verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);
211     }
212
213 }