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.custommonkey.xmlunit.XMLAssert;
34 import org.easymock.EasyMock;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.springframework.test.context.ContextConfiguration;
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;
84 RepositoryContentFactory factory =
85 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
86 RepositoryContentFactory.class );
88 archivaConfiguration =
89 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
91 listenerControl = EasyMock.createControl( );
93 listener = listenerControl.createMock( RepositoryListener.class );
94 List<RepositoryListener> listeners = Collections.singletonList( listener );
96 sessionControl.reset();
97 sessionFactoryControl.reset();
98 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
99 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
100 repositorySession.save();
101 EasyMock.expectLastCall().anyTimes();
102 sessionFactoryControl.replay();
103 sessionControl.replay();
104 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
105 applicationContext.getBean(
106 ArchivaRepositoryRegistry.class ),
107 repositorySession, listeners );
109 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
110 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
114 public void testReleasedSnapshotsExistsInSameRepo()
117 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class );
118 repositoryRegistry.removeRepository( TEST_REPO_ID );
119 repositoryRegistry.putRepository(
120 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
122 String repoRoot = prepareTestRepos();
123 String projectNs = "org.apache.maven.plugins";
124 String projectPath = projectNs.replaceAll("\\.","/");
125 String projectName = "maven-plugin-plugin";
126 String projectVersion = "2.3-SNAPSHOT";
127 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
128 Path repo = getTestRepoRootPath();
129 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
130 Set<String> deletedVersions = new HashSet<>();
131 deletedVersions.add("2.3-SNAPSHOT");
133 // test listeners for the correct artifacts
134 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
135 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
136 listenerControl.replay();
138 // Provide the metadata list
139 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
140 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
141 projectNs, projectName, projectVersion )).thenReturn(ml);
144 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
146 listenerControl.verify();
148 // Verify the metadataRepository invocations
149 // complete snapshot version removal for released
150 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
151 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") );
153 // check if the snapshot was removed
154 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
155 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
156 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
157 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
158 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
159 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
160 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
162 // check if the released version was not removed
163 assertExists( projectRoot + "/2.3" );
164 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
165 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
166 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
167 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
168 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
169 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
170 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
171 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
172 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
174 // check if metadata file was updated
175 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
177 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
179 String expectedVersions =
180 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
182 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
183 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
184 System.out.println(metadataXml);
185 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
186 "//metadata/versioning/versions/version", metadataXml );
187 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
191 public void testNonArtifactFile()
195 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
196 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
197 repositoryRegistry.removeRepository( managedRepository );
198 repositoryRegistry.putRepository(
199 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
201 String repoRoot = prepareTestRepos();
203 // test listeners for the correct artifacts
204 listenerControl.replay();
206 Path file = Paths.get(repoRoot, INDEX_PATH );
207 if ( !Files.exists(file) )
209 // help windauze to create directory with .
210 Files.createDirectories( file.getParent() );
211 Files.createFile( file );
213 assertTrue( Files.exists(file) );
215 repoPurge.process( INDEX_PATH );
217 listenerControl.verify();
219 assertTrue( Files.exists(file) );
223 public void testReleasedSnapshotsExistsInDifferentRepo()
227 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
228 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
229 repositoryRegistry.removeRepository( TEST_REPO_ID );
230 repositoryRegistry.putRepository(
231 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
234 repositoryRegistry.putRepository(
235 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
237 String repoRoot = prepareTestRepos();
238 String projectNs = "org.apache.archiva";
239 String projectPath = projectNs.replaceAll("\\.","/");
240 String projectName = "released-artifact-in-diff-repo";
241 String projectVersion = "1.0-SNAPSHOT";
242 String releaseVersion = "1.0";
243 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
244 Path repo = getTestRepoRootPath();
245 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
246 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
247 Set<String> deletedVersions = new HashSet<>();
248 deletedVersions.add("1.0-SNAPSHOT");
251 // test listeners for the correct artifacts
252 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
253 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
254 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
255 listenerControl.replay();
257 // Provide the metadata list
258 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
259 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
260 projectNs, projectName, projectVersion )).thenReturn(ml);
262 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
263 when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID,
264 projectNs, projectName, releaseVersion )).thenReturn(ml2);
267 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
269 listenerControl.verify();
271 // Verify the metadataRepository invocations
272 // Complete version removal for cleanup
273 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
274 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) );
277 // check if the snapshot was removed
278 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
279 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
280 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
281 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
282 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
283 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
284 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
286 String releasesProjectRoot =
287 AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
288 + "/org/apache/archiva/released-artifact-in-diff-repo" );
290 // check if the released version was not removed
291 assertExists( releasesProjectRoot + "/1.0" );
292 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
293 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
294 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
295 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
296 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
297 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
299 // remove RELEASES_TEST_REPO_ID so this test will be more independant
300 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
304 public void testHigherSnapshotExistsInSameRepo()
308 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
309 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
310 repositoryRegistry.removeRepository( TEST_REPO_ID );
311 repositoryRegistry.putRepository(
312 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
314 String repoRoot = prepareTestRepos();
315 String projectNs = "org.apache.maven.plugins";
316 String projectPath = projectNs.replaceAll("\\.","/");
317 String projectName = "maven-source-plugin";
318 String projectVersion = "2.0.2";
319 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
320 Path repo = getTestRepoRootPath();
321 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
322 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
323 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
325 // test listeners for the correct artifacts - no deletions
326 listenerControl.replay();
328 // Provide the metadata list
329 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
330 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
331 projectNs, projectName, projectVersion )).thenReturn(ml);
332 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
333 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
334 projectNs, projectName, "2.0.3-SNAPSHOT" )).thenReturn(ml);
335 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
336 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
337 projectNs, projectName, "2.0.4-SNAPSHOT" )).thenReturn(ml);
340 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
342 listenerControl.verify();
344 // Verify the metadataRepository invocations
346 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
347 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") );
348 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") );
349 verify(metadataRepository, never()).removeTimestampedArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) );
350 verify(metadataRepository, never()).removeFacetFromArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) );
354 // check if the snapshot was not removed
355 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
356 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
357 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
358 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
359 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
360 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
361 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
363 // check if the released version was not removed
364 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
365 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
366 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
367 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
368 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
369 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
370 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
372 // check if metadata file was not updated (because nothing was removed)
373 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
375 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
377 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
378 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
380 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
381 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
382 "//metadata/versioning/versions/version", metadataXml );
383 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );