]> source.dussan.org Git - archiva.git/blob
5073b74e314e5c10335d74c0b1240c46e48f4328
[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.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.custommonkey.xmlunit.XMLAssert;
30
31 import java.io.File;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
37  */
38 public class RepositoryPurgeConsumerTest
39     extends AbstractRepositoryPurgeTest
40 {
41     private void setLastModified( String path )
42     {
43         File dir = new File( path );
44         File[] contents = dir.listFiles();
45         for ( int i = 0; i < contents.length; i++ )
46         {
47             contents[i].setLastModified( 1179382029 );
48         }
49     }
50
51     public void testConsumerByRetentionCount()
52         throws Exception
53     {
54         KnownRepositoryContentConsumer repoPurgeConsumer =
55             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
56                                                      "repo-purge-consumer-by-retention-count" );
57
58         LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
59         indexFactory.setExpectedRecordsSize( 2 );
60
61         ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
62
63         populateDbForRetentionCountTest();
64
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 );
69
70         repoPurgeConsumer.beginScan( repoConfiguration );
71
72         String repoRoot = prepareTestRepo();
73
74         repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
75
76         String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
77
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" );
85
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" );
92
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" );
100
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" );
107     }
108
109     private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
110         throws Exception
111     {
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 );
117     }
118
119     public void testConsumerByDaysOld()
120         throws Exception
121     {
122         populateDbForDaysOldTest();
123
124         KnownRepositoryContentConsumer repoPurgeConsumer =
125             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
126                                                      "repo-purge-consumer-by-days-old" );
127
128         LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
129         indexFactory.setExpectedRecordsSize( 2 );
130
131         ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
132
133         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
134         repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
135         addRepoToConfiguration( "days-old", repoConfiguration );
136
137         repoPurgeConsumer.beginScan( repoConfiguration );
138
139         String repoRoot = prepareTestRepo();
140         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
141
142         setLastModified( projectRoot + "/2.2-SNAPSHOT" );
143
144         repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
145
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" );
152
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" );
160
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" );
167     }
168
169     /**
170      * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
171      * 
172      * @throws Exception
173      */
174     public void testReleasedSnapshotsWereNotCleaned()
175         throws Exception
176     {
177         KnownRepositoryContentConsumer repoPurgeConsumer =
178             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
179                                                      "repo-purge-consumer-by-retention-count" );
180
181         populateDbForReleasedSnapshotsTest();
182
183         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
184         repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
185         addRepoToConfiguration( "retention-count", repoConfiguration );
186
187         repoPurgeConsumer.beginScan( repoConfiguration );
188
189         String repoRoot = prepareTestRepo();
190
191         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
192
193         // check if the snapshot wasn't removed
194         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
195
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" );
203
204         // check if metadata file wasn't updated
205         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
206
207         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
208
209         String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
210
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 );
215     }
216
217     public void testReleasedSnapshotsWereCleaned()
218         throws Exception
219     {
220         KnownRepositoryContentConsumer repoPurgeConsumer =
221             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
222                                                      "repo-purge-consumer-by-days-old" );
223
224         populateDbForReleasedSnapshotsTest();
225
226         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
227         repoConfiguration.setDeleteReleasedSnapshots( true );
228         addRepoToConfiguration( "days-old", repoConfiguration );
229
230         repoPurgeConsumer.beginScan( repoConfiguration );
231
232         String repoRoot = prepareTestRepo();
233
234         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
235
236         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
237
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" );
246
247         // check if metadata file was updated
248         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
249
250         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
251
252         String expectedVersions =
253             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
254
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 );
259     }
260
261     public void populateDbForRetentionCountTest()
262         throws ArchivaDatabaseException
263     {
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" );
269
270         populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
271     }
272
273     private void populateDbForDaysOldTest()
274         throws ArchivaDatabaseException
275     {
276         List<String> versions = new ArrayList<String>();
277         versions.add( "2.2-SNAPSHOT" );
278
279         populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
280     }
281
282     public void populateDbForReleasedSnapshotsTest()
283         throws ArchivaDatabaseException
284     {
285         List<String> versions = new ArrayList<String>();
286         versions.add( "2.3-SNAPSHOT" );
287
288         populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );
289     }
290 }