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.archiva.metadata.repository.TestRepositorySessionFactory;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.common.utils.BaseFile;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.FileType;
28 import org.apache.maven.archiva.configuration.FileTypes;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.maven.archiva.consumers.functors.ConsumerWantsFilePredicate;
32 import org.custommonkey.xmlunit.XMLAssert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.springframework.test.context.ContextConfiguration;
41 @ContextConfiguration(
42 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
43 public class RepositoryPurgeConsumerTest
44 extends AbstractRepositoryPurgeTest
47 public void testConsumption()
50 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
55 public void testConsumptionOfOtherMetadata()
58 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
62 private void cleanupFileTypes()
64 ArchivaConfiguration archivaConfiguration =
65 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
67 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
68 fileType.removePattern( "**/*.xml" );
71 private void assertNotConsumed( String path )
74 ArchivaConfiguration archivaConfiguration =
75 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
77 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
78 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
79 fileType.addPattern( "**/*.xml" );
82 //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
83 for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
85 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
87 KnownRepositoryContentConsumer repoPurgeConsumer =
88 applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
89 KnownRepositoryContentConsumer.class );
91 File repoLocation = new File( "target/test-" + getName() + "/test-repo" );
93 File localFile = new File( repoLocation, path );
95 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
96 BaseFile baseFile = new BaseFile( repoLocation, localFile );
97 predicate.setBasefile( baseFile );
99 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
102 private void setLastModified( String path )
104 File dir = new File( path );
105 File[] contents = dir.listFiles();
106 for ( int i = 0; i < contents.length; i++ )
108 contents[i].setLastModified( 1179382029 );
113 public void testConsumerByRetentionCount()
116 KnownRepositoryContentConsumer repoPurgeConsumer =
117 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
118 KnownRepositoryContentConsumer.class );
120 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
121 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
122 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
123 addRepoToConfiguration( "retention-count", repoConfiguration );
125 repoPurgeConsumer.beginScan( repoConfiguration, null );
127 String repoRoot = prepareTestRepos();
129 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
131 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
133 // assert if removed from repo
134 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
135 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
136 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
137 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
138 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
139 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
141 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
142 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
143 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
144 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
145 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
146 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
148 // assert if not removed from repo
149 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
150 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
151 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
152 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
153 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
154 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
156 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
157 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
158 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
159 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
160 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
161 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
163 removeRepoFromConfiguration( "retention-count", repoConfiguration );
166 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
169 ArchivaConfiguration archivaConfiguration =
170 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
171 Configuration configuration = archivaConfiguration.getConfiguration();
172 ManagedRepositoryConfiguration managedRepositoryConfiguration =
173 configuration.findManagedRepositoryById( repoConfiguration.getId() );
174 if ( managedRepositoryConfiguration != null )
176 configuration.removeManagedRepository( managedRepositoryConfiguration );
178 configuration.addManagedRepository( repoConfiguration );
181 private void removeRepoFromConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
184 ArchivaConfiguration archivaConfiguration =
185 applicationContext.getBean( "archivaConfiguration#" + configHint, ArchivaConfiguration.class );
186 Configuration configuration = archivaConfiguration.getConfiguration();
187 ManagedRepositoryConfiguration managedRepositoryConfiguration =
188 configuration.findManagedRepositoryById( repoConfiguration.getId() );
189 if ( managedRepositoryConfiguration != null )
191 configuration.removeManagedRepository( managedRepositoryConfiguration );
196 public void testConsumerByDaysOld()
199 KnownRepositoryContentConsumer repoPurgeConsumer =
200 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
201 KnownRepositoryContentConsumer.class );
203 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
204 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
205 addRepoToConfiguration( "days-old", repoConfiguration );
207 repoPurgeConsumer.beginScan( repoConfiguration, null );
209 String repoRoot = prepareTestRepos();
210 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
212 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
214 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
216 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
217 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
218 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
219 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
220 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
221 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
223 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
224 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
225 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
226 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
227 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
228 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
229 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
231 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
232 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
233 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
234 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
235 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
236 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
238 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
239 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
240 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
241 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
242 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
243 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
245 removeRepoFromConfiguration( "days-old", repoConfiguration );
249 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
254 public void testReleasedSnapshotsWereNotCleaned()
257 KnownRepositoryContentConsumer repoPurgeConsumer =
258 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
259 KnownRepositoryContentConsumer.class );
261 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
262 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
263 addRepoToConfiguration( "retention-count", repoConfiguration );
265 repoPurgeConsumer.beginScan( repoConfiguration, null );
267 String repoRoot = prepareTestRepos();
269 repoPurgeConsumer.processFile(
270 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
272 // check if the snapshot wasn't removed
273 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
275 assertExists( projectRoot + "/2.3-SNAPSHOT" );
276 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
277 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
278 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
279 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
280 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
281 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
283 // check if metadata file wasn't updated
284 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
286 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
288 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
290 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
291 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
292 "//metadata/versioning/versions/version", metadataXml );
293 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
295 removeRepoFromConfiguration( "retention-count", repoConfiguration );
299 public void testReleasedSnapshotsWereCleaned()
302 KnownRepositoryContentConsumer repoPurgeConsumer =
303 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
304 KnownRepositoryContentConsumer.class );
306 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
307 repoConfiguration.setDeleteReleasedSnapshots( true );
308 addRepoToConfiguration( "days-old", repoConfiguration );
310 repoPurgeConsumer.beginScan( repoConfiguration, null );
312 String repoRoot = prepareTestRepos();
314 repoPurgeConsumer.processFile(
315 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
317 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
319 // check if the snapshot was removed
320 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
321 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
322 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
323 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
324 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
325 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
326 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
328 // check if metadata file was updated
329 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
331 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
333 String expectedVersions =
334 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
336 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
337 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
338 "//metadata/versioning/versions/version", metadataXml );
339 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
341 removeRepoFromConfiguration( "days-old", repoConfiguration );
350 TestRepositorySessionFactory factory = applicationContext.getBean( TestRepositorySessionFactory.class );
351 factory.setRepository( metadataRepository );