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.common.utils.BaseFile;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.FileType;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.functors.ConsumerWantsFilePredicate;
31 import org.custommonkey.xmlunit.XMLAssert;
37 public class RepositoryPurgeConsumerTest
38 extends AbstractRepositoryPurgeTest
40 public void testConsumption()
43 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
46 public void testConsumptionOfOtherMetadata()
49 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
52 private void assertNotConsumed( String path )
55 ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
57 (FileType) archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
58 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
59 fileType.addPattern( "**/*.xml" );
62 FileTypes fileTypes = (FileTypes) lookup( FileTypes.class );
63 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
65 KnownRepositoryContentConsumer repoPurgeConsumer =
66 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repository-purge" );
68 File repoLocation = getTestFile( "target/test-" + getName() + "/test-repo" );
71 new File( repoLocation, path );
73 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
74 BaseFile baseFile = new BaseFile( repoLocation, localFile );
75 predicate.setBasefile( baseFile );
77 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
80 private void setLastModified( String path )
82 File dir = new File( path );
83 File[] contents = dir.listFiles();
84 for ( int i = 0; i < contents.length; i++ )
86 contents[i].setLastModified( 1179382029 );
90 public void testConsumerByRetentionCount()
93 KnownRepositoryContentConsumer repoPurgeConsumer =
94 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
95 "repo-purge-consumer-by-retention-count" );
97 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
98 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
99 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
100 addRepoToConfiguration( "retention-count", repoConfiguration );
102 repoPurgeConsumer.beginScan( repoConfiguration, null );
104 String repoRoot = prepareTestRepos();
106 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
108 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
110 // assert if removed from repo
111 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
112 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
113 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
114 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
115 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
116 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
118 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
119 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
120 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
121 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
122 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
123 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
125 // assert if not removed from repo
126 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
127 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
128 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
129 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
130 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
131 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
133 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
134 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
135 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
136 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
137 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
138 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
141 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
144 ArchivaConfiguration archivaConfiguration =
145 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
146 Configuration configuration = archivaConfiguration.getConfiguration();
147 configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
148 configuration.addManagedRepository( repoConfiguration );
151 public void testConsumerByDaysOld()
154 KnownRepositoryContentConsumer repoPurgeConsumer =
155 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
156 "repo-purge-consumer-by-days-old" );
158 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
159 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
160 addRepoToConfiguration( "days-old", repoConfiguration );
162 repoPurgeConsumer.beginScan( repoConfiguration, null );
164 String repoRoot = prepareTestRepos();
165 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
167 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
169 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
171 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
172 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
173 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
174 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
175 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
176 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
178 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
179 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
180 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
181 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
182 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
183 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
184 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
186 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
187 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
188 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
189 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
190 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
191 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
193 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
194 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
195 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
196 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
197 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
198 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
202 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
206 public void testReleasedSnapshotsWereNotCleaned()
209 KnownRepositoryContentConsumer repoPurgeConsumer =
210 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
211 "repo-purge-consumer-by-retention-count" );
213 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
214 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
215 addRepoToConfiguration( "retention-count", repoConfiguration );
217 repoPurgeConsumer.beginScan( repoConfiguration, null );
219 String repoRoot = prepareTestRepos();
221 repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
223 // check if the snapshot wasn't removed
224 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
226 assertExists( projectRoot + "/2.3-SNAPSHOT" );
227 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
228 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
229 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
230 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
231 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
232 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
234 // check if metadata file wasn't updated
235 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
237 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
239 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
241 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
242 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
243 "//metadata/versioning/versions/version", metadataXml );
244 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
247 public void testReleasedSnapshotsWereCleaned()
250 KnownRepositoryContentConsumer repoPurgeConsumer =
251 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
252 "repo-purge-consumer-by-days-old" );
254 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
255 repoConfiguration.setDeleteReleasedSnapshots( true );
256 addRepoToConfiguration( "days-old", repoConfiguration );
258 repoPurgeConsumer.beginScan( repoConfiguration, null );
260 String repoRoot = prepareTestRepos();
262 repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
264 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
266 // check if the snapshot was removed
267 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
268 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
269 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
270 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
271 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
272 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
273 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
275 // check if metadata file was updated
276 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
278 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
280 String expectedVersions =
281 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
283 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
284 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
285 "//metadata/versioning/versions/version", metadataXml );
286 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );