]> source.dussan.org Git - archiva.git/blob
419b9418e18b6d4bde25cabac809bc797eefe982
[archiva.git] /
1 package org.apache.archiva.scheduler.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.util.Calendar;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.List;
27
28 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.model.ArtifactReference;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
36 import org.codehaus.plexus.util.FileUtils;
37
38 /**
39  * ArchivaRepositoryScanningTaskExecutorTest
40  *
41  * @version $Id$
42  */
43 public class ArchivaRepositoryScanningTaskExecutorTest
44     extends PlexusInSpringTestCase
45 {
46     private TaskExecutor taskExecutor;
47
48     private File repoDir;
49
50     private static final String TEST_REPO_ID = "testRepo";
51
52     private RepositoryStatisticsManager repositoryStatisticsManager;
53
54     private TestConsumer testConsumer;
55
56     protected void setUp()
57         throws Exception
58     {
59         super.setUp();
60
61         taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
62
63         File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
64         repoDir = new File( getBasedir(), "target/default-repository" );
65
66         FileUtils.deleteDirectory( repoDir );
67         assertFalse( "Default Test Repository should not exist.", repoDir.exists() );
68
69         repoDir.mkdir();
70
71         FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
72         // set the timestamps to a time well in the past
73         Calendar cal = Calendar.getInstance();
74         cal.add( Calendar.YEAR, -1 );
75         for ( File f : (List<File>) FileUtils.getFiles( repoDir, "**", null ) )
76         {
77             f.setLastModified( cal.getTimeInMillis() );
78         }
79         // TODO: test they are excluded instead
80         for ( String dir : (List<String>) FileUtils.getDirectoryNames( repoDir, "**/.svn", null, false ) )
81         {
82             FileUtils.deleteDirectory( new File( repoDir, dir ) );
83         }
84
85         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
86
87         ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
88         assertNotNull( archivaConfig );
89
90         // Create it
91         ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
92         repositoryConfiguration.setId( TEST_REPO_ID );
93         repositoryConfiguration.setName( "Test Repository" );
94         repositoryConfiguration.setLocation( repoDir.getAbsolutePath() );
95         archivaConfig.getConfiguration().getManagedRepositories().clear();
96         archivaConfig.getConfiguration().addManagedRepository( repositoryConfiguration );
97
98         repositoryStatisticsManager = (RepositoryStatisticsManager) lookup( RepositoryStatisticsManager.class );
99         testConsumer = (TestConsumer) lookup( KnownRepositoryContentConsumer.class, "test-consumer" );
100     }
101
102     protected void tearDown()
103         throws Exception
104     {
105         FileUtils.deleteDirectory( repoDir );
106
107         assertFalse( repoDir.exists() );
108
109         super.tearDown();
110     }
111
112     public void testExecutor()
113         throws Exception
114     {
115         RepositoryTask repoTask = new RepositoryTask();
116
117         repoTask.setRepositoryId( TEST_REPO_ID );
118
119         taskExecutor.executeTask( repoTask );
120
121         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
122
123         assertNotNull( unprocessedResultList );
124         assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
125     }
126
127     public void testExecutorScanOnlyNewArtifacts()
128         throws Exception
129     {
130         RepositoryTask repoTask = new RepositoryTask();
131
132         repoTask.setRepositoryId( TEST_REPO_ID );
133         repoTask.setScanAll( false );
134
135         createAndSaveTestStats();
136
137         taskExecutor.executeTask( repoTask );
138
139         // check no artifacts processed
140         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
141
142         assertNotNull( unprocessedResultList );
143         assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
144                       unprocessedResultList.size() );
145
146         // check correctness of new stats
147         RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
148         assertEquals( 0, newStats.getNewFileCount() );
149         assertEquals( 31, newStats.getTotalFileCount() );
150         // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
151 //        assertEquals( 8, newStats.getTotalArtifactCount() );
152 //        assertEquals( 3, newStats.getTotalGroupCount() );
153 //        assertEquals( 5, newStats.getTotalProjectCount() );
154 //        assertEquals( 14159, newStats.getTotalArtifactFileSize() );
155
156         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
157
158         FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
159                                           newArtifactGroup );
160
161         // update last modified date
162         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
163             Calendar.getInstance().getTimeInMillis() + 1000 );
164         new File( newArtifactGroup,
165                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
166             Calendar.getInstance().getTimeInMillis() + 1000 );
167
168         assertTrue( newArtifactGroup.exists() );
169
170         taskExecutor.executeTask( repoTask );
171
172         unprocessedResultList = testConsumer.getConsumed();
173         assertNotNull( unprocessedResultList );
174         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
175                       unprocessedResultList.size() );
176
177         // check correctness of new stats
178         RepositoryStatistics updatedStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
179         assertEquals( 2, updatedStats.getNewFileCount() );
180         assertEquals( 33, updatedStats.getTotalFileCount() );
181         // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
182 //        assertEquals( 8, newStats.getTotalArtifactCount() );
183 //        assertEquals( 3, newStats.getTotalGroupCount() );
184 //        assertEquals( 5, newStats.getTotalProjectCount() );
185 //        assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
186     }
187
188     public void testExecutorScanOnlyNewArtifactsChangeTimes()
189         throws Exception
190     {
191         RepositoryTask repoTask = new RepositoryTask();
192
193         repoTask.setRepositoryId( TEST_REPO_ID );
194         repoTask.setScanAll( false );
195
196         createAndSaveTestStats();
197
198         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
199
200         FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
201                                           newArtifactGroup );
202
203         // update last modified date, placing shortly after last scan
204         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
205             Calendar.getInstance().getTimeInMillis() + 1000 );
206         new File( newArtifactGroup,
207                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
208             Calendar.getInstance().getTimeInMillis() + 1000 );
209
210         assertTrue( newArtifactGroup.exists() );
211
212         // scan using the really long previous duration
213         taskExecutor.executeTask( repoTask );
214
215         // check no artifacts processed
216         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
217         assertNotNull( unprocessedResultList );
218         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
219                       unprocessedResultList.size() );
220
221         // check correctness of new stats
222         RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
223         assertEquals( 2, newStats.getNewFileCount() );
224         assertEquals( 33, newStats.getTotalFileCount() );
225         // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
226 //        assertEquals( 8, newStats.getTotalArtifactCount() );
227 //        assertEquals( 3, newStats.getTotalGroupCount() );
228 //        assertEquals( 5, newStats.getTotalProjectCount() );
229 //        assertEquals( 19301, newStats.getTotalArtifactFileSize() );
230     }
231
232     public void testExecutorScanOnlyNewArtifactsMidScan()
233         throws Exception
234     {
235         RepositoryTask repoTask = new RepositoryTask();
236
237         repoTask.setRepositoryId( TEST_REPO_ID );
238         repoTask.setScanAll( false );
239
240         createAndSaveTestStats();
241
242         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
243
244         FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
245                                           newArtifactGroup );
246
247         // update last modified date, placing in middle of last scan
248         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
249             Calendar.getInstance().getTimeInMillis() - 50000 );
250         new File( newArtifactGroup,
251                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
252             Calendar.getInstance().getTimeInMillis() - 50000 );
253
254         assertTrue( newArtifactGroup.exists() );
255
256         // scan using the really long previous duration
257         taskExecutor.executeTask( repoTask );
258
259         // check no artifacts processed
260         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
261         assertNotNull( unprocessedResultList );
262         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
263                       unprocessedResultList.size() );
264
265         // check correctness of new stats
266         RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
267         assertEquals( 2, newStats.getNewFileCount() );
268         assertEquals( 33, newStats.getTotalFileCount() );
269         // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
270 //        assertEquals( 8, newStats.getTotalArtifactCount() );
271 //        assertEquals( 3, newStats.getTotalGroupCount() );
272 //        assertEquals( 5, newStats.getTotalProjectCount() );
273 //        assertEquals( 19301, newStats.getTotalArtifactFileSize() );
274     }
275
276     public void testExecutorForceScanAll()
277         throws Exception
278     {
279         RepositoryTask repoTask = new RepositoryTask();
280
281         repoTask.setRepositoryId( TEST_REPO_ID );
282         repoTask.setScanAll( true );
283
284         Date date = Calendar.getInstance().getTime();
285         RepositoryStatistics stats = new RepositoryStatistics();
286         stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
287         stats.setScanEndTime( date );
288         stats.setNewFileCount( 8 );
289         stats.setTotalArtifactCount( 8 );
290         stats.setTotalFileCount( 8 );
291         stats.setTotalGroupCount( 3 );
292         stats.setTotalProjectCount( 5 );
293         stats.setTotalArtifactFileSize( 999999 );
294
295         repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
296
297         taskExecutor.executeTask( repoTask );
298
299         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
300
301         assertNotNull( unprocessedResultList );
302         assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
303     }
304
305     private void createAndSaveTestStats()
306     {
307         Date date = Calendar.getInstance().getTime();
308         RepositoryStatistics stats = new RepositoryStatistics();
309         stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
310         stats.setScanEndTime( date );
311         stats.setNewFileCount( 31 );
312         stats.setTotalArtifactCount( 8 );
313         stats.setTotalFileCount( 31 );
314         stats.setTotalGroupCount( 3 );
315         stats.setTotalProjectCount( 5 );
316         stats.setTotalArtifactFileSize( 38545 );
317
318         repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
319     }
320 }