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