View Javadoc
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.test.utils.ArchivaSpringJUnit4ClassRunner;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  import org.mockito.MockitoAnnotations;
34  import org.springframework.test.context.ContextConfiguration;
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 (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<>();
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          String targetRepoPath = "target" + File.separatorChar + "test-repository-target";
88          File mergedArtifact = new File( targetRepoPath,
89                                          "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
90  
91          File mavenMetadata = new File( targetRepoPath, "com/example/test/test-artifact/maven-metadata.xml" );
92  
93          File pom = new File( targetRepoPath,
94                               "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.pom" );
95  
96          assertFalse( mergedArtifact.exists() );
97          assertFalse( mavenMetadata.exists() );
98          assertFalse( pom.exists() );
99          Configuration c = new Configuration();
100         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
101         testRepo.setId( TEST_REPO_ID );
102         testRepo.setLocation( "target" + File.separatorChar + "test-repository" );
103 
104         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
105         List<String> knownContentConsumers = new ArrayList<>();
106         knownContentConsumers.add( "metadata-updater12" );
107         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
108         c.setRepositoryScanning( repoScanConfig );
109 
110         ManagedRepositoryConfiguration targetRepo = new ManagedRepositoryConfiguration();
111         targetRepo.setId( "target-rep" );
112         targetRepo.setLocation( targetRepoPath );
113         c.addManagedRepository( testRepo );
114         c.addManagedRepository( targetRepo );
115         configuration.save( c );
116 
117         when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( getArtifacts() );
118         repositoryMerger.merge( metadataRepository, TEST_REPO_ID, "target-rep" );
119         verify( metadataRepository ).getArtifacts( TEST_REPO_ID );
120         assertTrue( mergedArtifact.exists() );
121         assertTrue( mavenMetadata.exists() );
122         assertTrue( pom.exists() );
123     }
124 
125     @Test
126     public void testMergeWithOutConflictArtifacts()
127         throws Exception
128     {
129         String sourceRepoId = "source-repo";
130         ArtifactMetadata artifact1 = new ArtifactMetadata();
131         artifact1.setNamespace( "org.testng" );
132         artifact1.setProject( "testng" );
133         artifact1.setVersion( "5.8" );
134         artifact1.setProjectVersion( "5.8" );
135         artifact1.setId( "testng-5.8-jdk15.jar" );
136         artifact1.setRepositoryId( sourceRepoId );
137 
138         List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
139         sourceRepoArtifactsList.add( artifact1 );
140         List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
141 
142         Configuration c = new Configuration();
143         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
144         testRepo.setId( TEST_REPO_ID );
145         testRepo.setLocation( "target" + File.separatorChar + "test-repository" );
146 
147         String sourceRepo = "src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar
148             + "test-repository-with-conflict-artifacts";
149         ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
150         testRepoWithConflicts.setId( sourceRepoId );
151         testRepoWithConflicts.setLocation( sourceRepo );
152 
153         RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
154         List<String> knownContentConsumers = new ArrayList<>();
155         knownContentConsumers.add( "metadata-updater" );
156         repoScanConfig.setKnownContentConsumers( knownContentConsumers );
157         c.setRepositoryScanning( repoScanConfig );
158 
159         c.addManagedRepository( testRepo );
160         c.addManagedRepository( testRepoWithConflicts );
161         configuration.save( c );
162 
163         File targetRepoFile = new File(
164             "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
165         targetRepoFile.setReadOnly();
166 
167         when( metadataRepository.getArtifacts( sourceRepoId ) ).thenReturn( sourceRepoArtifactsList );
168         when( metadataRepository.getArtifacts( TEST_REPO_ID ) ).thenReturn( targetRepoArtifactsList );
169 
170         assertEquals( 1, repositoryMerger.getConflictingArtifacts( metadataRepository, sourceRepoId,
171                                                                    TEST_REPO_ID ).size() );
172         verify( metadataRepository ).getArtifacts( TEST_REPO_ID );
173     }
174 
175 }