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