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.repository.RepositoryContentFactory;
26 import org.apache.archiva.repository.events.RepositoryListener;
27 import org.apache.archiva.repository.metadata.MetadataTools;
28 import org.apache.commons.io.FileUtils;
29 import org.custommonkey.xmlunit.XMLAssert;
30 import org.easymock.EasyMock;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.springframework.test.context.ContextConfiguration;
35 import javax.inject.Inject;
37 import java.nio.charset.Charset;
38 import java.util.Collections;
39 import java.util.List;
41 import static org.junit.Assert.assertTrue;
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
47 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
48 public class CleanupReleasedSnapshotsRepositoryPurgeTest
49 extends AbstractRepositoryPurgeTest
51 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
53 private ArchivaConfiguration archivaConfiguration;
55 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
56 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
58 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
59 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
61 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
62 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
65 MetadataTools metadataTools;
74 RepositoryContentFactory factory =
75 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
76 RepositoryContentFactory.class );
78 archivaConfiguration =
79 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
81 listenerControl = EasyMock.createControl( );
83 listener = listenerControl.createMock( RepositoryListener.class );
84 List<RepositoryListener> listeners = Collections.singletonList( listener );
85 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
86 applicationContext.getBean(
87 ManagedRepositoryAdmin.class ), factory,
88 repositorySession, listeners );
90 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
91 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
96 public void testReleasedSnapshotsExistsInSameRepo()
99 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
100 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
101 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
103 String repoRoot = prepareTestRepos();
105 // test listeners for the correct artifacts
106 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
107 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
108 listenerControl.replay();
110 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
112 listenerControl.verify();
114 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
116 // check if the snapshot was removed
117 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
118 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
119 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
120 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
121 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
122 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
123 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
125 // check if the released version was not removed
126 assertExists( projectRoot + "/2.3" );
127 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
128 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
129 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
130 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
131 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
132 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
133 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
134 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
135 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
137 // check if metadata file was updated
138 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
140 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
142 String expectedVersions =
143 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
145 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
146 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
147 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
148 "//metadata/versioning/versions/version", metadataXml );
149 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
153 public void testNonArtifactFile()
157 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
158 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
159 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
161 String repoRoot = prepareTestRepos();
163 // test listeners for the correct artifacts
164 listenerControl.replay();
166 File file = new File( repoRoot, INDEX_PATH );
167 if ( !file.exists() )
169 // help windauze to create directory with .
170 file.getParentFile().mkdirs();
171 file.createNewFile();
173 assertTrue( file.exists() );
175 repoPurge.process( INDEX_PATH );
177 listenerControl.verify();
179 assertTrue( file.exists() );
183 public void testReleasedSnapshotsExistsInDifferentRepo()
187 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
188 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
189 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
191 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
192 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
194 String repoRoot = prepareTestRepos();
196 // test listeners for the correct artifacts
197 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
198 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
199 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
200 listenerControl.replay();
202 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
204 listenerControl.verify();
206 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
208 // check if the snapshot was removed
209 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
210 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
211 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
212 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
213 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
214 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
215 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
217 String releasesProjectRoot =
218 AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
219 + "/org/apache/archiva/released-artifact-in-diff-repo" );
221 // check if the released version was not removed
222 assertExists( releasesProjectRoot + "/1.0" );
223 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
224 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
225 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
226 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
227 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
228 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
230 // remove RELEASES_TEST_REPO_ID so this test will be more independant
231 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
235 public void testHigherSnapshotExistsInSameRepo()
239 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
240 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
241 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
243 String repoRoot = prepareTestRepos();
245 // test listeners for the correct artifacts - no deletions
246 listenerControl.replay();
248 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
250 listenerControl.verify();
252 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
254 // check if the snapshot was not removed
255 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
256 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
257 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
258 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
259 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
260 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
261 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
263 // check if the released version was not removed
264 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
265 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
266 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
267 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
268 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
269 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
270 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
272 // check if metadata file was not updated (because nothing was removed)
273 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
275 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
277 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
278 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
280 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
281 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
282 "//metadata/versioning/versions/version", metadataXml );
283 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );