]> source.dussan.org Git - archiva.git/blob
0a50b3043cb480eaceef9a320f473a9a5a090244
[archiva.git] /
1 package org.apache.archiva.consumers.core.repository;
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.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
23 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.MetadataFacet;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.ManagedRepository;
29 import org.apache.archiva.repository.RepositoryContentFactory;
30 import org.apache.archiva.repository.RepositoryRegistry;
31 import org.apache.archiva.metadata.audit.RepositoryListener;
32 import org.apache.archiva.repository.metadata.base.MetadataTools;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.xmlunit.assertj.XmlAssert;
38
39 import javax.inject.Inject;
40 import java.nio.charset.Charset;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.Collections;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Set;
48
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.ArgumentMatchers.eq;
51 import static org.mockito.Mockito.*;
52
53
54 /**
55  */
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
57     "classpath:/spring-context-cleanup-released-snapshots.xml" } )
58 public class CleanupReleasedSnapshotsRepositoryPurgeTest
59     extends AbstractRepositoryPurgeTest
60 {
61     private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
62
63     private ArchivaConfiguration archivaConfiguration;
64
65     public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
66         "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
67
68     public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
69         "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
70
71     public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
72         "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
73
74     @Inject
75     MetadataTools metadataTools;
76
77
78
79     @Before
80     @Override
81     public void setUp()
82         throws Exception
83     {
84         super.setUp();
85
86         RepositoryContentFactory factory =
87             applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
88                                         RepositoryContentFactory.class );
89
90         archivaConfiguration =
91             applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
92
93         listener = mock( RepositoryListener.class );
94         List<RepositoryListener> listeners = Collections.singletonList( listener );
95
96         Mockito.when( sessionFactory.createSession( ) ).thenReturn( repositorySession );
97         Mockito.when( repositorySession.getRepository()).thenReturn( metadataRepository );
98         repositorySession.save();
99         repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
100                                                                  applicationContext.getBean(
101                                                                      ArchivaRepositoryRegistry.class ),
102                                                                  repositorySession, listeners );
103
104         ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
105             ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
106     }
107
108     @Test
109     public void testReleasedSnapshotsExistsInSameRepo()
110         throws Exception
111     {
112         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class );
113         repositoryRegistry.removeRepository( TEST_REPO_ID );
114         repositoryRegistry.putRepository(
115             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
116
117         String repoRoot = prepareTestRepos();
118         String projectNs = "org.apache.maven.plugins";
119         String projectPath = projectNs.replaceAll("\\.","/");
120         String projectName = "maven-plugin-plugin";
121         String projectVersion = "2.3-SNAPSHOT";
122         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
123         Path repo = getTestRepoRootPath();
124         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
125         Set<String> deletedVersions = new HashSet<>();
126         deletedVersions.add("2.3-SNAPSHOT");
127
128         // test listeners for the correct artifacts
129         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
130                                  "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
131
132         // Provide the metadata list
133         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
134         when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
135             projectNs, projectName, projectVersion )).thenReturn(ml);
136
137
138         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
139
140         // Verify the metadataRepository invocations
141         // complete snapshot version removal for released
142         verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
143         verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") );
144
145         // check if the snapshot was removed
146         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
147         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
148         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
149         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
150         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
151         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
152         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
153
154         // check if the released version was not removed
155         assertExists( projectRoot + "/2.3" );
156         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
157         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
158         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
159         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
160         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
161         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
162         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
163         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
164         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
165
166         // check if metadata file was updated
167         Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
168
169         String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
170
171         String expectedVersions =
172             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
173
174         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/release" ).isEqualTo( "2.3" );
175         // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
176         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.3" );
177         // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
178         XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
179         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.2" );
180         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.3" );
181
182         // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
183         //                             "//metadata/versioning/versions/version", metadataXml );
184         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070315032817" );
185         // XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
186     }
187
188     @Test
189     public void testNonArtifactFile()
190         throws Exception
191     {
192
193         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
194         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
195         repositoryRegistry.removeRepository( managedRepository );
196         repositoryRegistry.putRepository(
197             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
198
199         String repoRoot = prepareTestRepos();
200
201         // test listeners for the correct artifacts
202         Path file = Paths.get(repoRoot, INDEX_PATH );
203         if ( !Files.exists(file) )
204         {
205             // help windauze to create directory with .
206             Files.createDirectories( file.getParent() );
207             Files.createFile( file );
208         }
209         assertTrue( Files.exists(file) );
210
211         repoPurge.process( INDEX_PATH );
212
213         assertTrue( Files.exists(file) );
214     }
215
216     @Test
217     public void testReleasedSnapshotsExistsInDifferentRepo()
218         throws Exception
219     {
220
221         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
222         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
223         repositoryRegistry.removeRepository( TEST_REPO_ID );
224         repositoryRegistry.putRepository(
225             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
226
227
228         repositoryRegistry.putRepository(
229             getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
230
231         String repoRoot = prepareTestRepos();
232         String projectNs = "org.apache.archiva";
233         String projectPath = projectNs.replaceAll("\\.","/");
234         String projectName = "released-artifact-in-diff-repo";
235         String projectVersion = "1.0-SNAPSHOT";
236         String releaseVersion = "1.0";
237         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
238         Path repo = getTestRepoRootPath();
239         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
240         Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
241         Set<String> deletedVersions = new HashSet<>();
242         deletedVersions.add("1.0-SNAPSHOT");
243
244
245         // test listeners for the correct artifacts
246         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
247                                  "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
248                                  "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
249         // Provide the metadata list
250         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
251         when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
252             projectNs, projectName, projectVersion )).thenReturn(ml);
253
254         List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
255         when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID,
256             projectNs, projectName, releaseVersion )).thenReturn(ml2);
257
258
259         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
260
261         // Verify the metadataRepository invocations
262         // Complete version removal for cleanup
263         verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
264         verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) );
265
266
267         // check if the snapshot was removed
268         assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
269         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
270         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
271         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
272         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
273         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
274         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
275
276         String releasesProjectRoot =
277             AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
278                 + "/org/apache/archiva/released-artifact-in-diff-repo" );
279
280         // check if the released version was not removed
281         assertExists( releasesProjectRoot + "/1.0" );
282         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
283         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
284         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
285         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
286         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
287         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
288         
289         // remove RELEASES_TEST_REPO_ID so this test will be more independant
290         applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
291     }
292
293     @Test
294     public void testHigherSnapshotExistsInSameRepo()
295         throws Exception
296     {
297
298         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
299         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
300         repositoryRegistry.removeRepository( TEST_REPO_ID );
301         repositoryRegistry.putRepository(
302             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
303
304         String repoRoot = prepareTestRepos();
305         String projectNs = "org.apache.maven.plugins";
306         String projectPath = projectNs.replaceAll("\\.","/");
307         String projectName = "maven-source-plugin";
308         String projectVersion = "2.0.2";
309         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
310         Path repo = getTestRepoRootPath();
311         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
312         Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
313         Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
314
315         // test listeners for the correct artifacts - no deletions
316         // Provide the metadata list
317         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
318         when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
319             projectNs, projectName, projectVersion )).thenReturn(ml);
320         List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
321         when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
322             projectNs, projectName, "2.0.3-SNAPSHOT" )).thenReturn(ml);
323         List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
324         when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
325             projectNs, projectName, "2.0.4-SNAPSHOT" )).thenReturn(ml);
326
327
328         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
329
330         // Verify the metadataRepository invocations
331         // No removal
332         verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
333         verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") );
334         verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") );
335         verify(metadataRepository, never()).removeTimestampedArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) );
336         verify(metadataRepository, never()).removeFacetFromArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) );
337
338
339
340         // check if the snapshot was not removed
341         assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
342         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
343         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
344         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
345         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
346         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
347         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
348
349         // check if the released version was not removed
350         assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
351         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
352         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
353         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
354         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
355         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
356         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
357
358         // check if metadata file was not updated (because nothing was removed)
359         Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
360
361         String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
362
363         String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
364             + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
365
366         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.0.4-SNAPSHOT" );
367         // XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
368         XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
369         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.0.3-SNAPSHOT" );
370         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.0.4-SNAPSHOT" );
371         // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
372         //                             "//metadata/versioning/versions/version", metadataXml );
373         XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070427033345" );
374         // XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
375     }
376 }