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.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub;
26 import org.apache.maven.archiva.database.ArchivaDatabaseException;
27 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.metadata.MetadataTools;
30 import org.custommonkey.xmlunit.XMLAssert;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
39 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
41 public class CleanupReleasedSnapshotsRepositoryPurgeTest
42 extends AbstractRepositoryPurgeTest
44 private ArchivaConfiguration archivaConfiguration;
46 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
47 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
49 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";
51 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";
53 protected void setUp()
58 Map<String, RepositoryContentIndex> map = new HashMap<String, RepositoryContentIndex>();
59 map.put( "filecontent", new LuceneRepositoryContentIndexStub() );
60 map.put( "hashcodes", new LuceneRepositoryContentIndexStub() );
61 map.put( "bytecode", new LuceneRepositoryContentIndexStub() );
63 MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class );
64 RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots");
66 archivaConfiguration =
67 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" );
70 new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), dao, metadataTools, map, archivaConfiguration, factory );
73 public void testReleasedSnapshotsExistsInSameRepo()
77 Configuration config = archivaConfiguration.getConfiguration();
78 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
79 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
81 populateReleasedSnapshotsTest();
83 String repoRoot = prepareTestRepos();
85 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
87 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
89 // check if the snapshot was removed
90 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
91 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
92 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
93 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
94 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
95 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
96 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
98 // check if the released version was not removed
99 assertExists( projectRoot + "/2.3" );
100 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
101 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
102 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
103 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
104 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
105 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
106 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
107 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
108 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
110 // check if metadata file was updated
111 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
113 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
115 String expectedVersions = "<expected><versions><version>2.2</version>" +
116 "<version>2.3</version></versions></expected>";
118 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
119 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
120 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
121 "//metadata/versioning/versions/version", metadataXml );
122 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
125 public void testReleasedSnapshotsExistsInDifferentRepo()
128 Configuration config = archivaConfiguration.getConfiguration();
129 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
130 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
131 config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) );
133 populateReleasedSnapshotsTestInDiffRepo();
135 String repoRoot = prepareTestRepos();
137 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
139 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
141 // check if the snapshot was removed
142 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
143 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
144 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
145 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
146 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
147 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
148 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
150 String releasesProjectRoot =
151 getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
152 "/org/apache/archiva/released-artifact-in-diff-repo";
154 // check if the released version was not removed
155 assertExists( releasesProjectRoot + "/1.0" );
156 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
157 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
158 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
159 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
160 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
161 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
164 public void testHigherSnapshotExistsInSameRepo()
167 Configuration config = archivaConfiguration.getConfiguration();
168 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
169 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
171 populateHigherSnapshotExistsTest();
173 String repoRoot = prepareTestRepos();
175 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
177 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
179 // check if the snapshot was not removed
180 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
181 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
182 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
183 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
184 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
185 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
186 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
188 // check if the released version was not removed
189 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
190 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
191 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
192 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
193 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
194 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
195 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
197 // check if metadata file was not updated (because nothing was removed)
198 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
200 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
202 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
203 "<version>2.0.4-SNAPSHOT</version></versions></expected>";
205 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
206 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
207 "//metadata/versioning/versions/version", metadataXml );
208 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
211 private void populateReleasedSnapshotsTest()
212 throws ArchivaDatabaseException
214 List<String> versions = new ArrayList<String>();
215 versions.add( "2.3-SNAPSHOT" );
217 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );
220 private void populateHigherSnapshotExistsTest()
223 List<String> versions = new ArrayList<String>();
224 versions.add( "2.0.3-SNAPSHOT" );
226 populateDb( "org.apache.maven.plugins", "maven-source-plugin", versions );
229 private void populateReleasedSnapshotsTestInDiffRepo()
230 throws ArchivaDatabaseException
232 List<String> versions = new ArrayList<String>();
233 versions.add( "1.0-SNAPSHOT" );
235 populateDb( "org.apache.archiva", "released-artifact-in-diff-repo", versions );