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