]> source.dussan.org Git - archiva.git/blob
0c08258842c328427b21428a2f3c98d4da772310
[archiva.git] /
1 package org.apache.maven.archiva.scheduled.executors;
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.net.URL;
24 import java.util.Calendar;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import javax.jdo.PersistenceManager;
29 import javax.jdo.PersistenceManagerFactory;
30
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.database.ArchivaDAO;
34 import org.apache.maven.archiva.database.ArtifactDAO;
35 import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint;
36 import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
37 import org.apache.maven.archiva.model.ArchivaArtifact;
38 import org.apache.maven.archiva.model.RepositoryContentStatistics;
39 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
40 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
41 import org.codehaus.plexus.jdo.JdoFactory;
42 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
44 import org.codehaus.plexus.util.FileUtils;
45 import org.jpox.SchemaTool;
46
47 /**
48  * ArchivaRepositoryScanningTaskExecutorTest
49  *
50  * @version $Id$
51  */
52 public class ArchivaRepositoryScanningTaskExecutorTest
53     extends PlexusInSpringTestCase
54 {
55     private TaskExecutor taskExecutor;
56
57     protected ArchivaDAO dao;
58
59     private File repoDir;
60
61     private static final String TEST_REPO_ID = "testRepo";
62
63     protected void setUp()
64         throws Exception
65     {
66         super.setUp();
67
68         DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
69         assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
70
71         jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
72
73         /* derby version
74        File derbyDbDir = new File( "target/plexus-home/testdb" );
75        if ( derbyDbDir.exists() )
76        {
77            FileUtils.deleteDirectory( derbyDbDir );
78        }
79
80        jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.apache.derby.jdbc.EmbeddedDriver" ) );
81        jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:derby:" + derbyDbDir.getAbsolutePath() + ";create=true" ) );
82         */
83
84         jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
85         jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
86
87         jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
88
89         jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
90
91         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
92
93         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
94
95         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
96
97         jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
98
99         jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
100
101         // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
102
103         jdoFactory.setProperty( "org.jpox.validateTables", "true" );
104
105         jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
106
107         jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
108
109         Properties properties = jdoFactory.getProperties();
110
111         for ( Map.Entry<Object, Object> entry : properties.entrySet() )
112         {
113             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
114         }
115
116         URL jdoFileUrls[] = new URL[]{getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" )};
117
118         if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
119         {
120             fail( "Unable to process test " + getName() + " - missing package.jdo." );
121         }
122
123         File propsFile = null; // intentional
124         boolean verbose = true;
125
126         SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose );
127         SchemaTool.createSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose, null );
128
129         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
130
131         assertNotNull( pmf );
132
133         PersistenceManager pm = pmf.getPersistenceManager();
134
135         pm.close();
136
137         this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
138
139         taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
140
141         File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
142         repoDir = new File( getBasedir(), "target/default-repository" );
143
144         repoDir.mkdir();
145
146         FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
147
148         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
149
150         ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
151         assertNotNull( archivaConfig );
152
153         // Create it
154         ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
155         repositoryConfiguration.setId( TEST_REPO_ID );
156         repositoryConfiguration.setName( "Test Repository" );
157         repositoryConfiguration.setLocation( repoDir.getAbsolutePath() );
158         archivaConfig.getConfiguration().getManagedRepositories().clear();
159         archivaConfig.getConfiguration().addManagedRepository( repositoryConfiguration );
160     }
161
162     protected void tearDown()
163         throws Exception
164     {
165         FileUtils.deleteDirectory( repoDir );
166
167         assertFalse( repoDir.exists() );
168
169         super.tearDown();
170     }
171
172     public void testExecutor()
173         throws Exception
174     {
175         RepositoryTask repoTask = new RepositoryTask();
176
177         repoTask.setRepositoryId( TEST_REPO_ID );
178
179         taskExecutor.executeTask( repoTask );
180
181         ArtifactDAO adao = dao.getArtifactDAO();
182         List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
183
184         assertNotNull( unprocessedResultList );
185         assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
186     }
187
188     public void testExecutorScanOnlyNewArtifacts()
189         throws Exception
190     {
191         RepositoryTask repoTask = new RepositoryTask();
192
193         repoTask.setRepositoryId( TEST_REPO_ID );
194         repoTask.setScanAll( false );
195
196         RepositoryContentStatistics stats = new RepositoryContentStatistics();
197         stats.setDuration( 1234567 );
198         stats.setNewFileCount( 31 );
199         stats.setRepositoryId( TEST_REPO_ID );
200         stats.setTotalArtifactCount( 8 );
201         stats.setTotalFileCount( 31 );
202         stats.setTotalGroupCount( 3 );
203         stats.setTotalProjectCount( 5 );
204         stats.setTotalSize( 38545 );
205         stats.setWhenGathered( Calendar.getInstance().getTime() );
206
207         dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( stats );
208
209         taskExecutor.executeTask( repoTask );
210
211         // check no artifacts processed
212         ArtifactDAO adao = dao.getArtifactDAO();
213         List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
214
215         assertNotNull( unprocessedResultList );
216         assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
217                       unprocessedResultList.size() );
218
219         // check correctness of new stats
220         List<RepositoryContentStatistics> results =
221             (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( TEST_REPO_ID ) );
222         RepositoryContentStatistics newStats = results.get( 0 );
223         assertEquals( 0, newStats.getNewFileCount() );
224         assertEquals( TEST_REPO_ID, newStats.getRepositoryId() );
225         assertEquals( 31, newStats.getTotalFileCount() );
226         // TODO: can't test these as they weren't stored in the database
227 //        assertEquals( 8, newStats.getTotalArtifactCount() );
228 //        assertEquals( 3, newStats.getTotalGroupCount() );
229 //        assertEquals( 5, newStats.getTotalProjectCount() );
230         assertEquals( 38545, newStats.getTotalSize() );
231
232         File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
233
234         FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
235                                           newArtifactGroup );
236
237         // update last modified date
238         new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
239             Calendar.getInstance().getTimeInMillis() + 1000 );
240         new File( newArtifactGroup,
241                   "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
242             Calendar.getInstance().getTimeInMillis() + 1000 );
243
244         assertTrue( newArtifactGroup.exists() );
245
246         taskExecutor.executeTask( repoTask );
247
248         unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
249         assertNotNull( unprocessedResultList );
250         assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
251                       unprocessedResultList.size() );
252
253         // check correctness of new stats
254         results =
255             (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( TEST_REPO_ID ) );
256         RepositoryContentStatistics updatedStats = results.get( 0 );
257         assertEquals( 2, updatedStats.getNewFileCount() );
258         assertEquals( TEST_REPO_ID, updatedStats.getRepositoryId() );
259         assertEquals( 33, updatedStats.getTotalFileCount() );
260         // TODO: can't test these as they weren't stored in the database
261 //        assertEquals( 8, newStats.getTotalArtifactCount() );
262 //        assertEquals( 3, newStats.getTotalGroupCount() );
263 //        assertEquals( 5, newStats.getTotalProjectCount() );
264         assertEquals( 43687, updatedStats.getTotalSize() );
265     }
266
267     public void testExecutorForceScanAll()
268         throws Exception
269     {
270         RepositoryTask repoTask = new RepositoryTask();
271
272         repoTask.setRepositoryId( TEST_REPO_ID );
273         repoTask.setScanAll( true );
274
275         RepositoryContentStatistics stats = new RepositoryContentStatistics();
276         stats.setDuration( 1234567 );
277         stats.setNewFileCount( 8 );
278         stats.setRepositoryId( TEST_REPO_ID );
279         stats.setTotalArtifactCount( 8 );
280         stats.setTotalFileCount( 8 );
281         stats.setTotalGroupCount( 3 );
282         stats.setTotalProjectCount( 5 );
283         stats.setTotalSize( 999999 );
284         stats.setWhenGathered( Calendar.getInstance().getTime() );
285
286         dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( stats );
287
288         taskExecutor.executeTask( repoTask );
289
290         ArtifactDAO adao = dao.getArtifactDAO();
291         List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
292
293         assertNotNull( unprocessedResultList );
294         assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
295     }
296 }