1 package org.apache.archiva.consumers.core.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.easymock.EasyMock;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.xmlunit.assertj.XmlAssert;
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;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Matchers.eq;
51 import static org.mockito.Mockito.*;
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
57 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
58 public class CleanupReleasedSnapshotsRepositoryPurgeTest
59 extends AbstractRepositoryPurgeTest
61 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
63 private ArchivaConfiguration archivaConfiguration;
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";
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";
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";
75 MetadataTools metadataTools;
86 RepositoryContentFactory factory =
87 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
88 RepositoryContentFactory.class );
90 archivaConfiguration =
91 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
93 listenerControl = EasyMock.createControl( );
95 listener = listenerControl.createMock( RepositoryListener.class );
96 List<RepositoryListener> listeners = Collections.singletonList( listener );
98 sessionControl.reset();
99 sessionFactoryControl.reset();
100 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
101 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
102 repositorySession.save();
103 EasyMock.expectLastCall().anyTimes();
104 sessionFactoryControl.replay();
105 sessionControl.replay();
106 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
107 applicationContext.getBean(
108 ArchivaRepositoryRegistry.class ),
109 repositorySession, listeners );
111 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
112 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
116 public void testReleasedSnapshotsExistsInSameRepo()
119 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class );
120 repositoryRegistry.removeRepository( TEST_REPO_ID );
121 repositoryRegistry.putRepository(
122 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
124 String repoRoot = prepareTestRepos();
125 String projectNs = "org.apache.maven.plugins";
126 String projectPath = projectNs.replaceAll("\\.","/");
127 String projectName = "maven-plugin-plugin";
128 String projectVersion = "2.3-SNAPSHOT";
129 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
130 Path repo = getTestRepoRootPath();
131 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
132 Set<String> deletedVersions = new HashSet<>();
133 deletedVersions.add("2.3-SNAPSHOT");
135 // test listeners for the correct artifacts
136 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
137 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
138 listenerControl.replay();
140 // Provide the metadata list
141 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
142 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
143 projectNs, projectName, projectVersion )).thenReturn(ml);
146 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
148 listenerControl.verify();
150 // Verify the metadataRepository invocations
151 // complete snapshot version removal for released
152 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
153 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") );
155 // check if the snapshot was removed
156 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
157 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
158 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
159 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
160 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
161 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
162 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
164 // check if the released version was not removed
165 assertExists( projectRoot + "/2.3" );
166 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
167 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
168 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
169 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
170 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
171 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
172 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
173 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
174 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
176 // check if metadata file was updated
177 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
179 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
181 String expectedVersions =
182 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
184 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/release" ).isEqualTo( "2.3" );
185 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
186 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.3" );
187 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
188 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
189 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.2" );
190 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.3" );
192 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
193 // "//metadata/versioning/versions/version", metadataXml );
194 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070315032817" );
195 // XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
199 public void testNonArtifactFile()
203 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
204 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
205 repositoryRegistry.removeRepository( managedRepository );
206 repositoryRegistry.putRepository(
207 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
209 String repoRoot = prepareTestRepos();
211 // test listeners for the correct artifacts
212 listenerControl.replay();
214 Path file = Paths.get(repoRoot, INDEX_PATH );
215 if ( !Files.exists(file) )
217 // help windauze to create directory with .
218 Files.createDirectories( file.getParent() );
219 Files.createFile( file );
221 assertTrue( Files.exists(file) );
223 repoPurge.process( INDEX_PATH );
225 listenerControl.verify();
227 assertTrue( Files.exists(file) );
231 public void testReleasedSnapshotsExistsInDifferentRepo()
235 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
236 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
237 repositoryRegistry.removeRepository( TEST_REPO_ID );
238 repositoryRegistry.putRepository(
239 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
242 repositoryRegistry.putRepository(
243 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
245 String repoRoot = prepareTestRepos();
246 String projectNs = "org.apache.archiva";
247 String projectPath = projectNs.replaceAll("\\.","/");
248 String projectName = "released-artifact-in-diff-repo";
249 String projectVersion = "1.0-SNAPSHOT";
250 String releaseVersion = "1.0";
251 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
252 Path repo = getTestRepoRootPath();
253 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
254 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
255 Set<String> deletedVersions = new HashSet<>();
256 deletedVersions.add("1.0-SNAPSHOT");
259 // test listeners for the correct artifacts
260 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
261 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
262 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
263 listenerControl.replay();
265 // Provide the metadata list
266 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
267 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
268 projectNs, projectName, projectVersion )).thenReturn(ml);
270 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
271 when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID,
272 projectNs, projectName, releaseVersion )).thenReturn(ml2);
275 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
277 listenerControl.verify();
279 // Verify the metadataRepository invocations
280 // Complete version removal for cleanup
281 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
282 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) );
285 // check if the snapshot was removed
286 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
287 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
288 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
289 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
290 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
291 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
292 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
294 String releasesProjectRoot =
295 AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
296 + "/org/apache/archiva/released-artifact-in-diff-repo" );
298 // check if the released version was not removed
299 assertExists( releasesProjectRoot + "/1.0" );
300 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
301 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
302 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
303 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
304 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
305 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
307 // remove RELEASES_TEST_REPO_ID so this test will be more independant
308 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
312 public void testHigherSnapshotExistsInSameRepo()
316 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
317 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
318 repositoryRegistry.removeRepository( TEST_REPO_ID );
319 repositoryRegistry.putRepository(
320 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
322 String repoRoot = prepareTestRepos();
323 String projectNs = "org.apache.maven.plugins";
324 String projectPath = projectNs.replaceAll("\\.","/");
325 String projectName = "maven-source-plugin";
326 String projectVersion = "2.0.2";
327 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
328 Path repo = getTestRepoRootPath();
329 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
330 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
331 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
333 // test listeners for the correct artifacts - no deletions
334 listenerControl.replay();
336 // Provide the metadata list
337 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
338 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
339 projectNs, projectName, projectVersion )).thenReturn(ml);
340 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
341 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
342 projectNs, projectName, "2.0.3-SNAPSHOT" )).thenReturn(ml);
343 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
344 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
345 projectNs, projectName, "2.0.4-SNAPSHOT" )).thenReturn(ml);
348 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
350 listenerControl.verify();
352 // Verify the metadataRepository invocations
354 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
355 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") );
356 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") );
357 verify(metadataRepository, never()).removeTimestampedArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) );
358 verify(metadataRepository, never()).removeFacetFromArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) );
362 // check if the snapshot was not removed
363 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
364 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
365 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
366 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
367 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
368 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
369 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
371 // check if the released version was not removed
372 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
373 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
374 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
375 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
376 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
377 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
378 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
380 // check if metadata file was not updated (because nothing was removed)
381 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
383 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
385 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
386 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
388 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.0.4-SNAPSHOT" );
389 // XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
390 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
391 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.0.3-SNAPSHOT" );
392 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.0.4-SNAPSHOT" );
393 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
394 // "//metadata/versioning/versions/version", metadataXml );
395 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070427033345" );
396 // XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );