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