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