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