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.repository.events.RepositoryListener;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.repository.RepositoryContentFactory;
28 import org.apache.archiva.repository.metadata.MetadataTools;
29 import org.custommonkey.xmlunit.XMLAssert;
30 import org.easymock.MockControl;
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.util.Collections;
38 import java.util.List;
43 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
44 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
45 public class CleanupReleasedSnapshotsRepositoryPurgeTest
46 extends AbstractRepositoryPurgeTest
48 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
50 private ArchivaConfiguration archivaConfiguration;
52 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
53 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
55 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
56 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
58 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
59 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
62 MetadataTools metadataTools;
70 RepositoryContentFactory factory =
71 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
72 RepositoryContentFactory.class );
74 archivaConfiguration =
75 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
77 listenerControl = MockControl.createControl( RepositoryListener.class );
79 listener = (RepositoryListener) listenerControl.getMock();
80 List<RepositoryListener> listeners = Collections.singletonList( listener );
81 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
82 applicationContext.getBean(
83 ManagedRepositoryAdmin.class ), factory,
84 repositorySession, listeners );
86 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
87 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
91 public void testReleasedSnapshotsExistsInSameRepo()
95 Configuration config = archivaConfiguration.getConfiguration();
96 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
97 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
100 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
101 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
102 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
104 String repoRoot = prepareTestRepos();
106 // test listeners for the correct artifacts
107 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
108 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
109 listenerControl.replay();
111 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
113 listenerControl.verify();
115 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
117 // check if the snapshot was removed
118 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
119 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
120 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
121 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
122 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
123 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
124 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
126 // check if the released version was not removed
127 assertExists( projectRoot + "/2.3" );
128 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
129 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
130 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
131 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
132 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
133 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
134 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
135 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
136 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
138 // check if metadata file was updated
139 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
141 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
143 String expectedVersions =
144 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
146 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
147 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
148 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
149 "//metadata/versioning/versions/version", metadataXml );
150 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
154 public void testNonArtifactFile()
157 /*Configuration config = archivaConfiguration.getConfiguration();
158 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
159 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );*/
161 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
162 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
163 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
165 String repoRoot = prepareTestRepos();
167 // test listeners for the correct artifacts
168 listenerControl.replay();
170 File file = new File( repoRoot, INDEX_PATH );
171 if ( !file.exists() )
173 // help windauze to create directory with .
174 file.getParentFile().mkdirs();
175 file.createNewFile();
177 assertTrue( file.exists() );
179 repoPurge.process( INDEX_PATH );
181 listenerControl.verify();
183 assertTrue( file.exists() );
187 public void testReleasedSnapshotsExistsInDifferentRepo()
191 Configuration config = archivaConfiguration.getConfiguration();
192 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
193 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
194 config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) );
196 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
197 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
198 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
200 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
201 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
203 String repoRoot = prepareTestRepos();
205 // test listeners for the correct artifacts
206 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
207 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
208 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
209 listenerControl.replay();
211 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
213 listenerControl.verify();
215 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
217 // check if the snapshot was removed
218 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
219 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
220 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
221 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
222 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
223 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
224 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
226 String releasesProjectRoot =
227 new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
228 + "/org/apache/archiva/released-artifact-in-diff-repo";
230 // check if the released version was not removed
231 assertExists( releasesProjectRoot + "/1.0" );
232 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
233 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
234 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
235 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
236 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
237 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
241 public void testHigherSnapshotExistsInSameRepo()
245 Configuration config = archivaConfiguration.getConfiguration();
246 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
247 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
249 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
250 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
251 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
253 String repoRoot = prepareTestRepos();
255 // test listeners for the correct artifacts - no deletions
256 listenerControl.replay();
258 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
260 listenerControl.verify();
262 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
264 // check if the snapshot was not removed
265 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
266 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
267 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
268 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
269 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
270 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
271 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
273 // check if the released version was not removed
274 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
275 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
276 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
277 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
278 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
279 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
280 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
282 // check if metadata file was not updated (because nothing was removed)
283 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
285 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
287 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
288 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
290 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
291 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
292 "//metadata/versioning/versions/version", metadataXml );
293 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );