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