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.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
30 import org.custommonkey.xmlunit.XMLAssert;
33 import java.util.ArrayList;
34 import java.util.List;
37 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
39 public class RepositoryPurgeConsumerTest
40 extends AbstractRepositoryPurgeTest
42 private void setLastModified( String path )
44 File dir = new File( path );
45 File[] contents = dir.listFiles();
46 for ( int i = 0; i < contents.length; i++ )
48 contents[i].setLastModified( 1179382029 );
52 public void testConsumerByRetentionCount()
55 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
56 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
58 LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
59 indexFactory.setExpectedRecordsSize( 2 );
61 ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
63 populateDbForRetentionCountTest();
65 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
66 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
67 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
68 addRepoToConfiguration( "retention-count", repoConfiguration );
70 repoPurgeConsumer.beginScan( repoConfiguration );
72 String repoRoot = prepareTestRepo();
74 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
76 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
78 // assert if removed from repo
79 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
80 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
81 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
82 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
83 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
84 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
86 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
87 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
88 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
89 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
90 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
91 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
93 // assert if not removed from repo
94 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
95 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
96 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
97 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
98 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
99 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
101 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
102 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
103 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
104 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
105 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
106 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
109 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
112 ArchivaConfiguration archivaConfiguration =
113 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
114 Configuration configuration = archivaConfiguration.getConfiguration();
115 configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
116 configuration.addManagedRepository( repoConfiguration );
119 public void testConsumerByDaysOld()
122 populateDbForDaysOldTest();
124 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
125 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
127 LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
128 indexFactory.setExpectedRecordsSize( 2 );
130 ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
132 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
133 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
134 addRepoToConfiguration( "days-old", repoConfiguration );
136 repoPurgeConsumer.beginScan( repoConfiguration );
138 String repoRoot = prepareTestRepo();
139 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
141 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
143 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
145 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
146 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
147 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
148 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
149 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
150 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
152 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
153 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
154 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
155 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
156 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
157 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
158 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
160 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
161 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
162 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
163 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
164 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
165 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
169 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots
170 * based on released versions.
174 public void testReleasedSnapshotsWereNotCleaned()
177 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
178 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
180 populateDbForReleasedSnapshotsTest();
182 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
183 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
184 addRepoToConfiguration( "retention-count", repoConfiguration );
186 repoPurgeConsumer.beginScan( repoConfiguration );
188 String repoRoot = prepareTestRepo();
190 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
192 // check if the snapshot wasn't removed
193 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
195 assertExists( projectRoot + "/2.3-SNAPSHOT" );
196 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
197 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
198 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
199 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
200 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
201 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
203 // check if metadata file wasn't updated
204 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
206 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
208 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
210 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
211 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
212 "//metadata/versioning/versions/version", metadataXml );
213 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
216 public void testReleasedSnapshotsWereCleaned()
219 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
220 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
222 populateDbForReleasedSnapshotsTest();
224 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
225 repoConfiguration.setDeleteReleasedSnapshots( true );
226 addRepoToConfiguration( "days-old", repoConfiguration );
228 repoPurgeConsumer.beginScan( repoConfiguration );
230 String repoRoot = prepareTestRepo();
232 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
234 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
236 // check if the snapshot was removed
237 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
238 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
239 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
240 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
241 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
242 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
243 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
245 // check if metadata file was updated
246 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
248 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
250 String expectedVersions =
251 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
253 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
254 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
255 "//metadata/versioning/versions/version", metadataXml );
256 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
259 public void populateDbForRetentionCountTest()
260 throws ArchivaDatabaseException
262 List<String> versions = new ArrayList<String>();
263 versions.add( "1.0RC1-20070504.153317-1" );
264 versions.add( "1.0RC1-20070504.160758-2" );
265 versions.add( "1.0RC1-20070505.090015-3" );
266 versions.add( "1.0RC1-20070506.090132-4" );
268 populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
271 private void populateDbForDaysOldTest()
272 throws ArchivaDatabaseException
274 List<String> versions = new ArrayList<String>();
275 versions.add( "2.2-SNAPSHOT" );
277 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
280 public void populateDbForReleasedSnapshotsTest()
281 throws ArchivaDatabaseException
283 List<String> versions = new ArrayList<String>();
284 versions.add( "2.3-SNAPSHOT" );
286 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );