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