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.custommonkey.xmlunit.XMLAssert;
32 import java.util.ArrayList;
33 import java.util.List;
36 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
38 public class RepositoryPurgeConsumerTest
39 extends AbstractRepositoryPurgeTest
41 private void setLastModified( String path )
43 File dir = new File( path );
44 File[] contents = dir.listFiles();
45 for ( int i = 0; i < contents.length; i++ )
47 contents[i].setLastModified( 1179382029 );
51 public void testConsumerByRetentionCount()
54 KnownRepositoryContentConsumer repoPurgeConsumer =
55 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
56 "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 =
125 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
126 "repo-purge-consumer-by-days-old" );
128 LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
129 indexFactory.setExpectedRecordsSize( 2 );
131 ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
133 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
134 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
135 addRepoToConfiguration( "days-old", repoConfiguration );
137 repoPurgeConsumer.beginScan( repoConfiguration );
139 String repoRoot = prepareTestRepo();
140 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
142 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
144 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
146 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
147 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
148 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
149 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
150 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
151 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
153 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
154 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
155 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
156 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
157 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
158 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
159 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
161 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
162 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
163 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
164 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
165 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
166 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
170 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
174 public void testReleasedSnapshotsWereNotCleaned()
177 KnownRepositoryContentConsumer repoPurgeConsumer =
178 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
179 "repo-purge-consumer-by-retention-count" );
181 populateDbForReleasedSnapshotsTest();
183 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
184 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
185 addRepoToConfiguration( "retention-count", repoConfiguration );
187 repoPurgeConsumer.beginScan( repoConfiguration );
189 String repoRoot = prepareTestRepo();
191 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
193 // check if the snapshot wasn't removed
194 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
196 assertExists( projectRoot + "/2.3-SNAPSHOT" );
197 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
198 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
199 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
200 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
201 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
202 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
204 // check if metadata file wasn't updated
205 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
207 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
209 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
211 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
212 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
213 "//metadata/versioning/versions/version", metadataXml );
214 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
217 public void testReleasedSnapshotsWereCleaned()
220 KnownRepositoryContentConsumer repoPurgeConsumer =
221 (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
222 "repo-purge-consumer-by-days-old" );
224 populateDbForReleasedSnapshotsTest();
226 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
227 repoConfiguration.setDeleteReleasedSnapshots( true );
228 addRepoToConfiguration( "days-old", repoConfiguration );
230 repoPurgeConsumer.beginScan( repoConfiguration );
232 String repoRoot = prepareTestRepo();
234 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
236 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
238 // check if the snapshot was removed
239 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
240 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
241 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
242 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
243 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
244 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
245 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
247 // check if metadata file was updated
248 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
250 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
252 String expectedVersions =
253 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
255 XMLAssert.assertXpathEvaluatesTo( "2.3", "//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 populateDbForRetentionCountTest()
262 throws ArchivaDatabaseException
264 List<String> versions = new ArrayList<String>();
265 versions.add( "1.0RC1-20070504.153317-1" );
266 versions.add( "1.0RC1-20070504.160758-2" );
267 versions.add( "1.0RC1-20070505.090015-3" );
268 versions.add( "1.0RC1-20070506.090132-4" );
270 populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
273 private void populateDbForDaysOldTest()
274 throws ArchivaDatabaseException
276 List<String> versions = new ArrayList<String>();
277 versions.add( "2.2-SNAPSHOT" );
279 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
282 public void populateDbForReleasedSnapshotsTest()
283 throws ArchivaDatabaseException
285 List<String> versions = new ArrayList<String>();
286 versions.add( "2.3-SNAPSHOT" );
288 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );