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.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
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.ArgumentMatchers.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 listener = mock( RepositoryListener.class );
94 List<RepositoryListener> listeners = Collections.singletonList( listener );
96 Mockito.when( sessionFactory.createSession( ) ).thenReturn( repositorySession );
97 Mockito.when( repositorySession.getRepository()).thenReturn( metadataRepository );
98 repositorySession.save();
99 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
100 applicationContext.getBean(
101 ArchivaRepositoryRegistry.class ),
102 repositorySession, listeners );
104 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
105 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
109 public void testReleasedSnapshotsExistsInSameRepo()
112 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class );
113 repositoryRegistry.removeRepository( TEST_REPO_ID );
114 repositoryRegistry.putRepository(
115 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
117 String repoRoot = prepareTestRepos();
118 String projectNs = "org.apache.maven.plugins";
119 String projectPath = projectNs.replaceAll("\\.","/");
120 String projectName = "maven-plugin-plugin";
121 String projectVersion = "2.3-SNAPSHOT";
122 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
123 Path repo = getTestRepoRootPath();
124 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
125 Set<String> deletedVersions = new HashSet<>();
126 deletedVersions.add("2.3-SNAPSHOT");
128 // test listeners for the correct artifacts
129 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
130 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
132 // Provide the metadata list
133 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
134 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
135 projectNs, projectName, projectVersion )).thenReturn(ml);
138 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
140 // Verify the metadataRepository invocations
141 // complete snapshot version removal for released
142 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
143 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") );
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" );
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" );
166 // check if metadata file was updated
167 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
169 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
171 String expectedVersions =
172 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
174 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/release" ).isEqualTo( "2.3" );
175 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
176 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.3" );
177 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
178 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
179 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.2" );
180 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.3" );
182 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
183 // "//metadata/versioning/versions/version", metadataXml );
184 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070315032817" );
185 // XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
189 public void testNonArtifactFile()
193 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
194 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
195 repositoryRegistry.removeRepository( managedRepository );
196 repositoryRegistry.putRepository(
197 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
199 String repoRoot = prepareTestRepos();
201 // test listeners for the correct artifacts
202 Path file = Paths.get(repoRoot, INDEX_PATH );
203 if ( !Files.exists(file) )
205 // help windauze to create directory with .
206 Files.createDirectories( file.getParent() );
207 Files.createFile( file );
209 assertTrue( Files.exists(file) );
211 repoPurge.process( INDEX_PATH );
213 assertTrue( Files.exists(file) );
217 public void testReleasedSnapshotsExistsInDifferentRepo()
221 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
222 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
223 repositoryRegistry.removeRepository( TEST_REPO_ID );
224 repositoryRegistry.putRepository(
225 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
228 repositoryRegistry.putRepository(
229 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
231 String repoRoot = prepareTestRepos();
232 String projectNs = "org.apache.archiva";
233 String projectPath = projectNs.replaceAll("\\.","/");
234 String projectName = "released-artifact-in-diff-repo";
235 String projectVersion = "1.0-SNAPSHOT";
236 String releaseVersion = "1.0";
237 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
238 Path repo = getTestRepoRootPath();
239 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
240 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
241 Set<String> deletedVersions = new HashSet<>();
242 deletedVersions.add("1.0-SNAPSHOT");
245 // test listeners for the correct artifacts
246 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
247 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
248 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
249 // Provide the metadata list
250 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
251 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
252 projectNs, projectName, projectVersion )).thenReturn(ml);
254 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
255 when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID,
256 projectNs, projectName, releaseVersion )).thenReturn(ml2);
259 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
261 // Verify the metadataRepository invocations
262 // Complete version removal for cleanup
263 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
264 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) );
267 // check if the snapshot was removed
268 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
269 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
270 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
271 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
272 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
273 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
274 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
276 String releasesProjectRoot =
277 AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
278 + "/org/apache/archiva/released-artifact-in-diff-repo" );
280 // check if the released version was not removed
281 assertExists( releasesProjectRoot + "/1.0" );
282 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
283 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
284 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
285 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
286 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
287 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
289 // remove RELEASES_TEST_REPO_ID so this test will be more independant
290 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
294 public void testHigherSnapshotExistsInSameRepo()
298 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
299 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
300 repositoryRegistry.removeRepository( TEST_REPO_ID );
301 repositoryRegistry.putRepository(
302 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
304 String repoRoot = prepareTestRepos();
305 String projectNs = "org.apache.maven.plugins";
306 String projectPath = projectNs.replaceAll("\\.","/");
307 String projectName = "maven-source-plugin";
308 String projectVersion = "2.0.2";
309 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
310 Path repo = getTestRepoRootPath();
311 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
312 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
313 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
315 // test listeners for the correct artifacts - no deletions
316 // Provide the metadata list
317 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
318 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
319 projectNs, projectName, projectVersion )).thenReturn(ml);
320 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
321 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
322 projectNs, projectName, "2.0.3-SNAPSHOT" )).thenReturn(ml);
323 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
324 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
325 projectNs, projectName, "2.0.4-SNAPSHOT" )).thenReturn(ml);
328 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
330 // Verify the metadataRepository invocations
332 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
333 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") );
334 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") );
335 verify(metadataRepository, never()).removeTimestampedArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) );
336 verify(metadataRepository, never()).removeFacetFromArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) );
340 // check if the snapshot was not removed
341 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
342 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
343 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
344 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
345 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
346 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
347 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
349 // check if the released version was not removed
350 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
351 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
352 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
353 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
354 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
355 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
356 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
358 // check if metadata file was not updated (because nothing was removed)
359 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
361 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
363 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
364 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
366 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.0.4-SNAPSHOT" );
367 // XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
368 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
369 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.0.3-SNAPSHOT" );
370 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.0.4-SNAPSHOT" );
371 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
372 // "//metadata/versioning/versions/version", metadataXml );
373 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070427033345" );
374 // XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );