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