View Javadoc

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.common.utils.BaseFile;
24  import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25  import org.apache.maven.archiva.configuration.Configuration;
26  import org.apache.maven.archiva.configuration.FileType;
27  import org.apache.maven.archiva.configuration.FileTypes;
28  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29  import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30  import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
31  import org.custommonkey.xmlunit.XMLAssert;
32  
33  import java.io.File;
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-20070510.010101-4.jar" );
187         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
188         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
189         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
190         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
191         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
192 
193         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
194         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
195         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
196         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
197         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
198         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
199     }
200 
201     /**
202      * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
203      *
204      * @throws Exception
205      */
206     public void testReleasedSnapshotsWereNotCleaned()
207         throws Exception
208     {
209         KnownRepositoryContentConsumer repoPurgeConsumer =
210             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
211                                                      "repo-purge-consumer-by-retention-count" );
212 
213         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
214         repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
215         addRepoToConfiguration( "retention-count", repoConfiguration );
216 
217         repoPurgeConsumer.beginScan( repoConfiguration, null );
218 
219         String repoRoot = prepareTestRepos();
220 
221         repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
222 
223         // check if the snapshot wasn't removed
224         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
225 
226         assertExists( projectRoot + "/2.3-SNAPSHOT" );
227         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
228         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
229         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
230         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
231         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
232         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
233 
234         // check if metadata file wasn't updated
235         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
236 
237         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
238 
239         String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
240 
241         XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
242         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
243                                      "//metadata/versioning/versions/version", metadataXml );
244         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
245     }
246 
247     public void testReleasedSnapshotsWereCleaned()
248         throws Exception
249     {
250         KnownRepositoryContentConsumer repoPurgeConsumer =
251             (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class,
252                                                      "repo-purge-consumer-by-days-old" );
253 
254         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
255         repoConfiguration.setDeleteReleasedSnapshots( true );
256         addRepoToConfiguration( "days-old", repoConfiguration );
257 
258         repoPurgeConsumer.beginScan( repoConfiguration, null );
259 
260         String repoRoot = prepareTestRepos();
261 
262         repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
263 
264         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
265 
266         // check if the snapshot was removed
267         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
268         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
269         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
270         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
271         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
272         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
273         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
274 
275         // check if metadata file was updated
276         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
277 
278         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
279 
280         String expectedVersions =
281             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
282 
283         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
284         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
285                                      "//metadata/versioning/versions/version", metadataXml );
286         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
287     }
288 }