]> source.dussan.org Git - archiva.git/blob
3f0ec595ca131af20eae0ee938e6bf19e5129e10
[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.archiva.repository.events.RepositoryListener;
23 import org.apache.commons.lang.time.DateUtils;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.junit.After;
26 import org.junit.Test;
27
28 import java.io.File;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.Collections;
32 import java.util.List;
33
34 /**
35  */
36 public class DaysOldRepositoryPurgeTest
37     extends AbstractRepositoryPurgeTest
38 {
39     private static final int OLD_TIMESTAMP = 1179382029;
40
41     private void setLastModified( String dirPath, long lastModified )
42     {
43         File dir = new File( dirPath );
44         File[] contents = dir.listFiles();
45         for ( File content : contents )
46         {
47             content.setLastModified( lastModified );
48         }
49     }
50
51     @Test
52     public void testByLastModified()
53         throws Exception
54     {
55         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
56         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
57                                                 repoConfiguration.getRetentionCount(), repositorySession,
58                                                 Collections.singletonList( listener ) );
59
60         String repoRoot = prepareTestRepos();
61
62         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
63
64         setLastModified( projectRoot + "/2.2-SNAPSHOT/", OLD_TIMESTAMP );
65
66         // test listeners for the correct artifacts
67         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
68                                  "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar" );
69         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
70                                  "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom" );
71         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
72                                  "maven-install-plugin", "2.2-20061118.060401-2",
73                                  "maven-install-plugin-2.2-20061118.060401-2.jar" );
74         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
75                                  "maven-install-plugin", "2.2-20061118.060401-2",
76                                  "maven-install-plugin-2.2-20061118.060401-2.pom" );
77         listenerControl.replay();
78
79         repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
80
81         listenerControl.verify();
82
83         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
84         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
85         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
86         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
87         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
88         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
89
90         // shouldn't be deleted because even if older than 30 days (because retention count = 2)
91         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
92         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
93         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
94         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
95         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
96         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
97
98         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
99         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
100         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
101         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
102         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
103         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
104
105         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
106         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
107         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
108         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
109         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
110         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
111     }
112
113     @Test
114     public void testOrderOfDeletion()
115         throws Exception
116     {
117         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
118         List<RepositoryListener> listeners = Collections.singletonList( listener );
119         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
120                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
121
122         String repoRoot = prepareTestRepos();
123
124         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
125
126         setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", OLD_TIMESTAMP );
127
128         // test listeners for the correct artifacts
129         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
130                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
131                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
132         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
133                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
134                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
135         listenerControl.replay();
136
137         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
138
139         listenerControl.verify();
140
141         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
142         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
143         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
144         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
145         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
146         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
147
148         // the following should not have been deleted
149         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
150         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
151         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
152         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
153         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
154         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
155
156         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
157         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
158         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
159         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
160         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
161         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
162     }
163
164     @Test
165     public void testMetadataDrivenSnapshots()
166         throws Exception
167     {
168         ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
169         List<RepositoryListener> listeners = Collections.singletonList( listener );
170         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
171                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
172
173         String repoRoot = prepareTestRepos();
174
175         String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
176
177         Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
178         setLastModified( versionRoot, currentDate.getTimeInMillis() );
179
180         String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
181
182         for ( int i = 5; i <= 7; i++ )
183         {
184             File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
185             jarFile.createNewFile();
186             File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
187             pomFile.createNewFile();
188
189             // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
190             if ( i == 5 )
191             {
192                 jarFile.setLastModified( OLD_TIMESTAMP );
193                 pomFile.setLastModified( OLD_TIMESTAMP );
194             }
195         }
196
197         // test listeners for the correct artifacts
198         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
199                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" );
200         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
201                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" );
202         listenerControl.replay();
203
204         repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
205
206         listenerControl.verify();
207
208         // this should be deleted since the filename version (timestamp) is older than
209         // 100 days even if the last modified date was <100 days ago
210         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
211         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
212         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
213         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
214
215         // this should not be deleted because last modified date is <100 days ago
216         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
217         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
218
219         for ( int i = 5; i <= 7; i++ )
220         {
221             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
222             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
223         }
224     }
225
226     @After
227     public void tearDown()
228         throws Exception
229     {
230         super.tearDown();
231         repoPurge = null;
232     }
233 }