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.common.utils.BaseFile;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.FileType;
28 import org.apache.archiva.configuration.FileTypes;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
31 import org.apache.archiva.metadata.repository.TestRepositorySessionFactory;
32 import org.apache.commons.io.FileUtils;
33 import org.custommonkey.xmlunit.XMLAssert;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.springframework.test.context.ContextConfiguration;
43 @ContextConfiguration(
44 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
45 public class RepositoryPurgeConsumerTest
46 extends AbstractRepositoryPurgeTest
54 TestRepositorySessionFactory factory = applicationContext.getBean( TestRepositorySessionFactory.class );
55 factory.setRepository( metadataRepository );
59 public void tearDown()
66 public void testConsumption()
69 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
74 public void testConsumptionOfOtherMetadata()
77 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
81 private void cleanupFileTypes()
83 ArchivaConfiguration archivaConfiguration =
84 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
86 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
87 fileType.removePattern( "**/*.xml" );
90 private void assertNotConsumed( String path )
93 ArchivaConfiguration archivaConfiguration =
94 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
96 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
97 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
98 fileType.addPattern( "**/*.xml" );
101 //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
102 for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
104 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
106 KnownRepositoryContentConsumer repoPurgeConsumer =
107 applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
108 KnownRepositoryContentConsumer.class );
110 File repoLocation = new File( "target/test-" + getName() + "/test-repo" );
112 File localFile = new File( repoLocation, path );
114 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
115 BaseFile baseFile = new BaseFile( repoLocation, localFile );
116 predicate.setBasefile( baseFile );
118 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
121 private void setLastModified( String path )
123 File dir = new File( path );
124 File[] contents = dir.listFiles();
125 for ( int i = 0; i < contents.length; i++ )
127 contents[i].setLastModified( 1179382029 );
132 public void testConsumerByRetentionCount()
135 KnownRepositoryContentConsumer repoPurgeConsumer =
136 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
137 KnownRepositoryContentConsumer.class );
139 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
140 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
141 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
142 addRepoToConfiguration( "retention-count", repoConfiguration );
144 repoPurgeConsumer.beginScan( repoConfiguration, null );
146 String repoRoot = prepareTestRepos();
148 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
150 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
152 // assert if removed from repo
153 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
154 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar" );
155 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip" );
156 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
157 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
158 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
159 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
160 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
162 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
163 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar" );
164 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip" );
165 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
166 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
167 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
168 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
169 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
171 // assert if not removed from repo
172 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
173 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.jar" );
174 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.zip" );
175 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
176 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
177 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
178 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
179 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
181 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
182 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
183 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
184 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
185 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
186 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
188 removeRepoFromConfiguration( "retention-count", repoConfiguration );
191 private void addRepoToConfiguration( String configHint, ManagedRepository repoConfiguration )
194 ArchivaConfiguration archivaConfiguration =
195 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
196 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
197 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
199 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
200 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
202 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, false );
204 managedRepositoryAdmin.addManagedRepository( repoConfiguration, false, null );
207 private void removeRepoFromConfiguration( String configHint, ManagedRepository repoConfiguration )
210 ArchivaConfiguration archivaConfiguration =
211 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
213 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
214 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
216 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
217 if ( managedRepositoryAdmin.getManagedRepository( repoConfiguration.getId() ) != null )
219 managedRepositoryAdmin.deleteManagedRepository( repoConfiguration.getId(), null, true );
224 public void testConsumerByDaysOld()
227 KnownRepositoryContentConsumer repoPurgeConsumer =
228 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
229 KnownRepositoryContentConsumer.class );
231 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
232 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
233 addRepoToConfiguration( "days-old", repoConfiguration );
235 repoPurgeConsumer.beginScan( repoConfiguration, null );
237 String repoRoot = prepareTestRepos();
238 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
240 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
242 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
244 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
245 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
246 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
247 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
248 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
249 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
251 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
252 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
253 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
254 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
255 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
256 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
257 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
259 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
260 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
261 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
262 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
263 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
264 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
266 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
267 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
268 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
269 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
270 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
271 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
273 removeRepoFromConfiguration( "days-old", repoConfiguration );
277 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
282 public void testReleasedSnapshotsWereNotCleaned()
285 KnownRepositoryContentConsumer repoPurgeConsumer =
286 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
287 KnownRepositoryContentConsumer.class );
289 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
290 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
291 addRepoToConfiguration( "retention-count", repoConfiguration );
293 repoPurgeConsumer.beginScan( repoConfiguration, null );
295 String repoRoot = prepareTestRepos();
297 repoPurgeConsumer.processFile(
298 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
300 // check if the snapshot wasn't removed
301 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
303 assertExists( projectRoot + "/2.3-SNAPSHOT" );
304 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
305 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
306 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
307 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
308 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
309 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
311 // check if metadata file wasn't updated
312 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
314 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
316 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
318 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
319 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
320 "//metadata/versioning/versions/version", metadataXml );
321 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
323 removeRepoFromConfiguration( "retention-count", repoConfiguration );
327 public void testReleasedSnapshotsWereCleaned()
330 KnownRepositoryContentConsumer repoPurgeConsumer =
331 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
332 KnownRepositoryContentConsumer.class );
334 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
335 repoConfiguration.setDeleteReleasedSnapshots( true );
336 addRepoToConfiguration( "days-old", repoConfiguration );
338 repoPurgeConsumer.beginScan( repoConfiguration, null );
340 String repoRoot = prepareTestRepos();
342 repoPurgeConsumer.processFile(
343 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
345 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
347 // check if the snapshot was removed
348 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
349 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
350 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
351 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
352 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
353 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
354 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
356 // check if metadata file was updated
357 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
359 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
361 String expectedVersions =
362 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
364 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
365 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
366 "//metadata/versioning/versions/version", metadataXml );
367 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
369 removeRepoFromConfiguration( "days-old", repoConfiguration );