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.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;
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;
48 import static org.junit.Assert.assertTrue;
49 import static org.mockito.Matchers.eq;
50 import static org.mockito.Mockito.*;
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
56 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
57 public class CleanupReleasedSnapshotsRepositoryPurgeTest
58 extends AbstractRepositoryPurgeTest
60 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
62 private ArchivaConfiguration archivaConfiguration;
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";
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";
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";
74 MetadataTools metadataTools;
83 RepositoryContentFactory factory =
84 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
85 RepositoryContentFactory.class );
87 archivaConfiguration =
88 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
90 listenerControl = EasyMock.createControl( );
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 );
99 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
100 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
101 removeMavenIndexes();
105 public void testReleasedSnapshotsExistsInSameRepo()
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 ));
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");
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();
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);
136 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
138 listenerControl.verify();
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"));
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.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 );
182 public void testNonArtifactFile()
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 ));
192 String repoRoot = prepareTestRepos();
194 // test listeners for the correct artifacts
195 listenerControl.replay();
197 Path file = Paths.get(repoRoot, INDEX_PATH );
198 if ( !Files.exists(file) )
200 // help windauze to create directory with .
201 Files.createDirectories( file.getParent() );
202 Files.createFile( file );
204 assertTrue( Files.exists(file) );
206 repoPurge.process( INDEX_PATH );
208 listenerControl.verify();
210 assertTrue( Files.exists(file) );
214 public void testReleasedSnapshotsExistsInDifferentRepo()
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 ));
225 repositoryRegistry.putRepository(
226 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
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");
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();
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);
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);
258 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
260 listenerControl.verify();
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));
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" );
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" );
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" );
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 );
295 public void testHigherSnapshotExistsInSameRepo()
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 ));
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");
316 // test listeners for the correct artifacts - no deletions
317 listenerControl.replay();
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);
331 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
333 listenerControl.verify();
335 // Verify the metadataRepository invocations
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));
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" );
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" );
363 // check if metadata file was not updated (because nothing was removed)
364 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
366 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
368 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
369 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
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 );