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