View Javadoc
1   package org.apache.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 java.io.File;
23  import java.text.SimpleDateFormat;
24  import java.util.Calendar;
25  import java.util.Collections;
26  import java.util.List;
27  import org.apache.archiva.admin.model.beans.ManagedRepository;
28  import org.apache.archiva.repository.events.RepositoryListener;
29  import org.apache.commons.lang.time.DateUtils;
30  import org.junit.After;
31  import org.junit.Test;
32  
33  /**
34   */
35  public class DaysOldRepositoryPurgeTest
36      extends AbstractRepositoryPurgeTest
37  {
38      private static final int OLD_TIMESTAMP = 1179382029;
39  
40      private void setLastModified( String dirPath, long lastModified )
41      {
42          File dir = new File( dirPath );
43          File[] contents = dir.listFiles();
44          for ( File content : contents )
45          {
46              content.setLastModified( lastModified );
47          }
48      }
49  
50      @After
51      @Override
52      public void tearDown()
53          throws Exception
54      {
55          super.tearDown();
56          repoPurge = null;
57      }
58  
59      @Test
60      public void testByLastModified()
61          throws Exception
62      {
63          ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
64          repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
65                                                  repoConfiguration.getRetentionCount(), repositorySession,
66                                                  Collections.singletonList( listener ) );
67  
68          String repoRoot = prepareTestRepos();
69  
70          String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-install-plugin";
71  
72          setLastModified( projectRoot + "/2.2-SNAPSHOT/", OLD_TIMESTAMP );
73  
74          // test listeners for the correct artifacts
75          listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
76                                   "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar" );
77          listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
78                                   "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom" );
79          listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
80                                   "maven-install-plugin", "2.2-20061118.060401-2",
81                                   "maven-install-plugin-2.2-20061118.060401-2.jar" );
82          listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
83                                   "maven-install-plugin", "2.2-20061118.060401-2",
84                                   "maven-install-plugin-2.2-20061118.060401-2.pom" );
85          listenerControl.replay();
86  
87          repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
88  
89          listenerControl.verify();
90  
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" );
97  
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" );
105 
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" );
112 
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" );
119     }
120 
121     @Test
122     public void testOrderOfDeletion()
123         throws Exception
124     {
125         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
126         List<RepositoryListener> listeners = Collections.singletonList( listener );
127         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
128                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
129 
130         String repoRoot = prepareTestRepos();
131 
132         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin";
133 
134         setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", OLD_TIMESTAMP );
135 
136         // test listeners for the correct artifacts
137         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
138                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
139                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
140         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
141                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
142                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
143         listenerControl.replay();
144 
145         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
146 
147         listenerControl.verify();
148 
149         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
150         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
151         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
152         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
153         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
154         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
155 
156         // the following should not have been deleted
157         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
158         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
159         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
160         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
161         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
162         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
163 
164         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
165         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
166         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
167         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
168         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
169         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
170     }
171 
172     @Test
173     public void testMetadataDrivenSnapshots()
174         throws Exception
175     {
176         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
177         List<RepositoryListener> listeners = Collections.singletonList( listener );
178         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
179                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
180 
181         String repoRoot = prepareTestRepos();
182 
183         String versionRoot = repoRoot + "/org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT";
184 
185         Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
186         setLastModified( versionRoot, currentDate.getTimeInMillis() );
187 
188         String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
189 
190         for ( int i = 5; i <= 7; i++ )
191         {
192             File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
193             jarFile.createNewFile();
194             File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
195             pomFile.createNewFile();
196 
197             // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
198             if ( i == 5 )
199             {
200                 jarFile.setLastModified( OLD_TIMESTAMP );
201                 pomFile.setLastModified( OLD_TIMESTAMP );
202             }
203         }
204 
205         // test listeners for the correct artifacts
206         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
207                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" );
208         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
209                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" );
210         listenerControl.replay();
211 
212         repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
213 
214         listenerControl.verify();
215 
216         // this should be deleted since the filename version (timestamp) is older than
217         // 100 days even if the last modified date was <100 days ago
218         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
219         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
220         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
221         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
222 
223         // this should not be deleted because last modified date is <100 days ago
224         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
225         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
226 
227         for ( int i = 5; i <= 7; i++ )
228         {
229             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
230             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
231         }
232     }
233 
234 
235 }