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