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