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 );
104 public void testReleasedSnapshotsExistsInSameRepo()
107 RepositoryRegistry repositoryRegistry = applicationContext.getBean( RepositoryRegistry.class );
108 repositoryRegistry.removeRepository( TEST_REPO_ID );
109 repositoryRegistry.putRepository(
110 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
112 String repoRoot = prepareTestRepos();
113 String projectNs = "org.apache.maven.plugins";
114 String projectPath = projectNs.replaceAll("\\.","/");
115 String projectName = "maven-plugin-plugin";
116 String projectVersion = "2.3-SNAPSHOT";
117 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
118 Path repo = getTestRepoRootPath();
119 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
120 Set<String> deletedVersions = new HashSet<>();
121 deletedVersions.add("2.3-SNAPSHOT");
123 // test listeners for the correct artifacts
124 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
125 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
126 listenerControl.replay();
128 // Provide the metadata list
129 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
130 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
131 projectName, projectVersion)).thenReturn(ml);
134 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
136 listenerControl.verify();
138 // Verify the metadataRepository invocations
139 // complete snapshot version removal for released
140 verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
141 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3"));
143 // check if the snapshot was removed
144 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
145 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
146 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
147 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
148 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
149 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
150 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
152 // check if the released version was not removed
153 assertExists( projectRoot + "/2.3" );
154 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
155 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
156 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
157 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
158 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
159 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
160 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
161 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
162 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
164 // check if metadata file was updated
165 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
167 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
169 String expectedVersions =
170 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
172 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
173 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
174 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
175 "//metadata/versioning/versions/version", metadataXml );
176 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
180 public void testNonArtifactFile()
184 RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
185 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
186 repositoryRegistry.removeRepository( managedRepository );
187 repositoryRegistry.putRepository(
188 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
190 String repoRoot = prepareTestRepos();
192 // test listeners for the correct artifacts
193 listenerControl.replay();
195 Path file = Paths.get(repoRoot, INDEX_PATH );
196 if ( !Files.exists(file) )
198 // help windauze to create directory with .
199 Files.createDirectories( file.getParent() );
200 Files.createFile( file );
202 assertTrue( Files.exists(file) );
204 repoPurge.process( INDEX_PATH );
206 listenerControl.verify();
208 assertTrue( Files.exists(file) );
212 public void testReleasedSnapshotsExistsInDifferentRepo()
216 RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
217 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
218 repositoryRegistry.removeRepository( TEST_REPO_ID );
219 repositoryRegistry.putRepository(
220 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
223 repositoryRegistry.putRepository(
224 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
226 String repoRoot = prepareTestRepos();
227 String projectNs = "org.apache.archiva";
228 String projectPath = projectNs.replaceAll("\\.","/");
229 String projectName = "released-artifact-in-diff-repo";
230 String projectVersion = "1.0-SNAPSHOT";
231 String releaseVersion = "1.0";
232 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
233 Path repo = getTestRepoRootPath();
234 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
235 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
236 Set<String> deletedVersions = new HashSet<>();
237 deletedVersions.add("1.0-SNAPSHOT");
240 // test listeners for the correct artifacts
241 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
242 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
243 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
244 listenerControl.replay();
246 // Provide the metadata list
247 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
248 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
249 projectName, projectVersion)).thenReturn(ml);
251 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
252 when(metadataRepository.getArtifacts(RELEASES_TEST_REPO_ID, projectNs,
253 projectName, releaseVersion)).thenReturn(ml2);
256 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
258 listenerControl.verify();
260 // Verify the metadataRepository invocations
261 // Complete version removal for cleanup
262 verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
263 verify(metadataRepository, never()).removeProjectVersion(eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion));
266 // check if the snapshot was removed
267 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
268 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
269 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
270 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
271 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
272 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
273 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
275 String releasesProjectRoot =
276 AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
277 + "/org/apache/archiva/released-artifact-in-diff-repo" );
279 // check if the released version was not removed
280 assertExists( releasesProjectRoot + "/1.0" );
281 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
282 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
283 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
284 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
285 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
286 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
288 // remove RELEASES_TEST_REPO_ID so this test will be more independant
289 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
293 public void testHigherSnapshotExistsInSameRepo()
297 RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
298 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
299 repositoryRegistry.removeRepository( TEST_REPO_ID );
300 repositoryRegistry.putRepository(
301 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
303 String repoRoot = prepareTestRepos();
304 String projectNs = "org.apache.maven.plugins";
305 String projectPath = projectNs.replaceAll("\\.","/");
306 String projectName = "maven-source-plugin";
307 String projectVersion = "2.0.2";
308 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
309 Path repo = getTestRepoRootPath();
310 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
311 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
312 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
314 // test listeners for the correct artifacts - no deletions
315 listenerControl.replay();
317 // Provide the metadata list
318 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
319 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
320 projectName, projectVersion)).thenReturn(ml);
321 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
322 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
323 projectName, "2.0.3-SNAPSHOT")).thenReturn(ml);
324 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
325 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
326 projectName, "2.0.4-SNAPSHOT")).thenReturn(ml);
329 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
331 listenerControl.verify();
333 // Verify the metadataRepository invocations
335 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
336 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT"));
337 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT"));
338 verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class));
339 verify(metadataRepository, never()).removeArtifact(any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class));
343 // check if the snapshot was not removed
344 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
345 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
346 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
347 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
348 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
349 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
350 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
352 // check if the released version was not removed
353 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
354 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
355 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
356 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
357 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
358 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
359 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
361 // check if metadata file was not updated (because nothing was removed)
362 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
364 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
366 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
367 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
369 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
370 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
371 "//metadata/versioning/versions/version", metadataXml );
372 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );