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