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