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.RepositoryContentFactory;
28 import org.apache.archiva.repository.events.RepositoryListener;
29 import org.apache.archiva.repository.metadata.MetadataTools;
30 import org.apache.commons.io.FileUtils;
31 import org.custommonkey.xmlunit.XMLAssert;
32 import org.easymock.EasyMock;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.springframework.test.context.ContextConfiguration;
38 import javax.inject.Inject;
40 import java.nio.charset.Charset;
41 import java.nio.file.Path;
42 import java.util.Collections;
43 import java.util.HashSet;
44 import java.util.List;
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.Matchers.eq;
49 import static org.mockito.Mockito.*;
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
55 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
56 public class CleanupReleasedSnapshotsRepositoryPurgeTest
57 extends AbstractRepositoryPurgeTest
59 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
61 private ArchivaConfiguration archivaConfiguration;
63 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
64 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
66 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
67 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
69 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
70 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
73 MetadataTools metadataTools;
82 RepositoryContentFactory factory =
83 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
84 RepositoryContentFactory.class );
86 archivaConfiguration =
87 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
89 listenerControl = EasyMock.createControl( );
91 listener = listenerControl.createMock( RepositoryListener.class );
92 List<RepositoryListener> listeners = Collections.singletonList( listener );
93 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
94 applicationContext.getBean(
95 ManagedRepositoryAdmin.class ), factory,
96 repositorySession, listeners );
98 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
99 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
100 removeMavenIndexes();
104 public void testReleasedSnapshotsExistsInSameRepo()
107 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
108 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
109 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
111 String repoRoot = prepareTestRepos();
112 String projectNs = "org.apache.maven.plugins";
113 String projectPath = projectNs.replaceAll("\\.","/");
114 String projectName = "maven-plugin-plugin";
115 String projectVersion = "2.3-SNAPSHOT";
116 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
117 Path repo = getTestRepoRootPath();
118 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
119 Set<String> deletedVersions = new HashSet<>();
120 deletedVersions.add("2.3-SNAPSHOT");
122 // test listeners for the correct artifacts
123 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
124 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
125 listenerControl.replay();
127 // Provide the metadata list
128 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
129 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
130 projectName, projectVersion)).thenReturn(ml);
133 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
135 listenerControl.verify();
137 // Verify the metadataRepository invocations
138 // complete snapshot version removal for released
139 verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
140 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3"));
142 // check if the snapshot was removed
143 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
144 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
145 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
146 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
147 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
148 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
149 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
151 // check if the released version was not removed
152 assertExists( projectRoot + "/2.3" );
153 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
154 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
155 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
156 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
157 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
158 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
159 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
160 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
161 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
163 // check if metadata file was updated
164 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
166 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
168 String expectedVersions =
169 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
171 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
172 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
173 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
174 "//metadata/versioning/versions/version", metadataXml );
175 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
179 public void testNonArtifactFile()
183 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
184 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
185 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
187 String repoRoot = prepareTestRepos();
189 // test listeners for the correct artifacts
190 listenerControl.replay();
192 File file = new File( repoRoot, INDEX_PATH );
193 if ( !file.exists() )
195 // help windauze to create directory with .
196 file.getParentFile().mkdirs();
197 file.createNewFile();
199 assertTrue( file.exists() );
201 repoPurge.process( INDEX_PATH );
203 listenerControl.verify();
205 assertTrue( file.exists() );
209 public void testReleasedSnapshotsExistsInDifferentRepo()
213 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
214 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
215 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
217 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
218 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
220 String repoRoot = prepareTestRepos();
221 String projectNs = "org.apache.archiva";
222 String projectPath = projectNs.replaceAll("\\.","/");
223 String projectName = "released-artifact-in-diff-repo";
224 String projectVersion = "1.0-SNAPSHOT";
225 String releaseVersion = "1.0";
226 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
227 Path repo = getTestRepoRootPath();
228 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
229 Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
230 Set<String> deletedVersions = new HashSet<>();
231 deletedVersions.add("1.0-SNAPSHOT");
234 // test listeners for the correct artifacts
235 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
236 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
237 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
238 listenerControl.replay();
240 // Provide the metadata list
241 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
242 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
243 projectName, projectVersion)).thenReturn(ml);
245 List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir );
246 when(metadataRepository.getArtifacts(RELEASES_TEST_REPO_ID, projectNs,
247 projectName, releaseVersion)).thenReturn(ml2);
250 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
252 listenerControl.verify();
254 // Verify the metadataRepository invocations
255 // Complete version removal for cleanup
256 verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
257 verify(metadataRepository, never()).removeProjectVersion(eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion));
260 // check if the snapshot was removed
261 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
262 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
263 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
264 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
265 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
266 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
267 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
269 String releasesProjectRoot =
270 AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
271 + "/org/apache/archiva/released-artifact-in-diff-repo" );
273 // check if the released version was not removed
274 assertExists( releasesProjectRoot + "/1.0" );
275 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
276 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
277 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
278 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
279 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
280 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
282 // remove RELEASES_TEST_REPO_ID so this test will be more independant
283 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
287 public void testHigherSnapshotExistsInSameRepo()
291 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
292 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
293 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
295 String repoRoot = prepareTestRepos();
296 String projectNs = "org.apache.maven.plugins";
297 String projectPath = projectNs.replaceAll("\\.","/");
298 String projectName = "maven-source-plugin";
299 String projectVersion = "2.0.2";
300 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
301 Path repo = getTestRepoRootPath();
302 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
303 Path vDir2 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.3-SNAPSHOT");
304 Path vDir3 = repo.resolve(projectPath).resolve(projectName).resolve("2.0.4-SNAPSHOT");
306 // test listeners for the correct artifacts - no deletions
307 listenerControl.replay();
309 // Provide the metadata list
310 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
311 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
312 projectName, projectVersion)).thenReturn(ml);
313 List<ArtifactMetadata> m2 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir2 );
314 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
315 projectName, "2.0.3-SNAPSHOT")).thenReturn(ml);
316 List<ArtifactMetadata> m3 = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir3 );
317 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
318 projectName, "2.0.4-SNAPSHOT")).thenReturn(ml);
321 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
323 listenerControl.verify();
325 // Verify the metadataRepository invocations
327 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
328 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT"));
329 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT"));
330 verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class));
331 verify(metadataRepository, never()).removeArtifact(any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class));
335 // check if the snapshot was not removed
336 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
337 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
338 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
339 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
340 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
341 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
342 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
344 // check if the released version was not removed
345 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
346 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
347 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
348 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
349 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
350 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
351 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
353 // check if metadata file was not updated (because nothing was removed)
354 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
356 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
358 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
359 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
361 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
362 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
363 "//metadata/versioning/versions/version", metadataXml );
364 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );