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.database.ArchivaDatabaseException;
28 import org.custommonkey.xmlunit.XMLAssert;
31 import java.util.ArrayList;
32 import java.util.List;
35 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
37 public class RepositoryPurgeConsumerTest
38 extends AbstractRepositoryPurgeTest
40 private void setLastModified( String path )
42 File dir = new File( path );
43 File[] contents = dir.listFiles();
44 for ( int i = 0; i < contents.length; i++ )
46 contents[i].setLastModified( 1179382029 );
50 public void testConsumerByRetentionCount()
53 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
54 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
56 populateDbForRetentionCountTest();
58 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
59 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
60 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
61 addRepoToConfiguration( "retention-count", repoConfiguration );
63 repoPurgeConsumer.beginScan( repoConfiguration );
65 String repoRoot = prepareTestRepo();
67 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
69 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
71 // assert if removed from repo
72 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
73 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
74 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
75 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
76 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
77 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
79 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
80 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
81 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
82 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
83 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
84 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
86 // assert if not removed from repo
87 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
88 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
89 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
90 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
91 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
92 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
94 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
95 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
96 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
97 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
98 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
99 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
102 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
105 ArchivaConfiguration archivaConfiguration =
106 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
107 Configuration configuration = archivaConfiguration.getConfiguration();
108 configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
109 configuration.addManagedRepository( repoConfiguration );
112 public void testConsumerByDaysOld()
115 populateDbForDaysOldTest();
117 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
118 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
120 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
121 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
122 addRepoToConfiguration( "days-old", repoConfiguration );
124 repoPurgeConsumer.beginScan( repoConfiguration );
126 String repoRoot = prepareTestRepo();
127 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
129 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
131 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
133 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
134 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
135 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
136 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
137 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
138 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
142 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots
143 * based on released versions.
147 public void testReleasedSnapshotsWereNotCleaned()
150 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
151 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
153 populateDbForReleasedSnapshotsTest();
155 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
156 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
157 addRepoToConfiguration( "retention-count", repoConfiguration );
159 repoPurgeConsumer.beginScan( repoConfiguration );
161 String repoRoot = prepareTestRepo();
163 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
165 // check if the snapshot wasn't removed
166 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
168 assertExists( projectRoot + "/2.3-SNAPSHOT" );
169 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
170 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
171 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
172 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
173 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
174 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
176 // check if metadata file wasn't updated
177 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
179 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
181 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
183 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
184 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
185 "//metadata/versioning/versions/version", metadataXml );
186 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
189 public void testReleasedSnapshotsWereCleaned()
192 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
193 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
195 populateDbForReleasedSnapshotsTest();
197 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
198 repoConfiguration.setDeleteReleasedSnapshots( true );
199 addRepoToConfiguration( "days-old", repoConfiguration );
201 repoPurgeConsumer.beginScan( repoConfiguration );
203 String repoRoot = prepareTestRepo();
205 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
207 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
209 // check if the snapshot was removed
210 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
211 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
212 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
213 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
214 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
215 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
216 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
218 // check if metadata file was updated
219 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
221 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
223 String expectedVersions =
224 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
226 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
227 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
228 "//metadata/versioning/versions/version", metadataXml );
229 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
232 public void populateDbForRetentionCountTest()
233 throws ArchivaDatabaseException
235 List<String> versions = new ArrayList<String>();
236 versions.add( "1.0RC1-20070504.153317-1" );
237 versions.add( "1.0RC1-20070504.160758-2" );
238 versions.add( "1.0RC1-20070505.090015-3" );
239 versions.add( "1.0RC1-20070506.090132-4" );
241 populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
244 private void populateDbForDaysOldTest()
245 throws ArchivaDatabaseException
247 List<String> versions = new ArrayList<String>();
248 versions.add( "2.2-SNAPSHOT" );
250 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
253 public void populateDbForReleasedSnapshotsTest()
254 throws ArchivaDatabaseException
256 List<String> versions = new ArrayList<String>();
257 versions.add( "2.3-SNAPSHOT" );
259 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );