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;
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.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/release" ).isEqualTo( "2.3" );
183 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
184 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.3" );
185 // XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
186 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
187 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.2" );
188 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.3" );
190 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
191 // "//metadata/versioning/versions/version", metadataXml );
192 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070315032817" );
193 // XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
197 public void testNonArtifactFile()
201 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
202 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
203 repositoryRegistry.removeRepository( managedRepository );
204 repositoryRegistry.putRepository(
205 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
207 String repoRoot = prepareTestRepos();
209 // test listeners for the correct artifacts
210 listenerControl.replay();
212 Path file = Paths.get(repoRoot, INDEX_PATH );
213 if ( !Files.exists(file) )
215 // help windauze to create directory with .
216 Files.createDirectories( file.getParent() );
217 Files.createFile( file );
219 assertTrue( Files.exists(file) );
221 repoPurge.process( INDEX_PATH );
223 listenerControl.verify();
225 assertTrue( Files.exists(file) );
229 public void testReleasedSnapshotsExistsInDifferentRepo()
233 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
234 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
235 repositoryRegistry.removeRepository( TEST_REPO_ID );
236 repositoryRegistry.putRepository(
237 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
240 repositoryRegistry.putRepository(
241 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ));
243 String repoRoot = prepareTestRepos();
244 String projectNs = "org.apache.archiva";
245 String projectPath = projectNs.replaceAll("\\.","/");
246 String projectName = "released-artifact-in-diff-repo";
247 String projectVersion = "1.0-SNAPSHOT";
248 String releaseVersion = "1.0";
249 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
250 Path repo = getTestRepoRootPath();
251 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
252 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
253 Set<String> deletedVersions = new HashSet<>();
254 deletedVersions.add("1.0-SNAPSHOT");
257 // test listeners for the correct artifacts
258 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
259 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
260 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
261 listenerControl.replay();
263 // Provide the metadata list
264 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
265 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
266 projectNs, projectName, projectVersion )).thenReturn(ml);
268 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
269 when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID,
270 projectNs, projectName, releaseVersion )).thenReturn(ml2);
273 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
275 listenerControl.verify();
277 // Verify the metadataRepository invocations
278 // Complete version removal for cleanup
279 verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
280 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) );
283 // check if the snapshot was removed
284 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
285 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
286 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
287 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
288 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
289 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
290 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
292 String releasesProjectRoot =
293 AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
294 + "/org/apache/archiva/released-artifact-in-diff-repo" );
296 // check if the released version was not removed
297 assertExists( releasesProjectRoot + "/1.0" );
298 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
299 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
300 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
301 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
302 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
303 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
305 // remove RELEASES_TEST_REPO_ID so this test will be more independant
306 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
310 public void testHigherSnapshotExistsInSameRepo()
314 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
315 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID );
316 repositoryRegistry.removeRepository( TEST_REPO_ID );
317 repositoryRegistry.putRepository(
318 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ));
320 String repoRoot = prepareTestRepos();
321 String projectNs = "org.apache.maven.plugins";
322 String projectPath = projectNs.replaceAll("\\.","/");
323 String projectName = "maven-source-plugin";
324 String projectVersion = "2.0.2";
325 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
326 Path repo = getTestRepoRootPath();
327 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
328 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
329 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
331 // test listeners for the correct artifacts - no deletions
332 listenerControl.replay();
334 // Provide the metadata list
335 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
336 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
337 projectNs, projectName, projectVersion )).thenReturn(ml);
338 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
339 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
340 projectNs, projectName, "2.0.3-SNAPSHOT" )).thenReturn(ml);
341 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
342 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
343 projectNs, projectName, "2.0.4-SNAPSHOT" )).thenReturn(ml);
346 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
348 listenerControl.verify();
350 // Verify the metadataRepository invocations
352 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
353 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") );
354 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") );
355 verify(metadataRepository, never()).removeTimestampedArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) );
356 verify(metadataRepository, never()).removeFacetFromArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) );
360 // check if the snapshot was not removed
361 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
362 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
363 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
364 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
365 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
366 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
367 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
369 // check if the released version was not removed
370 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
371 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
372 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
373 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
374 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
375 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
376 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
378 // check if metadata file was not updated (because nothing was removed)
379 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
381 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
383 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
384 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
386 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/latest" ).isEqualTo( "2.0.4-SNAPSHOT" );
387 // XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
388 XmlAssert.assertThat( metadataXml ).nodesByXPath( "//metadata/versioning/versions/version" ).hasSize( 2 );
389 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[1]" ).isEqualTo( "2.0.3-SNAPSHOT" );
390 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/versions/version[2]" ).isEqualTo( "2.0.4-SNAPSHOT" );
391 // XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
392 // "//metadata/versioning/versions/version", metadataXml );
393 XmlAssert.assertThat( metadataXml ).valueByXPath( "//metadata/versioning/lastUpdated" ).isEqualTo( "20070427033345" );
394 // XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );