1 package org.apache.maven.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.repository.events.RepositoryListener;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.repository.RepositoryContentFactory;
27 import org.apache.maven.archiva.repository.metadata.MetadataTools;
28 import org.custommonkey.xmlunit.XMLAssert;
29 import org.easymock.MockControl;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.springframework.test.context.ContextConfiguration;
34 import javax.inject.Inject;
36 import java.util.Collections;
37 import java.util.List;
42 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
43 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
44 public class CleanupReleasedSnapshotsRepositoryPurgeTest
45 extends AbstractRepositoryPurgeTest
47 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
49 private ArchivaConfiguration archivaConfiguration;
51 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
52 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
54 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
55 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
57 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
58 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
61 MetadataTools metadataTools;
69 RepositoryContentFactory factory =
70 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots", RepositoryContentFactory.class );
72 archivaConfiguration =
73 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
75 listenerControl = MockControl.createControl( RepositoryListener.class );
77 listener = (RepositoryListener) listenerControl.getMock();
78 List<RepositoryListener> listeners = Collections.singletonList( listener );
80 new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, archivaConfiguration, factory,
81 repositorySession, listeners );
85 public void testReleasedSnapshotsExistsInSameRepo()
88 Configuration config = archivaConfiguration.getConfiguration();
89 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
90 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
92 String repoRoot = prepareTestRepos();
94 // test listeners for the correct artifacts
95 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
96 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
97 listenerControl.replay();
99 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
101 listenerControl.verify();
103 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
105 // check if the snapshot was removed
106 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
107 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
108 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
109 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
110 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
111 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
112 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
114 // check if the released version was not removed
115 assertExists( projectRoot + "/2.3" );
116 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
117 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
118 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
119 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
120 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
121 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
122 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
123 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
124 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
126 // check if metadata file was updated
127 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
129 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
131 String expectedVersions =
132 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
134 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
135 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
136 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
137 "//metadata/versioning/versions/version", metadataXml );
138 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
142 public void testNonArtifactFile()
145 Configuration config = archivaConfiguration.getConfiguration();
146 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
147 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
149 String repoRoot = prepareTestRepos();
151 // test listeners for the correct artifacts
152 listenerControl.replay();
154 File file = new File( repoRoot, INDEX_PATH );
157 // help windauze to create directory with .
158 file.getParentFile().mkdirs();
159 file.createNewFile();
161 assertTrue( file.exists() );
163 repoPurge.process( INDEX_PATH );
165 listenerControl.verify();
167 assertTrue( file.exists() );
171 public void testReleasedSnapshotsExistsInDifferentRepo()
174 Configuration config = archivaConfiguration.getConfiguration();
175 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
176 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
177 config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) );
179 String repoRoot = prepareTestRepos();
181 // test listeners for the correct artifacts
182 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
183 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
184 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
185 listenerControl.replay();
187 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
189 listenerControl.verify();
191 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
193 // check if the snapshot was removed
194 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
195 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
196 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
197 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
198 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
199 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
200 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
202 String releasesProjectRoot =
203 new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
204 + "/org/apache/archiva/released-artifact-in-diff-repo";
206 // check if the released version was not removed
207 assertExists( releasesProjectRoot + "/1.0" );
208 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
209 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
210 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
211 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
212 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
213 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
217 public void testHigherSnapshotExistsInSameRepo()
220 Configuration config = archivaConfiguration.getConfiguration();
221 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
222 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
224 String repoRoot = prepareTestRepos();
226 // test listeners for the correct artifacts - no deletions
227 listenerControl.replay();
229 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
231 listenerControl.verify();
233 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
235 // check if the snapshot was not removed
236 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
237 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
238 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
239 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
240 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
241 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
242 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
244 // check if the released version was not removed
245 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
246 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
247 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
248 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
249 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
250 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
251 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
253 // check if metadata file was not updated (because nothing was removed)
254 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
256 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
258 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
259 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
261 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
262 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
263 "//metadata/versioning/versions/version", metadataXml );
264 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );