]> source.dussan.org Git - archiva.git/blob
9653c2cc1fa627ec4bc94fa703c9b117885b5e1b
[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.common.utils.BaseFile;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.FileType;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub;
31 import org.apache.maven.archiva.database.ArchivaDatabaseException;
32 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
33 import org.custommonkey.xmlunit.XMLAssert;
34
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
41  */
42 public class RepositoryPurgeConsumerTest
43     extends AbstractRepositoryPurgeTest
44 {
45     public void testConsumption()
46         throws Exception
47     {
48         assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
49     }
50
51     public void testConsumptionOfOtherMetadata()
52         throws Exception
53     {
54         assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
55     }
56
57     private void assertNotConsumed( String path )
58         throws Exception
59     {
60         ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
61         FileType fileType =
62             (FileType) archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
63         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
64         fileType.addPattern( "**/*.xml" );
65
66         // trigger reload
67         FileTypes fileTypes = (FileTypes) lookup( FileTypes.class );
68         fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
69
70         KnownRepositoryContentConsumer repoPurgeConsumer =
71             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repository-purge" );
72
73         File repoLocation = getTestFile( "target/test-" + getName() + "/test-repo" );
74
75         File localFile =
76             new File( repoLocation, path );
77
78         ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
79         BaseFile baseFile = new BaseFile( repoLocation, localFile );
80         predicate.setBasefile( baseFile );
81
82         assertFalse( predicate.evaluate( repoPurgeConsumer ) );
83     }
84
85     private void setLastModified( String path )
86     {
87         File dir = new File( path );
88         File[] contents = dir.listFiles();
89         for ( int i = 0; i < contents.length; i++ )
90         {
91             contents[i].setLastModified( 1179382029 );
92         }
93     }
94
95     public void testConsumerByRetentionCount()
96         throws Exception
97     {
98         KnownRepositoryContentConsumer repoPurgeConsumer =
99             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
100                                                      "repo-purge-consumer-by-retention-count" );
101
102         LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
103         indexFactory.setExpectedRecordsSize( 2 );
104
105         ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
106
107         populateDbForRetentionCountTest();
108
109         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
110         repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute.
111         repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT );
112         addRepoToConfiguration( "retention-count", repoConfiguration );
113
114         repoPurgeConsumer.beginScan( repoConfiguration, null );
115
116         String repoRoot = prepareTestRepo();
117
118         repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
119
120         String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT";
121
122         // assert if removed from repo
123         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
124         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
125         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
126         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
127         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
128         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
129
130         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
131         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
132         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
133         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
134         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
135         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
136
137         // assert if not removed from repo
138         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
139         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
140         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
141         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
142         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
143         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
144
145         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
146         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
147         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
148         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
149         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
150         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
151     }
152
153     private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
154         throws Exception
155     {
156         ArchivaConfiguration archivaConfiguration =
157             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
158         Configuration configuration = archivaConfiguration.getConfiguration();
159         configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
160         configuration.addManagedRepository( repoConfiguration );
161     }
162
163     public void testConsumerByDaysOld()
164         throws Exception
165     {
166         populateDbForDaysOldTest();
167
168         KnownRepositoryContentConsumer repoPurgeConsumer =
169             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
170                                                      "repo-purge-consumer-by-days-old" );
171
172         LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub();
173         indexFactory.setExpectedRecordsSize( 2 );
174
175         ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory );
176
177         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
178         repoConfiguration.setDaysOlder( TEST_DAYS_OLDER );
179         addRepoToConfiguration( "days-old", repoConfiguration );
180
181         repoPurgeConsumer.beginScan( repoConfiguration, null );
182
183         String repoRoot = prepareTestRepo();
184         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
185
186         setLastModified( projectRoot + "/2.2-SNAPSHOT" );
187
188         repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
189
190         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
191         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
192         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
193         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
194         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
195         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
196
197         // shouldn't be deleted because even if older than 30 days (because retention count = 2)
198         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
199         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
200         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
201         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
202         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
203         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
204
205         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
206         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
207         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
208         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
209         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
210         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
211     }
212
213     /**
214      * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
215      *
216      * @throws Exception
217      */
218     public void testReleasedSnapshotsWereNotCleaned()
219         throws Exception
220     {
221         KnownRepositoryContentConsumer repoPurgeConsumer =
222             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
223                                                      "repo-purge-consumer-by-retention-count" );
224
225         populateDbForReleasedSnapshotsTest();
226
227         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
228         repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
229         addRepoToConfiguration( "retention-count", repoConfiguration );
230
231         repoPurgeConsumer.beginScan( repoConfiguration, null );
232
233         String repoRoot = prepareTestRepo();
234
235         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
236
237         // check if the snapshot wasn't removed
238         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
239
240         assertExists( projectRoot + "/2.3-SNAPSHOT" );
241         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
242         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
243         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
244         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
245         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
246         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
247
248         // check if metadata file wasn't updated
249         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
250
251         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
252
253         String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
254
255         XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//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 testReleasedSnapshotsWereCleaned()
262         throws Exception
263     {
264         KnownRepositoryContentConsumer repoPurgeConsumer =
265             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
266                                                      "repo-purge-consumer-by-days-old" );
267
268         populateDbForReleasedSnapshotsTest();
269
270         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration();
271         repoConfiguration.setDeleteReleasedSnapshots( true );
272         addRepoToConfiguration( "days-old", repoConfiguration );
273
274         repoPurgeConsumer.beginScan( repoConfiguration, null );
275
276         String repoRoot = prepareTestRepo();
277
278         repoPurgeConsumer.processFile( PATH_TO_RELEASED_SNAPSHOT );
279
280         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
281
282         // check if the snapshot was removed
283         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
284         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
285         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
286         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
287         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
288         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
289         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
290
291         // check if metadata file was updated
292         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
293
294         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
295
296         String expectedVersions =
297             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
298
299         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
300         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
301                                      "//metadata/versioning/versions/version", metadataXml );
302         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
303     }
304
305     public void populateDbForRetentionCountTest()
306         throws ArchivaDatabaseException
307     {
308         List<String> versions = new ArrayList<String>();
309         versions.add( "1.0RC1-20070504.153317-1" );
310         versions.add( "1.0RC1-20070504.160758-2" );
311         versions.add( "1.0RC1-20070505.090015-3" );
312         versions.add( "1.0RC1-20070506.090132-4" );
313
314         populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions );
315     }
316
317     private void populateDbForDaysOldTest()
318         throws ArchivaDatabaseException
319     {
320         List<String> versions = new ArrayList<String>();
321         versions.add( "2.2-SNAPSHOT" );
322
323         populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );
324     }
325
326     public void populateDbForReleasedSnapshotsTest()
327         throws ArchivaDatabaseException
328     {
329         List<String> versions = new ArrayList<String>();
330         versions.add( "2.3-SNAPSHOT" );
331
332         populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );
333     }
334 }