]> source.dussan.org Git - archiva.git/blob
d498011f387656154d90607fcfac5eb66b434035
[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.lang.time.DateUtils;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Collections;
29 import java.util.List;
30
31 /**
32  */
33 public class DaysOldRepositoryPurgeTest
34     extends AbstractRepositoryPurgeTest
35 {
36     private static final String[] extensions =
37         new String[] { "-5.jar", "-5.pom", "-6.jar", "-6.pom", "-7.jar", "-7.pom" };
38
39     private String year;
40
41     private String mon;
42
43     private String day;
44
45     private String hr;
46
47     private String min;
48
49     private String sec;
50
51     private void setLastModified( String dirPath, long lastModified )
52     {
53         File dir = new File( dirPath );
54         File[] contents = dir.listFiles();
55         for ( int i = 0; i < contents.length; i++ )
56         {
57             contents[i].setLastModified( lastModified );
58         }
59     }
60
61     public void testByLastModified()
62         throws Exception
63     {
64         repoPurge =
65             new DaysOldRepositoryPurge( getRepository(),
66                                         getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(),
67                                         getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(),
68                                         Collections.singletonList( listener ) );
69
70         String repoRoot = prepareTestRepos();
71
72         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
73
74         setLastModified( projectRoot + "/2.2-SNAPSHOT/", 1179382029 );
75
76         // test listeners for the correct artifacts
77         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin",
78                                                                   "2.2-SNAPSHOT", "maven-plugin" ) );
79         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin",
80                                                                   "2.2-SNAPSHOT", "pom" ) );
81         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin",
82                                                                   "2.2-20061118.060401-2", "maven-plugin" ) );
83         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin",
84                                                                   "2.2-20061118.060401-2", "pom" ) );
85         listenerControl.replay();
86         
87         repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
88         
89         listenerControl.verify();
90
91         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
92         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
93         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
94         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
95         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
96         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
97
98         // shouldn't be deleted because even if older than 30 days (because retention count = 2)
99         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
100         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
101         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
102         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
103         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
104         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
105
106         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
107         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
108         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
109         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
110         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
111         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
112
113         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
114         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
115         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
116         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
117         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
118         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
119     }
120
121     public void testOrderOfDeletion()
122         throws Exception
123     {
124         repoPurge =
125             new DaysOldRepositoryPurge( getRepository(), getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(),
126                                         getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), 
127                                         Collections.singletonList( listener ) );
128
129         String repoRoot = prepareTestRepos();
130
131         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
132
133         setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", 1179382029 );
134
135         // test listeners for the correct artifacts
136         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin",
137                                                                   "1.1.2-20070427.065136-1", "maven-plugin" ) );
138         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin",
139                                                                   "1.1.2-20070427.065136-1", "pom" ) );
140         listenerControl.replay();
141         
142         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
143
144         listenerControl.verify();
145
146         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
147         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
148         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
149         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
150         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
151         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
152
153         // the following should not have been deleted
154         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
155         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
156         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
157         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
158         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
159         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
160
161         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
162         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
163         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
164         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
165         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
166         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
167     }
168
169     public void testMetadataDrivenSnapshots()
170         throws Exception
171     {
172         repoPurge =
173             new DaysOldRepositoryPurge( getRepository(),
174                                         getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(),
175                                         getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(),
176                                         Collections.singletonList( listener ) );
177
178         String repoRoot = prepareTestRepos();
179
180         String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
181
182         Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
183         setLastModified( versionRoot, currentDate.getTimeInMillis() );
184
185         year = String.valueOf( currentDate.get( Calendar.YEAR ) );
186         mon = String.valueOf( currentDate.get( Calendar.MONTH ) + 1 );
187         day = String.valueOf( currentDate.get( Calendar.DATE ) );
188         hr = String.valueOf( currentDate.get( Calendar.HOUR ) );
189         min = String.valueOf( currentDate.get( Calendar.MINUTE ) );
190         sec = String.valueOf( currentDate.get( Calendar.SECOND ) );
191
192         if ( mon.length() == 1 )
193         {
194             mon = "0" + mon;
195         }
196
197         if ( day.length() == 1 )
198         {
199             day = "0" + day;
200         }
201
202         if ( hr.length() == 1 )
203         {
204             hr = "0" + hr;
205         }
206
207         if ( min.length() == 1 )
208         {
209             min = "0" + min;
210         }
211
212         if ( sec.length() == 1 )
213         {
214             sec = "0" + sec;
215         }
216
217         createFiles( versionRoot );
218
219         List<String> versions = new ArrayList<String>();
220         versions.add( "1.4.3-20070113.163208-4" );
221         versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-5" );
222         versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-6" );
223         versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-7" );
224         versions.add( "1.4.3-SNAPSHOT" );
225
226         // test listeners for the correct artifacts
227         listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.plexus", "plexus-utils",
228                                                                   "1.4.3-20070113.163208-4", "jar" ) );
229         listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.plexus", "plexus-utils",
230                                                                   "1.4.3-20070113.163208-4", "pom" ) );
231         listenerControl.replay();
232         
233         repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
234
235         listenerControl.verify();
236
237         // this should be deleted since the filename version (timestamp) is older than
238         // 100 days even if the last modified date was <100 days ago
239         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
240         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
241         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
242         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
243
244         // this should not be deleted because last modified date is <100 days ago
245         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
246         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
247
248         for ( int i = 0; i < extensions.length; i++ )
249         {
250             assertExists( versionRoot + "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec +
251                 extensions[i] );
252         }
253     }
254
255     private void createFiles( String versionRoot )
256         throws IOException
257     {
258         for ( int i = 0; i < extensions.length; i++ )
259         {
260             File file =
261                 new File( versionRoot, "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec + extensions[i] );
262             file.createNewFile();
263         }
264     }
265
266     protected void tearDown()
267         throws Exception
268     {
269         super.tearDown();
270         repoPurge = null;
271     }
272 }