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