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;
23 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub;
24 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Calendar;
30 import java.util.HashMap;
31 import java.util.List;
35 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
37 public class DaysOldRepositoryPurgeTest
38 extends AbstractRepositoryPurgeTest
41 private Map<String, RepositoryContentIndex> map;
43 private static final String[] extensions =
44 new String[] { "-5.jar", "-5.pom", "-6.jar", "-6.pom", "-7.jar", "-7.pom" };
58 protected void setUp()
64 private void setLastModified( String dirPath, long lastModified )
66 File dir = new File( dirPath );
67 File[] contents = dir.listFiles();
68 for ( int i = 0; i < contents.length; i++ )
70 contents[i].setLastModified( lastModified );
74 public void testByLastModified()
77 map = new HashMap<String, RepositoryContentIndex>();
78 map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) );
79 map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) );
80 map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) );
83 new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
84 getRepoConfiguration().getRetentionCount(), map );
86 String repoRoot = prepareTestRepo();
88 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
90 setLastModified( projectRoot + "/2.2-SNAPSHOT/", 1179382029 );
92 populateDbForTestByLastModified();
94 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
96 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
97 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
98 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
99 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
100 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
101 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
103 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
104 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
105 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
106 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
107 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
108 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
109 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
111 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
112 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
113 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
114 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
115 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
116 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
119 public void testOrderOfDeletion()
122 map = new HashMap<String, RepositoryContentIndex>();
123 map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) );
124 map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) );
125 map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) );
128 new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
129 getRepoConfiguration().getRetentionCount(), map );
131 String repoRoot = prepareTestRepo();
133 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
135 setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", 1179382029 );
137 populateDbForTestOrderOfDeletion();
139 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
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" );
164 public void testMetadataDrivenSnapshots()
167 map = new HashMap<String, RepositoryContentIndex>();
168 map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) );
169 map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) );
170 map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) );
173 new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
174 getRepoConfiguration().getRetentionCount(), map );
176 String repoRoot = prepareTestRepo();
178 String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
180 Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
181 setLastModified( versionRoot, currentDate.getTimeInMillis() );
183 year = String.valueOf( currentDate.get( Calendar.YEAR ) );
184 mon = String.valueOf( currentDate.get( Calendar.MONTH ) + 1 );
185 day = String.valueOf( currentDate.get( Calendar.DATE ) );
186 hr = String.valueOf( currentDate.get( Calendar.HOUR ) );
187 min = String.valueOf( currentDate.get( Calendar.MINUTE ) );
188 sec = String.valueOf( currentDate.get( Calendar.SECOND ) );
190 if ( mon.length() == 1 )
195 if ( day.length() == 1 )
200 if ( hr.length() == 1 )
205 if ( min.length() == 1 )
210 if ( sec.length() == 1 )
215 createFiles( versionRoot );
217 List<String> versions = new ArrayList<String>();
218 versions.add( "1.4.3-20070113.163208-4" );
219 versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-5" );
220 versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-6" );
221 versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-7" );
222 versions.add( "1.4.3-SNAPSHOT" );
224 populateDb( "org.codehaus.plexus", "plexus-utils", versions );
226 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
228 // this should be deleted since the filename version (timestamp) is older than
229 // 100 days even if the last modified date was <100 days ago
230 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
231 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
232 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
233 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
235 // this should not be deleted because last modified date is <100 days ago
236 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
237 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
239 for ( int i = 0; i < extensions.length; i++ )
241 assertExists( versionRoot + "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec +
246 private void createFiles( String versionRoot )
249 for ( int i = 0; i < extensions.length; i++ )
252 new File( versionRoot, "/plexus-utils-1.4.3-" + year + mon + day + "." + hr + min + sec + extensions[i] );
253 file.createNewFile();
257 protected void tearDown()
264 private void populateDbForTestByLastModified()
267 List<String> versions = new ArrayList<String>();
268 versions.add( "2.2-20061118.060401-2" );
269 versions.add( "2.2-20070513.034619-5" );
270 versions.add( "2.2-SNAPSHOT" );
272 populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions );