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.commons.io.FileUtils;
23 import org.apache.maven.archiva.common.utils.BaseFile;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.FileType;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub;
31 import org.apache.maven.archiva.database.ArchivaDatabaseException;
32 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
33 import org.custommonkey.xmlunit.XMLAssert;
36 import java.util.ArrayList;
37 import java.util.List;
40 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42 public class RepositoryPurgeConsumerTest
43 extends AbstractRepositoryPurgeTest
45 public void testConsumption()
48 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
51 public void testConsumptionOfOtherMetadata()
54 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
57 private void assertNotConsumed( String path )
60 ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
62 (FileType) archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
63 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
64 fileType.addPattern( "**/*.xml" );
67 FileTypes fileTypes = (FileTypes) lookup( FileTypes.class );
68 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
70 KnownRepositoryContentConsumer repoPurgeConsumer =
71 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repository-purge" );
73 File repoLocation = getTestFile( "target/test-" + getName() + "/test-repo" );
76 new File( repoLocation, path );
78 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
79 BaseFile baseFile = new BaseFile( repoLocation, localFile );
80 predicate.setBasefile( baseFile );
82 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
85 private void setLastModified( String path )
87 File dir = new File( path );
88 File[] contents = dir.listFiles();
89 for ( int i = 0; i < contents.length; i++ )
91 contents[i].setLastModified( 1179382029 );
95 public void testConsumerByRetentionCount()
98 KnownRepositoryContentConsumer repoPurgeConsumer =
99 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
100 "repo-purge-consumer-by-retention-count" );
102 LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
103 indexFactory.setExpectedRecordsSize( 2 );
105 ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
107 populateDbForRetentionCountTest();
109 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
110 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
111 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
112 addRepoToConfiguration( "retention-count", repoConfiguration );
114 repoPurgeConsumer.beginScan( repoConfiguration, null );
116 String repoRoot = prepareTestRepo();
118 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
120 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
122 // assert if removed from repo
123 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
124 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
125 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
126 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
127 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
128 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
130 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
131 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
132 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
133 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
134 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
135 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
137 // assert if not removed from repo
138 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
139 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
140 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
141 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
142 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
143 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
145 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
146 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
147 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
148 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
149 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
150 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
153 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
156 ArchivaConfiguration archivaConfiguration =
157 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
158 Configuration configuration = archivaConfiguration.getConfiguration();
159 configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
160 configuration.addManagedRepository( repoConfiguration );
163 public void testConsumerByDaysOld()
166 populateDbForDaysOldTest();
168 KnownRepositoryContentConsumer repoPurgeConsumer =
169 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
170 "repo-purge-consumer-by-days-old" );
172 LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
173 indexFactory.setExpectedRecordsSize( 2 );
175 ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
177 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
178 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
179 addRepoToConfiguration( "days-old", repoConfiguration );
181 repoPurgeConsumer.beginScan( repoConfiguration, null );
183 String repoRoot = prepareTestRepo();
184 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
186 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
188 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
190 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
191 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
192 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
193 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
194 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
195 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
197 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
198 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
199 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
200 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
201 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
202 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
203 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
205 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
206 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
207 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
208 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
209 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
210 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
214 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
218 public void testReleasedSnapshotsWereNotCleaned()
221 KnownRepositoryContentConsumer repoPurgeConsumer =
222 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
223 "repo-purge-consumer-by-retention-count" );
225 populateDbForReleasedSnapshotsTest();
227 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
228 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
229 addRepoToConfiguration( "retention-count", repoConfiguration );
231 repoPurgeConsumer.beginScan( repoConfiguration, null );
233 String repoRoot = prepareTestRepo();
235 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
237 // check if the snapshot wasn't removed
238 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
240 assertExists( projectRoot + "/2.3-SNAPSHOT" );
241 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
242 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
243 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
244 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
245 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
246 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
248 // check if metadata file wasn't updated
249 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
251 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
253 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
255 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
256 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
257 "//metadata/versioning/versions/version", metadataXml );
258 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
261 public void testReleasedSnapshotsWereCleaned()
264 KnownRepositoryContentConsumer repoPurgeConsumer =
265 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
266 "repo-purge-consumer-by-days-old" );
268 populateDbForReleasedSnapshotsTest();
270 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
271 repoConfiguration.setDeleteReleasedSnapshots( true );
272 addRepoToConfiguration( "days-old", repoConfiguration );
274 repoPurgeConsumer.beginScan( repoConfiguration, null );
276 String repoRoot = prepareTestRepo();
278 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
280 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
282 // check if the snapshot was removed
283 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
284 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
285 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
286 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
287 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
288 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
289 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
291 // check if metadata file was updated
292 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
294 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
296 String expectedVersions =
297 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
299 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
300 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
301 "//metadata/versioning/versions/version", metadataXml );
302 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
305 public void populateDbForRetentionCountTest()
306 throws ArchivaDatabaseException
308 List<String> versions = new ArrayList<String>();
309 versions.add( "1.0RC1-20070504.153317-1" );
310 versions.add( "1.0RC1-20070504.160758-2" );
311 versions.add( "1.0RC1-20070505.090015-3" );
312 versions.add( "1.0RC1-20070506.090132-4" );
314 populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
317 private void populateDbForDaysOldTest()
318 throws ArchivaDatabaseException
320 List<String> versions = new ArrayList<String>();
321 versions.add( "2.2-SNAPSHOT" );
323 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
326 public void populateDbForReleasedSnapshotsTest()
327 throws ArchivaDatabaseException
329 List<String> versions = new ArrayList<String>();
330 versions.add( "2.3-SNAPSHOT" );
332 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );