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