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
23 import java.nio.charset.Charset;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.inject.Inject;
27 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
28 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
29 import org.apache.archiva.configuration.ArchivaConfiguration;
30 import org.apache.archiva.repository.RepositoryContentFactory;
31 import org.apache.archiva.repository.events.RepositoryListener;
32 import org.apache.archiva.repository.metadata.MetadataTools;
33 import org.apache.commons.io.FileUtils;
34 import org.custommonkey.xmlunit.XMLAssert;
35 import org.easymock.MockControl;
36 import static org.junit.Assert.*;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.springframework.test.context.ContextConfiguration;
44 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
45 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
46 public class CleanupReleasedSnapshotsRepositoryPurgeTest
47 extends AbstractRepositoryPurgeTest
49 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
51 private ArchivaConfiguration archivaConfiguration;
53 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
54 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
56 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
57 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
59 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
60 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
63 MetadataTools metadataTools;
72 RepositoryContentFactory factory =
73 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
74 RepositoryContentFactory.class );
76 archivaConfiguration =
77 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
79 listenerControl = MockControl.createControl( RepositoryListener.class );
81 listener = (RepositoryListener) listenerControl.getMock();
82 List<RepositoryListener> listeners = Collections.singletonList( listener );
83 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
84 applicationContext.getBean(
85 ManagedRepositoryAdmin.class ), factory,
86 repositorySession, listeners );
88 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
89 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
94 public void testReleasedSnapshotsExistsInSameRepo()
97 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
98 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
99 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
101 String repoRoot = prepareTestRepos();
103 // test listeners for the correct artifacts
104 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
105 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
106 listenerControl.replay();
108 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
110 listenerControl.verify();
112 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
114 // check if the snapshot was removed
115 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
116 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
117 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
118 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
119 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
120 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
121 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
123 // check if the released version was not removed
124 assertExists( projectRoot + "/2.3" );
125 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
126 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
127 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
128 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
129 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
130 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
131 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
132 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
133 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
135 // check if metadata file was updated
136 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
138 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
140 String expectedVersions =
141 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
143 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
144 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
145 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
146 "//metadata/versioning/versions/version", metadataXml );
147 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
151 public void testNonArtifactFile()
155 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
156 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
157 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
159 String repoRoot = prepareTestRepos();
161 // test listeners for the correct artifacts
162 listenerControl.replay();
164 File file = new File( repoRoot, INDEX_PATH );
165 if ( !file.exists() )
167 // help windauze to create directory with .
168 file.getParentFile().mkdirs();
169 file.createNewFile();
171 assertTrue( file.exists() );
173 repoPurge.process( INDEX_PATH );
175 listenerControl.verify();
177 assertTrue( file.exists() );
181 public void testReleasedSnapshotsExistsInDifferentRepo()
185 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
186 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
187 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
189 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
190 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
192 String repoRoot = prepareTestRepos();
194 // test listeners for the correct artifacts
195 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
196 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
197 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
198 listenerControl.replay();
200 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
202 listenerControl.verify();
204 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
206 // check if the snapshot was removed
207 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
208 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
209 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
210 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
211 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
212 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
213 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
215 String releasesProjectRoot =
216 AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
217 + "/org/apache/archiva/released-artifact-in-diff-repo" );
219 // check if the released version was not removed
220 assertExists( releasesProjectRoot + "/1.0" );
221 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
222 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
223 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
224 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
225 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
226 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
228 // remove RELEASES_TEST_REPO_ID so this test will be more independant
229 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
233 public void testHigherSnapshotExistsInSameRepo()
237 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
238 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
239 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
241 String repoRoot = prepareTestRepos();
243 // test listeners for the correct artifacts - no deletions
244 listenerControl.replay();
246 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
248 listenerControl.verify();
250 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
252 // check if the snapshot was not removed
253 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
254 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
255 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
256 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
257 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
258 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
259 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
261 // check if the released version was not removed
262 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
263 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
264 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
265 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
266 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
267 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
268 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
270 // check if metadata file was not updated (because nothing was removed)
271 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
273 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
275 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
276 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
278 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
279 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
280 "//metadata/versioning/versions/version", metadataXml );
281 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );