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;
32 import java.util.Collections;
37 public class CleanupReleasedSnapshotsRepositoryPurgeTest
38 extends AbstractRepositoryPurgeTest
40 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
42 private ArchivaConfiguration archivaConfiguration;
44 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
45 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
47 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO = "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
49 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO = "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
52 protected void setUp()
57 MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class );
58 RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots");
60 archivaConfiguration =
61 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" );
63 listenerControl = MockControl.createControl( RepositoryListener.class );
65 listener = (RepositoryListener) listenerControl.getMock();
67 new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, archivaConfiguration, factory,
68 Collections.singletonList( listener ) );
71 public void testReleasedSnapshotsExistsInSameRepo()
74 Configuration config = archivaConfiguration.getConfiguration();
75 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
76 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
78 String repoRoot = prepareTestRepos();
80 // test listeners for the correct artifacts
81 listener.deleteArtifact( getRepository().getId(), "org.apache.maven.plugins", "maven-plugin-plugin",
82 "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
83 listenerControl.replay();
85 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
87 listenerControl.verify();
89 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
91 // check if the snapshot was removed
92 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
93 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
94 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
95 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
96 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
97 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
98 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
100 // check if the released version was not removed
101 assertExists( projectRoot + "/2.3" );
102 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
103 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
104 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
105 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
106 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
107 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
108 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
109 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
110 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
112 // check if metadata file was updated
113 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
115 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
117 String expectedVersions = "<expected><versions><version>2.2</version>" +
118 "<version>2.3</version></versions></expected>";
120 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
121 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
122 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
123 "//metadata/versioning/versions/version", metadataXml );
124 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
127 public void testNonArtifactFile()
130 Configuration config = archivaConfiguration.getConfiguration();
131 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
132 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
134 String repoRoot = prepareTestRepos();
136 // test listeners for the correct artifacts
137 listenerControl.replay();
139 File file = new File( repoRoot, INDEX_PATH );
140 file.createNewFile();
141 assertTrue( file.exists() );
143 repoPurge.process( INDEX_PATH );
145 listenerControl.verify();
147 assertTrue( file.exists() );
150 public void testReleasedSnapshotsExistsInDifferentRepo()
153 Configuration config = archivaConfiguration.getConfiguration();
154 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
155 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
156 config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) );
158 String repoRoot = prepareTestRepos();
160 // test listeners for the correct artifacts
161 listener.deleteArtifact( getRepository().getId(), "org.apache.archiva", "released-artifact-in-diff-repo",
162 "1.0-SNAPSHOT", "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
163 listenerControl.replay();
165 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
167 listenerControl.verify();
169 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
171 // check if the snapshot was removed
172 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
173 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
174 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
175 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
176 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
177 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
178 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
180 String releasesProjectRoot =
181 getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
182 "/org/apache/archiva/released-artifact-in-diff-repo";
184 // check if the released version was not removed
185 assertExists( releasesProjectRoot + "/1.0" );
186 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
187 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
188 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
189 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
190 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
191 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
194 public void testHigherSnapshotExistsInSameRepo()
197 Configuration config = archivaConfiguration.getConfiguration();
198 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
199 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
201 String repoRoot = prepareTestRepos();
203 // test listeners for the correct artifacts - no deletions
204 listenerControl.replay();
206 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
208 listenerControl.verify();
210 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
212 // check if the snapshot was not removed
213 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
214 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
215 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
216 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
217 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
218 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
219 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
221 // check if the released version was not removed
222 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
223 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
224 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
225 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
226 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
227 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
228 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
230 // check if metadata file was not updated (because nothing was removed)
231 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
233 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
235 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
236 "<version>2.0.4-SNAPSHOT</version></versions></expected>";
238 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
239 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
240 "//metadata/versioning/versions/version", metadataXml );
241 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );