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