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.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.repository.TestRepositorySessionFactory;
26 import org.apache.commons.io.FileUtils;
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.custommonkey.xmlunit.XMLAssert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.springframework.test.context.ContextConfiguration;
42 @ContextConfiguration(
43 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
44 public class RepositoryPurgeConsumerTest
45 extends AbstractRepositoryPurgeTest
53 TestRepositorySessionFactory factory = applicationContext.getBean( TestRepositorySessionFactory.class );
54 factory.setRepository( metadataRepository );
58 public void testConsumption()
61 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
66 public void testConsumptionOfOtherMetadata()
69 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
73 private void cleanupFileTypes()
75 ArchivaConfiguration archivaConfiguration =
76 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
78 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
79 fileType.removePattern( "**/*.xml" );
82 private void assertNotConsumed( String path )
85 ArchivaConfiguration archivaConfiguration =
86 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
88 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
89 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
90 fileType.addPattern( "**/*.xml" );
93 //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
94 for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
96 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
98 KnownRepositoryContentConsumer repoPurgeConsumer =
99 applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
100 KnownRepositoryContentConsumer.class );
102 File repoLocation = new File( "target/test-" + getName() + "/test-repo" );
104 File localFile = new File( repoLocation, path );
106 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
107 BaseFile baseFile = new BaseFile( repoLocation, localFile );
108 predicate.setBasefile( baseFile );
110 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
113 private void setLastModified( String path )
115 File dir = new File( path );
116 File[] contents = dir.listFiles();
117 for ( int i = 0; i < contents.length; i++ )
119 contents[i].setLastModified( 1179382029 );
124 public void testConsumerByRetentionCount()
127 KnownRepositoryContentConsumer repoPurgeConsumer =
128 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
129 KnownRepositoryContentConsumer.class );
131 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
132 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
133 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
134 addRepoToConfiguration( "retention-count", repoConfiguration );
136 repoPurgeConsumer.beginScan( repoConfiguration, null );
138 String repoRoot = prepareTestRepos();
140 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
142 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
144 // assert if removed from repo
145 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
146 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
147 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
148 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
149 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
150 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
152 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
153 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
154 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
155 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
156 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
157 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
159 // assert if not removed from repo
160 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
161 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
162 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
163 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
164 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
165 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
167 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
168 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
169 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
170 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
171 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
172 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
174 removeRepoFromConfiguration( "retention-count", repoConfiguration );
177 private void addRepoToConfiguration( String configHint, ManagedRepository repoConfiguration )
180 ArchivaConfiguration archivaConfiguration =
181 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
182 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
183 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
185 Configuration configuration = archivaConfiguration.getConfiguration();
186 ManagedRepositoryConfiguration managedRepositoryConfiguration =
187 configuration.findManagedRepositoryById( repoConfiguration.getId() );
188 if ( managedRepositoryConfiguration != null )
190 configuration.removeManagedRepository( managedRepositoryConfiguration );
192 configuration.addManagedRepository( repoConfiguration );
194 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
195 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
197 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, false );
199 managedRepositoryAdmin.addManagedRepository( repoConfiguration, false, null );
202 private void removeRepoFromConfiguration( String configHint, ManagedRepository repoConfiguration )
205 ArchivaConfiguration archivaConfiguration =
206 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
208 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
209 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
211 Configuration configuration = archivaConfiguration.getConfiguration();
212 ManagedRepositoryConfiguration managedRepositoryConfiguration =
213 configuration.findManagedRepositoryById( repoConfiguration.getId() );
214 if ( managedRepositoryConfiguration != null )
216 configuration.removeManagedRepository( managedRepositoryConfiguration );
218 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
219 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
221 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, true );
226 public void testConsumerByDaysOld()
229 KnownRepositoryContentConsumer repoPurgeConsumer =
230 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
231 KnownRepositoryContentConsumer.class );
233 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
234 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
235 addRepoToConfiguration( "days-old", repoConfiguration );
237 repoPurgeConsumer.beginScan( repoConfiguration, null );
239 String repoRoot = prepareTestRepos();
240 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
242 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
244 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
246 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
247 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
248 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
249 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
250 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
251 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
253 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
254 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
255 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
256 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
257 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
258 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
259 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
261 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
262 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
263 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
264 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
265 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
266 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
268 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
269 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
270 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
271 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
272 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
273 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
275 removeRepoFromConfiguration( "days-old", repoConfiguration );
279 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
284 public void testReleasedSnapshotsWereNotCleaned()
287 KnownRepositoryContentConsumer repoPurgeConsumer =
288 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
289 KnownRepositoryContentConsumer.class );
291 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
292 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
293 addRepoToConfiguration( "retention-count", repoConfiguration );
295 repoPurgeConsumer.beginScan( repoConfiguration, null );
297 String repoRoot = prepareTestRepos();
299 repoPurgeConsumer.processFile(
300 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
302 // check if the snapshot wasn't removed
303 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
305 assertExists( projectRoot + "/2.3-SNAPSHOT" );
306 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
307 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
308 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
309 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
310 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
311 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
313 // check if metadata file wasn't updated
314 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
316 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
318 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
320 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
321 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
322 "//metadata/versioning/versions/version", metadataXml );
323 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
325 removeRepoFromConfiguration( "retention-count", repoConfiguration );
329 public void testReleasedSnapshotsWereCleaned()
332 KnownRepositoryContentConsumer repoPurgeConsumer =
333 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
334 KnownRepositoryContentConsumer.class );
336 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
337 repoConfiguration.setDeleteReleasedSnapshots( true );
338 addRepoToConfiguration( "days-old", repoConfiguration );
340 repoPurgeConsumer.beginScan( repoConfiguration, null );
342 String repoRoot = prepareTestRepos();
344 repoPurgeConsumer.processFile(
345 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
347 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
349 // check if the snapshot was removed
350 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
351 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
352 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
353 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
354 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
355 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
356 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
358 // check if metadata file was updated
359 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
361 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
363 String expectedVersions =
364 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
366 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
367 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
368 "//metadata/versioning/versions/version", metadataXml );
369 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
371 removeRepoFromConfiguration( "days-old", repoConfiguration );