]> source.dussan.org Git - archiva.git/blob
f89cce1790cd09d45977275090ea1377905d0f86
[archiva.git] /
1 package org.apache.maven.archiva.consumers.core.repository;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
29
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
36  */
37 public class RepositoryPurgeConsumerTest
38     extends AbstractRepositoryPurgeTest
39 {
40     private void setLastModified( String path )
41     {
42         File dir = new File( path );
43         File[] contents = dir.listFiles();
44         for ( int i = 0; i < contents.length; i++ )
45         {
46             contents[i].setLastModified( 1179382029 );
47         }
48     }
49
50     public void testConsumerByRetentionCount()
51         throws Exception
52     {
53         KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
54             KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
55
56         populateDbForRetentionCountTest();
57
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 );
62
63         repoPurgeConsumer.beginScan( repoConfiguration );
64
65         String repoRoot = prepareTestRepo();
66
67         repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
68
69         String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
70
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" );
78
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" );
85
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" );
93
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" );
100     }
101
102     private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
103         throws Exception
104     {
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 );
110     }
111
112     public void testConsumerByDaysOld()
113         throws Exception
114     {
115         populateDbForDaysOldTest();
116
117         KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
118             KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
119
120         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
121         repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
122         addRepoToConfiguration( "days-old", repoConfiguration );
123
124         repoPurgeConsumer.beginScan( repoConfiguration );
125
126         String repoRoot = prepareTestRepo();
127         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
128
129         setLastModified( projectRoot + "/2.2-SNAPSHOT" );
130
131         repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
132
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" );
139     }
140
141     /**
142      * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots
143      * based on released versions.
144      *
145      * @throws Exception
146      */
147     public void testReleasedSnapshotsWereNotCleaned()
148         throws Exception
149     {
150         KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
151             KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" );
152
153         populateDbForReleasedSnapshotsTest();
154
155         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
156         repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
157         addRepoToConfiguration( "retention-count", repoConfiguration );
158
159         repoPurgeConsumer.beginScan( repoConfiguration );
160
161         String repoRoot = prepareTestRepo();
162
163         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
164
165         // check if the snapshot wasn't removed
166         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
167
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" );
175
176         // check if metadata file wasn't updated
177         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
178
179         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
180
181         String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
182
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 );
187     }
188
189     public void testReleasedSnapshotsWereCleaned()
190         throws Exception
191     {
192         KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup(
193             KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" );
194
195         populateDbForReleasedSnapshotsTest();
196
197         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
198         repoConfiguration.setDeleteReleasedSnapshots( true );
199         addRepoToConfiguration( "days-old", repoConfiguration );
200
201         repoPurgeConsumer.beginScan( repoConfiguration );
202
203         String repoRoot = prepareTestRepo();
204
205         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
206
207         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
208
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" );
217
218         // check if metadata file was updated
219         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
220
221         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
222
223         String expectedVersions =
224             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
225
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 );
230     }
231
232     public void populateDbForRetentionCountTest()
233         throws ArchivaDatabaseException
234     {
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" );
240
241         populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
242     }
243
244     private void populateDbForDaysOldTest()
245         throws ArchivaDatabaseException
246     {
247         List<String> versions = new ArrayList<String>();
248         versions.add( "2.2-SNAPSHOT" );
249
250         populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
251     }
252
253     public void populateDbForReleasedSnapshotsTest()
254         throws ArchivaDatabaseException
255     {
256         List<String> versions = new ArrayList<String>();
257         versions.add( "2.3-SNAPSHOT" );
258
259         populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );
260     }
261 }