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