1 package org.apache.maven.archiva.scheduled.executors;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
24 import java.util.Calendar;
25 import java.util.List;
27 import java.util.Properties;
28 import javax.jdo.PersistenceManager;
29 import javax.jdo.PersistenceManagerFactory;
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;
48 * ArchivaRepositoryScanningTaskExecutorTest
52 public class ArchivaRepositoryScanningTaskExecutorTest
53 extends PlexusInSpringTestCase
55 private TaskExecutor taskExecutor;
57 protected ArchivaDAO dao;
61 private static final String TEST_REPO_ID = "testRepo";
63 protected void setUp()
68 DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
69 assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
71 jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
74 File derbyDbDir = new File( "target/plexus-home/testdb" );
75 if ( derbyDbDir.exists() )
77 FileUtils.deleteDirectory( derbyDbDir );
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" ) );
84 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
85 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
87 jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
89 jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
91 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
93 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
95 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
97 jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
99 jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
101 // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
103 jdoFactory.setProperty( "org.jpox.validateTables", "true" );
105 jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
107 jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
109 Properties properties = jdoFactory.getProperties();
111 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
113 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
116 URL jdoFileUrls[] = new URL[]{getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" )};
118 if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
120 fail( "Unable to process test " + getName() + " - missing package.jdo." );
123 File propsFile = null; // intentional
124 boolean verbose = true;
126 SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose );
127 SchemaTool.createSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose, null );
129 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
131 assertNotNull( pmf );
133 PersistenceManager pm = pmf.getPersistenceManager();
137 this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
139 taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
141 File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
142 repoDir = new File( getBasedir(), "target/default-repository" );
146 FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
148 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
150 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
151 assertNotNull( archivaConfig );
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 );
162 protected void tearDown()
165 FileUtils.deleteDirectory( repoDir );
167 assertFalse( repoDir.exists() );
172 public void testExecutor()
175 RepositoryTask repoTask = new RepositoryTask();
177 repoTask.setRepositoryId( TEST_REPO_ID );
179 taskExecutor.executeTask( repoTask );
181 ArtifactDAO adao = dao.getArtifactDAO();
182 List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
184 assertNotNull( unprocessedResultList );
185 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
188 public void testExecutorScanOnlyNewArtifacts()
191 RepositoryTask repoTask = new RepositoryTask();
193 repoTask.setRepositoryId( TEST_REPO_ID );
194 repoTask.setScanAll( false );
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() );
207 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( stats );
209 taskExecutor.executeTask( repoTask );
211 // check no artifacts processed
212 ArtifactDAO adao = dao.getArtifactDAO();
213 List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
215 assertNotNull( unprocessedResultList );
216 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
217 unprocessedResultList.size() );
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() );
232 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
234 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
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 );
244 assertTrue( newArtifactGroup.exists() );
246 taskExecutor.executeTask( repoTask );
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() );
253 // check correctness of new stats
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() );
267 public void testExecutorForceScanAll()
270 RepositoryTask repoTask = new RepositoryTask();
272 repoTask.setRepositoryId( TEST_REPO_ID );
273 repoTask.setScanAll( true );
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() );
286 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( stats );
288 taskExecutor.executeTask( repoTask );
290 ArtifactDAO adao = dao.getArtifactDAO();
291 List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
293 assertNotNull( unprocessedResultList );
294 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );