1 package org.apache.continuum.purge.executor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.continuum.model.repository.DirectoryPurgeConfiguration;
24 import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
25 import org.apache.continuum.purge.AbstractPurgeTest;
26 import org.apache.continuum.purge.task.PurgeTask;
27 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36 public abstract class AbstractPurgeExecutorTest
37 extends AbstractPurgeTest
38 {
39 private static final String[] jar_extensions = new String[]{".jar", ".jar.md5", ".jar.sha1"};
40
41 private static final String[] pom_extensions = new String[]{".pom", ".pom.md5", ".pom.sha1"};
42
43 private static final String[] metadata_extensions = new String[]{".xml", ".xml.sha1", ".xml.md5"};
44
45 private static final String TEST_MAVEN_METADATA = "maven-metadata-central";
46
47 private RepositoryPurgeConfiguration repoConfig;
48
49 private DirectoryPurgeConfiguration dirConfig;
50
51 protected TaskExecutor purgeExecutor;
52
53 protected PurgeTask purgeDefaultRepoTask;
54
55 protected PurgeTask purgeReleasesDirTask;
56
57 protected PurgeTask purgeBuildOutputDirTask;
58
59 protected void setUp()
60 throws Exception
61 {
62 super.setUp();
63
64 if ( purgeExecutor == null )
65 {
66 purgeExecutor = (TaskExecutor) lookup( TaskExecutor.class.getName(), "purge" );
67 }
68 }
69
70 protected void tearDown()
71 throws Exception
72 {
73 super.tearDown();
74
75 FileUtils.deleteDirectory( getDefaultRepositoryLocation() );
76 FileUtils.deleteDirectory( getReleasesDirectoryLocation() );
77 FileUtils.deleteDirectory( getBuildOutputDirectoryLocation() );
78 }
79
80 protected PurgeTask getDaysOldRepoPurgeTask()
81 throws Exception
82 {
83 repoConfig = new RepositoryPurgeConfiguration();
84 repoConfig.setRepository( defaultRepository );
85 repoConfig.setDaysOlder( TEST_DAYS_OLDER );
86 repoConfig = repositoryPurgeConfigurationDao.addRepositoryPurgeConfiguration( repoConfig );
87
88 return new PurgeTask( repoConfig.getId() );
89 }
90
91 protected PurgeTask getRetentionCountRepoPurgeTask()
92 throws Exception
93 {
94 repoConfig = new RepositoryPurgeConfiguration();
95 repoConfig.setRepository( defaultRepository );
96 repoConfig.setDaysOlder( -1 );
97 repoConfig.setRetentionCount( TEST_RETENTION_COUNT );
98 repoConfig = repositoryPurgeConfigurationDao.addRepositoryPurgeConfiguration( repoConfig );
99
100 return new PurgeTask( repoConfig.getId() );
101 }
102
103 protected PurgeTask getReleasedSnapshotsRepoPurgeTask()
104 throws Exception
105 {
106 repoConfig = new RepositoryPurgeConfiguration();
107 repoConfig.setRepository( defaultRepository );
108 repoConfig.setDaysOlder( -1 );
109 repoConfig.setRetentionCount( TEST_RETENTION_COUNT );
110 repoConfig.setDeleteReleasedSnapshots( true );
111 repoConfig = repositoryPurgeConfigurationDao.addRepositoryPurgeConfiguration( repoConfig );
112
113 return new PurgeTask( repoConfig.getId() );
114 }
115
116 protected PurgeTask getDaysOldReleasesDirPurgeTask()
117 throws Exception
118 {
119 dirConfig = new DirectoryPurgeConfiguration();
120 dirConfig.setDirectoryType( TEST_RELEASES_DIRECTORY_TYPE );
121 dirConfig.setLocation( getReleasesDirectoryLocation().getAbsolutePath() );
122 dirConfig.setDaysOlder( TEST_DAYS_OLDER );
123 dirConfig = directoryPurgeConfigurationDao.addDirectoryPurgeConfiguration( dirConfig );
124
125 return new PurgeTask( dirConfig.getId() );
126 }
127
128 protected PurgeTask getDaysOldBuildOutputDirPurgeTask()
129 throws Exception
130 {
131 dirConfig = new DirectoryPurgeConfiguration();
132 dirConfig.setDirectoryType( TEST_BUILDOUTPUT_DIRECTORY_TYPE );
133 dirConfig.setLocation( getBuildOutputDirectoryLocation().getAbsolutePath() );
134 dirConfig.setDaysOlder( TEST_DAYS_OLDER );
135 dirConfig = directoryPurgeConfigurationDao.addDirectoryPurgeConfiguration( dirConfig );
136
137 return new PurgeTask( dirConfig.getId() );
138 }
139
140 protected PurgeTask getRetentionCountReleasesDirPurgeTask()
141 throws Exception
142 {
143 dirConfig = new DirectoryPurgeConfiguration();
144 dirConfig.setDirectoryType( TEST_RELEASES_DIRECTORY_TYPE );
145 dirConfig.setLocation( getReleasesDirectoryLocation().getAbsolutePath() );
146 dirConfig.setDaysOlder( -1 );
147 dirConfig.setRetentionCount( TEST_RETENTION_COUNT );
148 dirConfig = directoryPurgeConfigurationDao.addDirectoryPurgeConfiguration( dirConfig );
149
150 return new PurgeTask( dirConfig.getId() );
151 }
152
153 protected PurgeTask getRetentionCountBuildOutputDirPurgeTask()
154 throws Exception
155 {
156 dirConfig = new DirectoryPurgeConfiguration();
157 dirConfig.setDirectoryType( TEST_BUILDOUTPUT_DIRECTORY_TYPE );
158 dirConfig.setLocation( getBuildOutputDirectoryLocation().getAbsolutePath() );
159 dirConfig.setDaysOlder( -1 );
160 dirConfig.setRetentionCount( TEST_RETENTION_COUNT );
161 dirConfig = directoryPurgeConfigurationDao.addDirectoryPurgeConfiguration( dirConfig );
162
163 return new PurgeTask( dirConfig.getId() );
164 }
165
166 protected PurgeTask getCleanAllDefaultRepoPurgeTask()
167 throws Exception
168 {
169 return new PurgeTask( defaultRepoPurge.getId() );
170 }
171
172 protected PurgeTask getCleanAllReleasesDirPurgeTask()
173 throws Exception
174 {
175 return new PurgeTask( defaultReleasesDirPurge.getId() );
176 }
177
178 protected PurgeTask getCleanAllBuildOutputDirPurgeTask()
179 throws Exception
180 {
181 return new PurgeTask( defaultBuildOutputDirPurge.getId() );
182 }
183
184 protected void setLastModified( String dirPath, long lastModified, boolean recurse )
185 {
186 File dir = new File( dirPath );
187
188 if ( recurse )
189 {
190 for ( File content : dir.listFiles() )
191 {
192 content.setLastModified( lastModified );
193
194 if ( content.list() != null && content.list().length > 0 )
195 {
196 setLastModified( content.getAbsolutePath(), lastModified, true );
197 }
198 }
199 }
200 else
201 {
202 dir.setLastModified( lastModified );
203 }
204 }
205
206 protected void assertIsEmpty( File dir )
207 {
208 File[] files = dir.listFiles();
209
210 assertEquals( "Directory should be clean: " + dir.getName(), 0, files.length );
211 }
212
213 protected void assertDeleted( String path )
214 {
215 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
216 }
217
218 protected void assertExists( String path )
219 {
220 assertTrue( "File should exist: " + path, new File( path ).exists() );
221 }
222
223 protected void assertMetadataDeleted( String projectRoot )
224 {
225 assertDeleted( projectRoot + "/" + TEST_MAVEN_METADATA + ".xml" );
226 assertDeleted( projectRoot + "/" + TEST_MAVEN_METADATA + ".xml.sha1" );
227 assertDeleted( projectRoot + "/" + TEST_MAVEN_METADATA + ".xml.md5" );
228 }
229
230 protected void populateDefaultRepositoryForRetentionCount()
231 throws Exception
232 {
233 prepareTestFolders();
234
235 List<String> versions = new ArrayList<String>();
236 versions.add( "1.0RC1-20070504.153317-1" );
237 versions.add( "1.0RC1-20070504.160758-2" );
238 versions.add( "1.0RC1-20070505.090015-3" );
239 versions.add( "1.0RC1-20070506.090132-4" );
240
241 createDefaultRepoFiles( "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT", "jruby-rake-plugin", versions );
242
243 versions = new ArrayList<String>();
244 versions.add( "1.1.2-20070427.065136-1" );
245 versions.add( "1.1.2-20070615.105019-3" );
246 versions.add( "1.1.2-20070506.163513-2" );
247
248 createDefaultRepoFiles( "/org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT", "castor-anttasks", versions );
249 }
250
251 protected void populateDefaultRepository()
252 throws Exception
253 {
254 prepareTestFolders();
255
256 List<String> versions = new ArrayList<String>();
257 versions.add( "1.1.2-20070427.065136-1" );
258 versions.add( "1.1.2-20070506.163513-2" );
259 versions.add( "1.1.2-20070615.105019-3" );
260
261 createDefaultRepoFiles( "/org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT",
262 "maven-assembly-plugin", versions );
263
264 versions = new ArrayList<String>();
265 versions.add( "2.2-20061118.060401-2" );
266 versions.add( "2.2-20070513.034619-5" );
267 versions.add( "2.2-SNAPSHOT" );
268
269 createDefaultRepoFiles( "/org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT", "maven-install-plugin",
270 versions );
271 }
272
273 protected void populateDefaultRepositoryForReleasedSnapshots()
274 throws Exception
275 {
276 populateDefaultRepository();
277
278 List<String> versions = new ArrayList<String>();
279 versions.add( "2.2" );
280
281 createDefaultRepoFiles( "/org/apache/maven/plugins/maven-plugin-plugin/2.2", "maven-plugin-plugin", versions );
282
283 versions = new ArrayList<String>();
284 versions.add( "2.3" );
285 createDefaultRepoFiles( "/org/apache/maven/plugins/maven-plugin-plugin/2.3", "maven-plugin-plugin", versions );
286
287 versions = new ArrayList<String>();
288 versions.add( "2.3-SNAPSHOT" );
289 createDefaultRepoFiles( "/org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT", "maven-plugin-plugin",
290 versions );
291 }
292
293 protected void populateReleasesDirectory()
294 throws Exception
295 {
296 prepareTestFolders();
297
298 String repoPath = getReleasesDirectoryLocation().getAbsolutePath();
299
300 String[] folders = new String[]{"1", "releases-4234729018", "", "releases-1234567809", "releases-1234567890"};
301
302 for ( String folder : folders )
303 {
304 File dir = new File( repoPath, folder );
305 dir.mkdir();
306 }
307 }
308
309 protected void populateBuildOutputDirectory()
310 throws Exception
311 {
312 prepareTestFolders();
313
314 String repoPath = getBuildOutputDirectoryLocation().getAbsolutePath();
315
316 File projectDir1 = new File( repoPath, "1" );
317 projectDir1.mkdir();
318
319 File projectDir2 = new File( repoPath, "2" );
320 projectDir2.mkdir();
321
322 String[] buildOutputs1 = new String[]{"1", "3", "6"};
323 String[] buildOutputs2 = new String[]{"4", "7", "9"};
324
325 for ( int i = 0; i < 3; i++ )
326 {
327 File outputDir1 = new File( projectDir1.getAbsolutePath(), buildOutputs1[i] );
328 outputDir1.mkdir();
329
330 File outputFile1 = new File( projectDir1.getAbsolutePath(), buildOutputs1[i] + ".log.txt" );
331 outputFile1.createNewFile();
332
333 File outputDir2 = new File( projectDir2.getAbsolutePath(), buildOutputs2[i] );
334 outputDir2.mkdir();
335
336 File outputFile2 = new File( projectDir2.getAbsolutePath(), buildOutputs2[i] + ".log.txt" );
337 outputFile2.createNewFile();
338 }
339 }
340
341 private void createDefaultRepoFiles( String versionPath, String artifactId, List<String> versions )
342 throws Exception
343 {
344 String repoPath = getDefaultRepositoryLocation().getAbsolutePath();
345
346 File versionDir = new File( repoPath + versionPath );
347 if ( !versionDir.exists() )
348 {
349 versionDir.mkdirs();
350
351
352 for ( String metadata_extension : metadata_extensions )
353 {
354 File metadata = new File( versionDir.getParentFile().getAbsolutePath(),
355 TEST_MAVEN_METADATA + metadata_extension );
356 metadata.createNewFile();
357 }
358 }
359
360 for ( String version : versions )
361 {
362 for ( String jar_extension : jar_extensions )
363 {
364 File file = new File( versionDir.getAbsolutePath(), artifactId + "-" + version + jar_extension );
365 file.createNewFile();
366 }
367
368 for ( String pom_extension : pom_extensions )
369 {
370 File file = new File( versionDir.getAbsolutePath(), artifactId + "-" + version + pom_extension );
371 file.createNewFile();
372 }
373 }
374 }
375
376 private void prepareTestFolders()
377 throws Exception
378 {
379 FileUtils.cleanDirectory( getDefaultRepositoryLocation() );
380 FileUtils.cleanDirectory( getReleasesDirectoryLocation() );
381 FileUtils.cleanDirectory( getBuildOutputDirectoryLocation() );
382 }
383 }