]> source.dussan.org Git - archiva.git/blob
a5f864de96aadba543a37dad93b9705a3066059d
[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.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.MockitoAnnotations;
33 import org.springframework.test.context.ContextConfiguration;
34
35 import javax.inject.Inject;
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.List;
39 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
40
41 import static org.mockito.Mockito.*;
42
43 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
44 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
45 public class Maven2RepositoryMergerTest
46     extends TestCase
47 {
48
49     private static final String TEST_REPO_ID = "test";
50
51     @Inject
52     private Maven2RepositoryMerger repositoryMerger;
53
54     @Inject
55     ArchivaConfiguration configuration;
56
57     private MetadataRepository metadataRepository;
58
59     @Before
60     @Override
61     public void setUp()
62         throws Exception
63     {
64         super.setUp();
65         MockitoAnnotations.initMocks( this );
66         metadataRepository = mock( MetadataRepository.class );
67     }
68
69     private List<ArtifactMetadata> getArtifacts()
70     {
71         List<ArtifactMetadata> metadata = new ArrayList<ArtifactMetadata>();
72         ArtifactMetadata artifact1 = new ArtifactMetadata();
73         artifact1.setNamespace( "com.example.test" );
74         artifact1.setProject( "test-artifact" );
75         artifact1.setVersion( "1.0-SNAPSHOT" );
76         artifact1.setProjectVersion( "1.0-SNAPSHOT" );
77         artifact1.setId( "test-artifact-1.0-20100308.230825-1.jar" );
78
79         metadata.add( artifact1 );
80         return metadata;
81     }
82
83     @Test
84     public void testMerge()
85         throws Exception
86     {
87         Configuration c = new Configuration();
88         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
89         testRepo.setId( TEST_REPO_ID );
90         testRepo.setLocation( "target" + File.separatorChar + "test-repository" );
91
92         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
93         List<String> knownContentConsumers = new ArrayList<String>();
94         knownContentConsumers.add( "metadata-updater12" );
95         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
96         c.setRepositoryScanning( repoScanConfig );
97
98         ManagedRepositoryConfiguration targetRepo = new ManagedRepositoryConfiguration();
99         targetRepo.setId( "target-rep" );
100         targetRepo.setLocation( "target" );
101         c.addManagedRepository( testRepo );
102         c.addManagedRepository( targetRepo );
103         configuration.save( c );
104
105         when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( getArtifacts() );
106         repositoryMerger.merge( metadataRepository, TEST_REPO_ID, "target-rep" );
107         verify( metadataRepository ).getArtifacts( TEST_REPO_ID );
108     }
109
110     @Test
111     public void testMergeWithOutConflictArtifacts()
112         throws Exception
113     {
114         String sourceRepoId = "source-repo";
115         ArtifactMetadata artifact1 = new ArtifactMetadata();
116         artifact1.setNamespace( "org.testng" );
117         artifact1.setProject( "testng" );
118         artifact1.setVersion( "5.8" );
119         artifact1.setProjectVersion( "5.8" );
120         artifact1.setId( "testng-5.8-jdk15.jar" );
121         artifact1.setRepositoryId( sourceRepoId );
122
123         List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
124         sourceRepoArtifactsList.add( artifact1 );
125         List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
126
127         Configuration c = new Configuration();
128         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
129         testRepo.setId( TEST_REPO_ID );
130         testRepo.setLocation( "target" + File.separatorChar + "test-repository" );
131
132         String sourceRepo = "src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar
133             + "test-repository-with-conflict-artifacts";
134         ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
135         testRepoWithConflicts.setId( sourceRepoId );
136         testRepoWithConflicts.setLocation( sourceRepo );
137
138         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
139         List<String> knownContentConsumers = new ArrayList<String>();
140         knownContentConsumers.add( "metadata-updater" );
141         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
142         c.setRepositoryScanning( repoScanConfig );
143
144         c.addManagedRepository( testRepo );
145         c.addManagedRepository( testRepoWithConflicts );
146         configuration.save( c );
147
148         File targetRepoFile = new File(
149             "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
150         targetRepoFile.setReadOnly();
151
152         when( metadataRepository.getArtifacts( sourceRepoId ) ).thenReturn( sourceRepoArtifactsList );
153         when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( targetRepoArtifactsList );
154
155         assertEquals( 1, repositoryMerger.getConflictingArtifacts( metadataRepository, sourceRepoId,
156                                                                    TEST_REPO_ID ).size() );
157         verify( metadataRepository ).getArtifacts( TEST_REPO_ID );
158     }
159
160 }