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 );
92 public void testReleasedSnapshotsExistsInSameRepo()
95 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
96 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
97 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
99 String repoRoot = prepareTestRepos();
101 // test listeners for the correct artifacts
102 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
103 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
104 listenerControl.replay();
106 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
108 listenerControl.verify();
110 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
112 // check if the snapshot was removed
113 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
114 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
115 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
116 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
117 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
118 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
119 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
121 // check if the released version was not removed
122 assertExists( projectRoot + "/2.3" );
123 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
124 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
125 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
126 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
127 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
128 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
129 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
130 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
131 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
133 // check if metadata file was updated
134 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
136 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
138 String expectedVersions =
139 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
141 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
142 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
143 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
144 "//metadata/versioning/versions/version", metadataXml );
145 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
149 public void testNonArtifactFile()
153 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
154 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
155 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
157 String repoRoot = prepareTestRepos();
159 // test listeners for the correct artifacts
160 listenerControl.replay();
162 File file = new File( repoRoot, INDEX_PATH );
163 if ( !file.exists() )
165 // help windauze to create directory with .
166 file.getParentFile().mkdirs();
167 file.createNewFile();
169 assertTrue( file.exists() );
171 repoPurge.process( INDEX_PATH );
173 listenerControl.verify();
175 assertTrue( file.exists() );
179 public void testReleasedSnapshotsExistsInDifferentRepo()
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 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
188 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
190 String repoRoot = prepareTestRepos();
192 // test listeners for the correct artifacts
193 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
194 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
195 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
196 listenerControl.replay();
198 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
200 listenerControl.verify();
202 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
204 // check if the snapshot was removed
205 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
206 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
207 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
208 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
209 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
210 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
211 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
213 String releasesProjectRoot =
214 new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
215 + "/org/apache/archiva/released-artifact-in-diff-repo";
217 // check if the released version was not removed
218 assertExists( releasesProjectRoot + "/1.0" );
219 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
220 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
221 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
222 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
223 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
224 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
228 public void testHigherSnapshotExistsInSameRepo()
232 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
233 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
234 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
236 String repoRoot = prepareTestRepos();
238 // test listeners for the correct artifacts - no deletions
239 listenerControl.replay();
241 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
243 listenerControl.verify();
245 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
247 // check if the snapshot was not removed
248 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
249 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
250 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
251 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
252 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
253 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
254 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
256 // check if the released version was not removed
257 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
258 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
259 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
260 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
261 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
262 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
263 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
265 // check if metadata file was not updated (because nothing was removed)
266 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
268 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
270 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
271 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
273 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
274 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
275 "//metadata/versioning/versions/version", metadataXml );
276 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );