]> source.dussan.org Git - archiva.git/blob
6252979801b2fbf0e4d5c58ec5f86ce9b13fec32
[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.model.ArtifactReference;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
29 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30
31 import java.io.File;
32 import java.util.Calendar;
33 import java.util.Collection;
34 import java.util.Date;
35
36 /**
37  * ArchivaRepositoryScanningTaskExecutorPhase2Test
38  *
39  * @version $Id: ArchivaRepositoryScanningTaskExecutorTest.java 1214303 2011-12-14 15:37:51Z olamy $
40  */
41 @RunWith( SpringJUnit4ClassRunner.class )
42 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
43 public class ArchivaRepositoryScanningTaskExecutorPhase2Test
44     extends ArchivaRepositoryScanningTaskExecutorAbstractTest
45 {
46
47     @Test
48     public void testExecutorScanOnlyNewArtifacts()
49         throws Exception
50     {
51         RepositoryTask repoTask = new RepositoryTask();
52
53         repoTask.setRepositoryId( TEST_REPO_ID );
54         repoTask.setScanAll( false );
55
56         createAndSaveTestStats();
57
58         taskExecutor.executeTask( repoTask );
59
60         // check no artifacts processed
61         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
62
63         assertNotNull( unprocessedResultList );
64         assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
65                       unprocessedResultList.size() );
66
67         // check correctness of new stats
68         RepositoryStatistics newStats =
69             repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
70         assertEquals( 0, newStats.getNewFileCount() );
71         assertEquals( 31, newStats.getTotalFileCount() );
72         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
73 //        assertEquals( 8, newStats.getTotalArtifactCount() );
74 //        assertEquals( 3, newStats.getTotalGroupCount() );
75 //        assertEquals( 5, newStats.getTotalProjectCount() );
76 //        assertEquals( 14159, newStats.getTotalArtifactFileSize() );
77
78         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
79         assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
80
81         FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
82                                           newArtifactGroup );
83
84         // update last modified date
85         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
86             Calendar.getInstance().getTimeInMillis() + 1000 );
87         new File( newArtifactGroup,
88                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
89             Calendar.getInstance().getTimeInMillis() + 1000 );
90
91         assertTrue( newArtifactGroup.exists() );
92
93         taskExecutor.executeTask( repoTask );
94
95         unprocessedResultList = testConsumer.getConsumed();
96         assertNotNull( unprocessedResultList );
97         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
98                       unprocessedResultList.size() );
99
100         // check correctness of new stats
101         RepositoryStatistics updatedStats =
102             repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
103         assertEquals( 2, updatedStats.getNewFileCount() );
104         assertEquals( 33, updatedStats.getTotalFileCount() );
105         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
106 //        assertEquals( 8, newStats.getTotalArtifactCount() );
107 //        assertEquals( 3, newStats.getTotalGroupCount() );
108 //        assertEquals( 5, newStats.getTotalProjectCount() );
109 //        assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
110     }
111
112     @Test
113     public void testExecutorScanOnlyNewArtifactsChangeTimes()
114         throws Exception
115     {
116         RepositoryTask repoTask = new RepositoryTask();
117
118         repoTask.setRepositoryId( TEST_REPO_ID );
119         repoTask.setScanAll( false );
120
121         createAndSaveTestStats();
122
123         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
124         assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
125
126         FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
127                                           newArtifactGroup );
128
129         // update last modified date, placing shortly after last scan
130         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
131             Calendar.getInstance().getTimeInMillis() + 1000 );
132         new File( newArtifactGroup,
133                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
134             Calendar.getInstance().getTimeInMillis() + 1000 );
135
136         assertTrue( newArtifactGroup.exists() );
137
138         // scan using the really long previous duration
139         taskExecutor.executeTask( repoTask );
140
141         // check no artifacts processed
142         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
143         assertNotNull( unprocessedResultList );
144         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
145                       unprocessedResultList.size() );
146
147         // check correctness of new stats
148         RepositoryStatistics newStats =
149             repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
150         assertEquals( 2, newStats.getNewFileCount() );
151         assertEquals( 33, newStats.getTotalFileCount() );
152         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
153 //        assertEquals( 8, newStats.getTotalArtifactCount() );
154 //        assertEquals( 3, newStats.getTotalGroupCount() );
155 //        assertEquals( 5, newStats.getTotalProjectCount() );
156 //        assertEquals( 19301, newStats.getTotalArtifactFileSize() );
157     }
158
159     @Test
160     public void testExecutorScanOnlyNewArtifactsMidScan()
161         throws Exception
162     {
163         RepositoryTask repoTask = new RepositoryTask();
164
165         repoTask.setRepositoryId( TEST_REPO_ID );
166         repoTask.setScanAll( false );
167
168         createAndSaveTestStats();
169
170         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
171         assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
172
173         FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
174                                           newArtifactGroup );
175
176         // update last modified date, placing in middle of last scan
177         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
178             Calendar.getInstance().getTimeInMillis() - 50000 );
179         new File( newArtifactGroup,
180                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
181             Calendar.getInstance().getTimeInMillis() - 50000 );
182
183         assertTrue( newArtifactGroup.exists() );
184
185         // scan using the really long previous duration
186         taskExecutor.executeTask( repoTask );
187
188         // check no artifacts processed
189         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
190         assertNotNull( unprocessedResultList );
191         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
192                       unprocessedResultList.size() );
193
194         // check correctness of new stats
195         RepositoryStatistics newStats =
196             repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
197         assertEquals( 2, newStats.getNewFileCount() );
198         assertEquals( 33, newStats.getTotalFileCount() );
199         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
200 //        assertEquals( 8, newStats.getTotalArtifactCount() );
201 //        assertEquals( 3, newStats.getTotalGroupCount() );
202 //        assertEquals( 5, newStats.getTotalProjectCount() );
203 //        assertEquals( 19301, newStats.getTotalArtifactFileSize() );
204     }
205
206     @Test
207     public void testExecutorForceScanAll()
208         throws Exception
209     {
210         RepositoryTask repoTask = new RepositoryTask();
211
212         repoTask.setRepositoryId( TEST_REPO_ID );
213         repoTask.setScanAll( true );
214
215         Date date = Calendar.getInstance().getTime();
216         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
217                                                             new Date( date.getTime() - 1234567 ), date, 8, 8 );
218
219         taskExecutor.executeTask( repoTask );
220
221         Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
222
223         assertNotNull( unprocessedResultList );
224         assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
225     }
226
227     private void createAndSaveTestStats()
228         throws MetadataRepositoryException
229     {
230         Date date = Calendar.getInstance().getTime();
231         RepositoryStatistics stats = new RepositoryStatistics();
232         stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
233         stats.setScanEndTime( date );
234         stats.setNewFileCount( 31 );
235         stats.setTotalArtifactCount( 8 );
236         stats.setTotalFileCount( 31 );
237         stats.setTotalGroupCount( 3 );
238         stats.setTotalProjectCount( 5 );
239         stats.setTotalArtifactFileSize( 38545 );
240
241         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
242                                                             new Date( date.getTime() - 1234567 ), date, 31, 31 );
243     }
244 }