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.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.maven.archiva.database.ArchivaDatabaseException;
27 import org.custommonkey.xmlunit.XMLAssert;
30 import java.util.ArrayList;
31 import java.util.List;
34 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
36 public class RepositoryPurgeConsumerTest
37 extends AbstractRepositoryPurgeTest
39 private void setLastModified( String path )
41 File dir = new File( path );
42 File[] contents = dir.listFiles();
43 for ( int i = 0; i < contents.length; i++ )
45 contents[i].setLastModified( 1179382029 );
49 public void testConsumerByRetentionCount()
52 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
53 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
55 populateDbForRetentionCountTest();
57 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
58 repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
59 repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
60 addRepoToConfiguration( "retention-count", repoConfiguration );
62 repoPurgeConsumer.beginScan( repoConfiguration );
64 String repoRoot = prepareTestRepo();
66 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
68 String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
70 // assert if removed from repo
71 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
72 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
73 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
74 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
75 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
76 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
78 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
79 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
80 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
81 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
82 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
83 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
85 // assert if not removed from repo
86 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
87 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
88 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
89 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
90 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
91 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
93 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
94 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
95 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
96 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
97 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
98 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
101 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
104 ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class,
106 archivaConfiguration.getConfiguration().addManagedRepository( repoConfiguration );
109 public void testConsumerByDaysOld()
112 populateDbForDaysOldTest();
114 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
115 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
117 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
118 repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
119 addRepoToConfiguration( "days-old", repoConfiguration );
121 repoPurgeConsumer.beginScan( repoConfiguration );
123 String repoRoot = prepareTestRepo();
124 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
126 setLastModified( projectRoot + "/2.2-SNAPSHOT" );
128 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
130 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
131 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
132 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
133 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
134 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
135 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
139 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots
140 * based on released versions.
144 public void testReleasedSnapshotsWereNotCleaned()
147 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
148 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
150 populateDbForReleasedSnapshotsTest();
152 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
153 repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
154 addRepoToConfiguration( "retention-count", repoConfiguration );
156 repoPurgeConsumer.beginScan( repoConfiguration );
158 String repoRoot = prepareTestRepo();
160 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
162 // check if the snapshot wasn't removed
163 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
165 assertExists( projectRoot + "/2.3-SNAPSHOT" );
166 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
167 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
168 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
169 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
170 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
171 assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
173 // check if metadata file wasn't updated
174 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
176 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
178 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
180 XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
181 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
182 "//metadata/versioning/versions/version", metadataXml );
183 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
186 public void testReleasedSnapshotsWereCleaned()
189 KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
190 KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
192 populateDbForReleasedSnapshotsTest();
194 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
195 repoConfiguration.setDeleteReleasedSnapshots( true );
196 addRepoToConfiguration( "days-old", repoConfiguration );
198 repoPurgeConsumer.beginScan( repoConfiguration );
200 String repoRoot = prepareTestRepo();
202 repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
204 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
206 // check if the snapshot was removed
207 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
208 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
209 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
210 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
211 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
212 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
213 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
215 // check if metadata file was updated
216 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
218 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
220 String expectedVersions = "<expected><versions><version>2.2</version>"
221 + "<version>2.3</version></versions></expected>";
223 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
224 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
225 "//metadata/versioning/versions/version", metadataXml );
226 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
229 public void populateDbForRetentionCountTest()
230 throws ArchivaDatabaseException
232 List<String> versions = new ArrayList<String>();
233 versions.add( "1.0RC1-20070504.153317-1" );
234 versions.add( "1.0RC1-20070504.160758-2" );
235 versions.add( "1.0RC1-20070505.090015-3" );
236 versions.add( "1.0RC1-20070506.090132-4" );
238 populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
241 private void populateDbForDaysOldTest()
242 throws ArchivaDatabaseException
244 List<String> versions = new ArrayList<String>();
245 versions.add( "2.2-SNAPSHOT" );
247 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
250 public void populateDbForReleasedSnapshotsTest()
251 throws ArchivaDatabaseException
253 List<String> versions = new ArrayList<String>();
254 versions.add( "2.3-SNAPSHOT" );
256 populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );