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