1 package org.apache.maven.archiva.consumers.core.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.time.DateUtils;
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;
33 public class DaysOldRepositoryPurgeTest
34 extends AbstractRepositoryPurgeTest
36 private static final String[] extensions =
37 new String[] { "-5.jar", "-5.pom", "-6.jar", "-6.pom", "-7.jar", "-7.pom" };
51 private void setLastModified( String dirPath, long lastModified )
53 File dir = new File( dirPath );
54 File[] contents = dir.listFiles();
55 for ( int i = 0; i < contents.length; i++ )
57 contents[i].setLastModified( lastModified );
61 public void testByLastModified()
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 ) );
70 String repoRoot = prepareTestRepos();
72 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
74 setLastModified( projectRoot + "/2.2-SNAPSHOT/", 1179382029 );
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();
87 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
89 listenerControl.verify();
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" );
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" );
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" );
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" );
121 public void testOrderOfDeletion()
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 ) );
129 String repoRoot = prepareTestRepos();
131 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
133 setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", 1179382029 );
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();
142 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
144 listenerControl.verify();
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" );
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" );
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" );
169 public void testMetadataDrivenSnapshots()
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 ) );
178 String repoRoot = prepareTestRepos();
180 String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
182 Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
183 setLastModified( versionRoot, currentDate.getTimeInMillis() );
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 ) );
192 if ( mon.length() == 1 )
197 if ( day.length() == 1 )
202 if ( hr.length() == 1 )
207 if ( min.length() == 1 )
212 if ( sec.length() == 1 )
217 createFiles( versionRoot );
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" );
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();
233 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
235 listenerControl.verify();
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" );
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" );
248 for ( int i = 0; i < extensions.length; i++ )
250 assertExists( versionRoot + "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec +
255 private void createFiles( String versionRoot )
258 for ( int i = 0; i < extensions.length; i++ )
261 new File( versionRoot, "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec + extensions[i] );
262 file.createNewFile();
266 protected void tearDown()