1 package org.apache.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.admin.model.RepositoryCommonValidator;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
26 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
27 import org.apache.archiva.common.utils.BaseFile;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.FileType;
30 import org.apache.archiva.configuration.FileTypes;
31 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
33 import org.apache.archiva.metadata.repository.TestRepositorySessionFactory;
34 import org.apache.commons.io.FileUtils;
35 import org.custommonkey.xmlunit.XMLAssert;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.springframework.test.context.ContextConfiguration;
42 import java.nio.charset.Charset;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
49 @ContextConfiguration (
50 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
51 public class RepositoryPurgeConsumerTest
52 extends AbstractRepositoryPurgeTest
61 TestRepositorySessionFactory factory = applicationContext.getBean( TestRepositorySessionFactory.class );
62 factory.setRepository( metadataRepository );
67 public void tearDown()
74 public void testConsumption()
77 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
82 public void testConsumptionOfOtherMetadata()
85 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
89 private void cleanupFileTypes()
91 ArchivaConfiguration archivaConfiguration =
92 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
94 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
95 fileType.removePattern( "**/*.xml" );
98 private void assertNotConsumed( String path )
101 ArchivaConfiguration archivaConfiguration =
102 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
104 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
105 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
106 fileType.addPattern( "**/*.xml" );
109 //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
110 for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
112 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
114 KnownRepositoryContentConsumer repoPurgeConsumer =
115 applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
116 KnownRepositoryContentConsumer.class );
118 File repoLocation = new File( "target/test-" + getName() + "/test-repo" );
120 File localFile = new File( repoLocation, path );
122 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
123 BaseFile baseFile = new BaseFile( repoLocation, localFile );
124 predicate.setBasefile( baseFile );
126 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
129 private void setLastModified( String path )
131 File dir = new File( path );
132 File[] contents = dir.listFiles();
133 for ( int i = 0; i < contents.length; i++ )
135 contents[i].setLastModified( 1179382029 );
140 public void testConsumerByRetentionCount()
143 KnownRepositoryContentConsumer repoPurgeConsumer =
144 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
145 KnownRepositoryContentConsumer.class );
147 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
148 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
149 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
150 addRepoToConfiguration( "retention-count", repoConfiguration );
152 repoPurgeConsumer.beginScan( repoConfiguration, null );
154 String repoRoot = prepareTestRepos();
156 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
158 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
160 // assert if removed from repo
161 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
162 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar" );
163 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip" );
164 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
165 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
166 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
167 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
168 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
170 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
171 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar" );
172 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip" );
173 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
174 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
175 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
176 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
177 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
179 // assert if not removed from repo
180 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
181 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.jar" );
182 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.zip" );
183 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
184 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
185 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
186 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
187 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
189 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
190 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
191 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
192 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
193 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
194 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
196 removeRepoFromConfiguration( "retention-count", repoConfiguration );
199 private void addRepoToConfiguration( String configHint, ManagedRepository repoConfiguration )
202 ArchivaConfiguration archivaConfiguration =
203 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
204 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
205 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
206 // skygo: Default Validator was not looking at same config
207 ( (DefaultRepositoryCommonValidator) applicationContext.getBean(
208 RepositoryCommonValidator.class ) ).setArchivaConfiguration( archivaConfiguration );
209 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
210 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
212 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, false );
214 managedRepositoryAdmin.addManagedRepository( repoConfiguration, false, null );
217 private void removeRepoFromConfiguration( String configHint, ManagedRepository repoConfiguration )
220 ArchivaConfiguration archivaConfiguration =
221 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
223 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
224 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
225 // skygo: Default Validator was not looking at same config
226 ( (DefaultRepositoryCommonValidator) applicationContext.getBean(
227 RepositoryCommonValidator.class ) ).setArchivaConfiguration( archivaConfiguration );
228 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
229 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
231 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, true );
236 public void testConsumerByDaysOld()
239 KnownRepositoryContentConsumer repoPurgeConsumer =
240 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
241 KnownRepositoryContentConsumer.class );
243 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
244 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
245 addRepoToConfiguration( "days-old", repoConfiguration );
247 repoPurgeConsumer.beginScan( repoConfiguration, null );
249 String repoRoot = prepareTestRepos();
250 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
252 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
254 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
256 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
257 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
258 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
259 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
260 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
261 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
263 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
264 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
265 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
266 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
267 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
268 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
269 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
271 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
272 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
273 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
274 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
275 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
276 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
278 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
279 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
280 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
281 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
282 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
283 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
285 removeRepoFromConfiguration( "days-old", repoConfiguration );
289 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
294 public void testReleasedSnapshotsWereNotCleaned()
297 KnownRepositoryContentConsumer repoPurgeConsumer =
298 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
299 KnownRepositoryContentConsumer.class );
301 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
302 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
303 addRepoToConfiguration( "retention-count", repoConfiguration );
305 repoPurgeConsumer.beginScan( repoConfiguration, null );
307 String repoRoot = prepareTestRepos();
309 repoPurgeConsumer.processFile(
310 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
312 // check if the snapshot wasn't removed
313 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
315 assertExists( projectRoot + "/2.3-SNAPSHOT" );
316 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
317 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
318 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
319 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
320 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
321 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
323 // check if metadata file wasn't updated
324 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
326 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
328 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
330 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
331 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
332 "//metadata/versioning/versions/version", metadataXml );
333 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
335 removeRepoFromConfiguration( "retention-count", repoConfiguration );
339 public void testReleasedSnapshotsWereCleaned()
342 KnownRepositoryContentConsumer repoPurgeConsumer =
343 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
344 KnownRepositoryContentConsumer.class );
346 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
347 repoConfiguration.setDeleteReleasedSnapshots( true );
348 addRepoToConfiguration( "days-old", repoConfiguration );
350 repoPurgeConsumer.beginScan( repoConfiguration, null );
352 String repoRoot = prepareTestRepos();
354 repoPurgeConsumer.processFile(
355 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
357 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
359 // check if the snapshot was removed
360 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
361 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
362 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
363 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
364 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
365 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
366 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
368 // check if metadata file was updated
369 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
371 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
373 String expectedVersions =
374 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
376 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
377 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
378 "//metadata/versioning/versions/version", metadataXml );
379 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
381 removeRepoFromConfiguration( "days-old", repoConfiguration );