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