1 package org.apache.continuum.buildagent.manager;
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.buildagent.configuration.BuildAgentConfigurationService;
24 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
25 import org.jmock.Expectations;
26 import org.jmock.Mockery;
27 import org.jmock.integration.junit3.JUnit3Mockery;
28 import org.jmock.lib.legacy.ClassImposteriser;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.text.SimpleDateFormat;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.List;
36
37
38
39
40 public class BuildAgentPurgeManagerTest
41 extends PlexusInSpringTestCase
42 {
43 private static final int DAYS_OLD = 2;
44
45 private static final int RELEASES_COUNT = 5;
46
47 private static final int RELEASES_DAYS_OLD_COUNT = 3;
48
49 private static final int WORKING_COUNT = 10;
50
51 private static final int WORKING_DAYS_OLD_COUNT = 9;
52
53 private static final String DIRECTORY_TYPE_RELEASES = "releases";
54
55 private static final String DIRECTORY_TYPE_WORKING = "working";
56
57 private Mockery context;
58
59 private BuildAgentConfigurationService buildAgentConfigurationService;
60
61 private DefaultBuildAgentPurgeManager purgeManager;
62
63 private File tempDir;
64
65 protected void setUp()
66 throws Exception
67 {
68 super.setUp();
69
70 context = new JUnit3Mockery();
71 context.setImposteriser( ClassImposteriser.INSTANCE );
72
73 purgeManager = (DefaultBuildAgentPurgeManager) lookup( BuildAgentPurgeManager.class );
74
75 buildAgentConfigurationService = context.mock( BuildAgentConfigurationService.class );
76
77 purgeManager.setBuildAgentConfigurationService( buildAgentConfigurationService );
78
79 createTestDirectoriesAndFiles();
80 }
81
82 protected void tearDown()
83 throws Exception
84 {
85 purgeManager = null;
86 cleanUpTestDirectoriesAndFiles();
87 super.tearDown();
88 }
89
90
91 public void testCleanAllPurge()
92 throws Exception
93 {
94 context.checking( new Expectations()
95 {
96 {
97 one( buildAgentConfigurationService ).getWorkingDirectory();
98 will( returnValue( tempDir ) );
99
100 one( buildAgentConfigurationService ).getWorkingDirectory();
101 will( returnValue( tempDir ) );
102 }
103 } );
104
105
106
107 assertEquals( RELEASES_COUNT + WORKING_COUNT + 2, tempDir.list().length );
108
109 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_WORKING, 1, 1, true );
110
111
112
113 assertEquals( RELEASES_COUNT + 2, tempDir.list().length );
114
115 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_RELEASES, 1, 1, true );
116
117
118
119 assertEquals( 2, tempDir.list().length );
120 }
121
122 public void testRetentionOnlyPurge()
123 throws Exception
124 {
125 context.checking( new Expectations()
126 {
127 {
128 one( buildAgentConfigurationService ).getWorkingDirectory();
129 will( returnValue( tempDir ) );
130
131 one( buildAgentConfigurationService ).getWorkingDirectory();
132 will( returnValue( tempDir ) );
133 }
134 } );
135
136
137
138 assertEquals( RELEASES_COUNT + WORKING_COUNT + 2, tempDir.list().length );
139
140 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_WORKING, 0, 2, false );
141
142 List<String> fileNames = Arrays.asList( tempDir.list() );
143
144 File[] files = tempDir.listFiles();
145
146
147
148 assertEquals( RELEASES_COUNT + 2 + 2, fileNames.size() );
149
150 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_RELEASES, 0, 4, false );
151
152 fileNames = Arrays.asList( tempDir.list() );
153
154
155
156 assertEquals( 4 + 2 + 2, fileNames.size() );
157 }
158
159 public void testDaysOldOnlyPurge()
160 throws Exception
161 {
162 context.checking( new Expectations()
163 {
164 {
165 one( buildAgentConfigurationService ).getWorkingDirectory();
166 will( returnValue( tempDir ) );
167
168 one( buildAgentConfigurationService ).getWorkingDirectory();
169 will( returnValue( tempDir ) );
170 }
171 } );
172
173
174
175 assertEquals( RELEASES_COUNT + WORKING_COUNT + 2, tempDir.list().length );
176
177 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_WORKING, 1, 0, false );
178
179 List<String> fileNames = Arrays.asList( tempDir.list() );
180
181
182
183 assertEquals( RELEASES_COUNT + ( WORKING_COUNT - WORKING_DAYS_OLD_COUNT ) + 2, fileNames.size() );
184
185 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_RELEASES, 1, 0, false );
186
187 fileNames = Arrays.asList( tempDir.list() );
188
189
190
191 assertEquals( ( RELEASES_COUNT - RELEASES_DAYS_OLD_COUNT ) + ( WORKING_COUNT - WORKING_DAYS_OLD_COUNT ) + 2,
192 fileNames.size() );
193 }
194
195 public void testRetentionAndDaysOldOnlyPurge()
196 throws Exception
197 {
198 context.checking( new Expectations()
199 {
200 {
201 one( buildAgentConfigurationService ).getWorkingDirectory();
202 will( returnValue( tempDir ) );
203
204 one( buildAgentConfigurationService ).getWorkingDirectory();
205 will( returnValue( tempDir ) );
206 }
207 } );
208
209
210
211 assertEquals( RELEASES_COUNT + WORKING_COUNT + 2, tempDir.list().length );
212
213 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_WORKING, 1, 5, false );
214
215 List<String> fileNames = Arrays.asList( tempDir.list() );
216
217
218
219 assertEquals( RELEASES_COUNT + Math.max( 5, WORKING_COUNT - WORKING_DAYS_OLD_COUNT ) + 2, fileNames.size() );
220
221 purgeManager.executeDirectoryPurge( DIRECTORY_TYPE_RELEASES, 1, 1, false );
222
223 fileNames = Arrays.asList( tempDir.list() );
224
225
226
227 assertEquals( Math.max( 1, RELEASES_COUNT - RELEASES_DAYS_OLD_COUNT ) + Math.max( 5, WORKING_COUNT -
228 WORKING_DAYS_OLD_COUNT ) + 2, fileNames.size() );
229 }
230
231 private void createTestDirectoriesAndFiles()
232 throws Exception
233 {
234 SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );
235 tempDir = new File( System.getProperty( "java.io.tmpdir" ) + System.getProperty( "file.separator" ) +
236 format.format( new Date() ) );
237 if ( !tempDir.mkdirs() )
238 {
239 throw new IOException( "Unable to create test directory: " + tempDir.getName() );
240 }
241
242 createReleasesDirectories( tempDir, RELEASES_COUNT, DAYS_OLD, RELEASES_DAYS_OLD_COUNT );
243 createWorkingDirectories( tempDir, WORKING_COUNT, DAYS_OLD, WORKING_DAYS_OLD_COUNT );
244 createRandomFile( tempDir, "random.txt" );
245 createRandomFile( tempDir, "releases-random.txt" );
246 }
247
248 private void createReleasesDirectories( File parentDir, int count, int daysOld, int daysOldCount )
249 throws Exception
250 {
251 int daysOldIndex = 0;
252 for ( int x = 1; x <= count; x++ )
253 {
254 File file = new File( tempDir.getAbsolutePath() + System.getProperty( "file.separator" ) + "releases-" +
255 x );
256 if ( !file.mkdirs() )
257 {
258 throw new IOException( "Unable to create test directory: " + file.getName() );
259 }
260 if ( daysOldIndex < daysOldCount )
261 {
262 long daysOldTime = System.currentTimeMillis() - 24 * 60 * 60 * 1000 * daysOld;
263 file.setLastModified( daysOldTime );
264 daysOldIndex++;
265 }
266
267 }
268 }
269
270 private void createWorkingDirectories( File parentDir, int count, int daysOld, int daysOldCount )
271 throws Exception
272 {
273 int daysOldIndex = 0;
274 for ( int x = 1; x <= count; x++ )
275 {
276 File file = new File( tempDir.getAbsolutePath() + System.getProperty( "file.separator" ) + x );
277 if ( !file.mkdirs() )
278 {
279 throw new IOException( "Unable to create test directory: " + file.getName() );
280 }
281 if ( daysOldIndex < daysOldCount )
282 {
283 long daysOldTime = System.currentTimeMillis() - 24 * 60 * 60 * 1000 * daysOld;
284 file.setLastModified( daysOldTime );
285 daysOldIndex++;
286 }
287
288 }
289 }
290
291 private File createRandomFile( File parentDir, String fileName )
292 throws IOException
293 {
294 File randomFile = new File( parentDir.getAbsolutePath() + System.getProperty( "file.separator" ) + fileName );
295 if ( !randomFile.exists() )
296 {
297 randomFile.createNewFile();
298 }
299 return randomFile;
300 }
301
302 private void cleanUpTestDirectoriesAndFiles()
303 throws IOException
304 {
305 FileUtils.deleteDirectory( tempDir );
306 }
307 }