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