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
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Collections;
27 import org.apache.commons.lang.time.DateUtils;
31 public class DaysOldRepositoryPurgeTest
32 extends AbstractRepositoryPurgeTest
34 private static final String[] extensions =
35 new String[] { "-5.jar", "-5.pom", "-6.jar", "-6.pom", "-7.jar", "-7.pom" };
49 private void setLastModified( String dirPath, long lastModified )
51 File dir = new File( dirPath );
52 File[] contents = dir.listFiles();
53 for ( File content : contents )
55 content.setLastModified( lastModified );
59 public void testByLastModified()
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 ) );
68 String repoRoot = prepareTestRepos();
70 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
72 setLastModified( projectRoot + "/2.2-SNAPSHOT/", 1179382029 );
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();
81 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
83 listenerControl.verify();
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" );
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" );
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" );
108 public void testOrderOfDeletion()
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 ) );
116 String repoRoot = prepareTestRepos();
118 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
120 setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", 1179382029 );
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();
129 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
131 listenerControl.verify();
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" );
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" );
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" );
156 public void testMetadataDrivenSnapshots()
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 ) );
165 String repoRoot = prepareTestRepos();
167 String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
169 Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
170 setLastModified( versionRoot, currentDate.getTimeInMillis() );
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 ) );
179 if ( mon.length() == 1 )
184 if ( day.length() == 1 )
189 if ( hr.length() == 1 )
194 if ( min.length() == 1 )
199 if ( sec.length() == 1 )
204 createFiles( versionRoot );
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();
213 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
215 listenerControl.verify();
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" );
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" );
228 for ( String extension : extensions )
230 assertExists( versionRoot + "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec + extension );
234 private void createFiles( String versionRoot )
237 for ( String extension : extensions )
240 new File( versionRoot, "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec + extension );
241 file.createNewFile();
245 protected void tearDown()