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
23 import java.util.Collections;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.events.RepositoryListener;
30 import org.apache.maven.archiva.repository.metadata.MetadataTools;
31 import org.custommonkey.xmlunit.XMLAssert;
32 import org.easymock.MockControl;
37 public class CleanupReleasedSnapshotsRepositoryPurgeTest
38 extends AbstractRepositoryPurgeTest
40 private ArchivaConfiguration archivaConfiguration;
42 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
43 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
45 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";
47 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";
50 protected void setUp()
55 MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class );
56 RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots");
58 archivaConfiguration =
59 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" );
61 listenerControl = MockControl.createControl( RepositoryListener.class );
63 listener = (RepositoryListener) listenerControl.getMock();
65 new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, archivaConfiguration, factory,
66 Collections.singletonList( listener ) );
69 public void testReleasedSnapshotsExistsInSameRepo()
72 Configuration config = archivaConfiguration.getConfiguration();
73 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
74 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
76 String repoRoot = prepareTestRepos();
78 // test listeners for the correct artifacts
79 listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-plugin-plugin",
80 "2.3-SNAPSHOT", "maven-plugin" ) );
81 listenerControl.replay();
83 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
85 listenerControl.verify();
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 String repoRoot = prepareTestRepos();
135 // test listeners for the correct artifacts
136 listener.deleteArtifact( getRepository(), createArtifact( "org.apache.archiva",
137 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
139 listenerControl.replay();
141 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
143 listenerControl.verify();
145 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
147 // check if the snapshot was removed
148 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
149 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
150 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
151 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
152 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
153 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
154 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
156 String releasesProjectRoot =
157 getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
158 "/org/apache/archiva/released-artifact-in-diff-repo";
160 // check if the released version was not removed
161 assertExists( releasesProjectRoot + "/1.0" );
162 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
163 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
164 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
165 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
166 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
167 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
170 public void testHigherSnapshotExistsInSameRepo()
173 Configuration config = archivaConfiguration.getConfiguration();
174 config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
175 config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
177 String repoRoot = prepareTestRepos();
179 // test listeners for the correct artifacts - no deletions
180 listenerControl.replay();
182 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
184 listenerControl.verify();
186 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
188 // check if the snapshot was not removed
189 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
190 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
191 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
192 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
193 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
194 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
195 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
197 // check if the released version was not removed
198 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
199 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
200 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
201 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
202 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
203 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
204 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
206 // check if metadata file was not updated (because nothing was removed)
207 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
209 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
211 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
212 "<version>2.0.4-SNAPSHOT</version></versions></expected>";
214 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
215 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
216 "//metadata/versioning/versions/version", metadataXml );
217 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );