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.archiva.repository.events.RepositoryListener;
23 import org.apache.commons.lang.time.DateUtils;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.junit.After;
26 import org.junit.Test;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.Collections;
32 import java.util.List;
36 public class DaysOldRepositoryPurgeTest
37 extends AbstractRepositoryPurgeTest
39 private static final int OLD_TIMESTAMP = 1179382029;
41 private void setLastModified( String dirPath, long lastModified )
43 File dir = new File( dirPath );
44 File[] contents = dir.listFiles();
45 for ( File content : contents )
47 content.setLastModified( lastModified );
52 public void testByLastModified()
55 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
56 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
57 repoConfiguration.getRetentionCount(), repositorySession,
58 Collections.singletonList( listener ) );
60 String repoRoot = prepareTestRepos();
62 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
64 setLastModified( projectRoot + "/2.2-SNAPSHOT/", OLD_TIMESTAMP );
66 // test listeners for the correct artifacts
67 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
68 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar" );
69 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
70 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom" );
71 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
72 "maven-install-plugin", "2.2-20061118.060401-2",
73 "maven-install-plugin-2.2-20061118.060401-2.jar" );
74 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
75 "maven-install-plugin", "2.2-20061118.060401-2",
76 "maven-install-plugin-2.2-20061118.060401-2.pom" );
77 listenerControl.replay();
79 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
81 listenerControl.verify();
83 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
84 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
85 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
86 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
87 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
88 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
90 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
91 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
92 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
93 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
94 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
95 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
96 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
98 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
99 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
100 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
101 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
102 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
103 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
105 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
106 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
107 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
108 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
109 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
110 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
114 public void testOrderOfDeletion()
117 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
118 List<RepositoryListener> listeners = Collections.singletonList( listener );
119 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
120 repoConfiguration.getRetentionCount(), repositorySession, listeners );
122 String repoRoot = prepareTestRepos();
124 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
126 setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", OLD_TIMESTAMP );
128 // test listeners for the correct artifacts
129 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
130 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
131 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
132 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
133 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
134 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
135 listenerControl.replay();
137 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
139 listenerControl.verify();
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" );
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" );
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" );
165 public void testMetadataDrivenSnapshots()
168 ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
169 List<RepositoryListener> listeners = Collections.singletonList( listener );
170 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
171 repoConfiguration.getRetentionCount(), repositorySession, listeners );
173 String repoRoot = prepareTestRepos();
175 String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
177 Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
178 setLastModified( versionRoot, currentDate.getTimeInMillis() );
180 String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
182 for ( int i = 5; i <= 7; i++ )
184 File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
185 jarFile.createNewFile();
186 File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
187 pomFile.createNewFile();
189 // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
192 jarFile.setLastModified( OLD_TIMESTAMP );
193 pomFile.setLastModified( OLD_TIMESTAMP );
197 // test listeners for the correct artifacts
198 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
199 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" );
200 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
201 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" );
202 listenerControl.replay();
204 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
206 listenerControl.verify();
208 // this should be deleted since the filename version (timestamp) is older than
209 // 100 days even if the last modified date was <100 days ago
210 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
211 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
212 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
213 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
215 // this should not be deleted because last modified date is <100 days ago
216 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
217 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
219 for ( int i = 5; i <= 7; i++ )
221 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
222 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
227 public void tearDown()