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
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.archiva.common.utils.BaseFile;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.FileType;
29 import org.apache.maven.archiva.configuration.FileTypes;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
33 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-20061118.060401-2.jar" );
187 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
188 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
189 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
190 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
191 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
195 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
199 public void testReleasedSnapshotsWereNotCleaned()
202 KnownRepositoryContentConsumer repoPurgeConsumer =
203 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
204 "repo-purge-consumer-by-retention-count" );
206 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
207 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
208 addRepoToConfiguration( "retention-count", repoConfiguration );
210 repoPurgeConsumer.beginScan( repoConfiguration, null );
212 String repoRoot = prepareTestRepos();
214 repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
216 // check if the snapshot wasn't removed
217 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
219 assertExists( projectRoot + "/2.3-SNAPSHOT" );
220 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
221 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
222 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
223 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
224 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
225 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
227 // check if metadata file wasn't updated
228 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
230 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
232 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
234 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
235 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
236 "//metadata/versioning/versions/version", metadataXml );
237 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
240 public void testReleasedSnapshotsWereCleaned()
243 KnownRepositoryContentConsumer repoPurgeConsumer =
244 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
245 "repo-purge-consumer-by-days-old" );
247 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
248 repoConfiguration.setDeleteReleasedSnapshots( true );
249 addRepoToConfiguration( "days-old", repoConfiguration );
251 repoPurgeConsumer.beginScan( repoConfiguration, null );
253 String repoRoot = prepareTestRepos();
255 repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
257 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
259 // check if the snapshot was removed
260 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
261 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
262 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
263 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
264 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
265 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
266 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
268 // check if metadata file was updated
269 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
271 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
273 String expectedVersions =
274 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
276 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
277 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
278 "//metadata/versioning/versions/version", metadataXml );
279 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );