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
22 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
23 import org.apache.maven.archiva.database.ArchivaDAO;
24 import org.apache.maven.archiva.database.ArtifactDAO;
25 import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
28 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
29 import org.codehaus.plexus.jdo.JdoFactory;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
32 import org.jpox.SchemaTool;
36 import java.util.Date;
37 import java.util.Iterator;
38 import java.util.List;
40 import java.util.Properties;
42 import javax.jdo.PersistenceManager;
43 import javax.jdo.PersistenceManagerFactory;
46 * ArchivaDatabaseUpdateTaskExecutorTest
50 public class ArchivaDatabaseUpdateTaskExecutorTest
51 extends PlexusInSpringTestCase
53 private TaskExecutor taskExecutor;
55 protected ArchivaDAO dao;
57 protected void setUp()
62 DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
63 assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
65 jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
68 File derbyDbDir = new File( "target/plexus-home/testdb" );
69 if ( derbyDbDir.exists() )
71 FileUtils.deleteDirectory( derbyDbDir );
74 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.apache.derby.jdbc.EmbeddedDriver" ) );
75 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:derby:" + derbyDbDir.getAbsolutePath() + ";create=true" ) );
78 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
79 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
81 jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
83 jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
85 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
87 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
89 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
91 jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
93 jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
95 // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
97 jdoFactory.setProperty( "org.jpox.validateTables", "true" );
99 jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
101 jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
103 Properties properties = jdoFactory.getProperties();
105 for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
107 Map.Entry entry = (Map.Entry) it.next();
109 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
112 URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
114 if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
116 fail( "Unable to process test " + getName() + " - missing package.jdo." );
119 File propsFile = null; // intentional
120 boolean verbose = true;
122 SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
123 SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
125 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
127 assertNotNull( pmf );
129 PersistenceManager pm = pmf.getPersistenceManager();
133 this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
135 taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-database-update" );
138 public void testExecutor()
141 File repoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
143 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
145 ManagedRepositoryConfiguration repo = createRepository( "testRepo", "Test Repository", repoDir );
146 assertNotNull( repo );
148 ArtifactDAO adao = dao.getArtifactDAO();
150 ArchivaArtifact sqlArtifact = adao.createArtifact( "javax.sql", "jdbc", "2.0", "", "jar", repo.getId() );
151 sqlArtifact.getModel().setLastModified( new Date() );
152 sqlArtifact.getModel().setSize( 1234 );
153 sqlArtifact.getModel().setOrigin( "testcase" );
154 sqlArtifact.getModel().setWhenProcessed( null );
156 adao.saveArtifact( sqlArtifact );
158 ArchivaArtifact artifact = adao.getArtifact( "javax.sql", "jdbc", "2.0", null, "jar", repo.getId() );
160 assertNotNull( artifact );
162 // Test for artifact existance.
163 List artifactList = adao.queryArtifacts( null );
164 assertNotNull( "Artifact list should not be null.", artifactList );
165 assertEquals( "Artifact list size", 1, artifactList.size() );
167 // Test for unprocessed artifacts.
168 List unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
169 assertNotNull( "Unprocessed Results should not be null.", unprocessedResultList );
170 assertEquals( "Incorrect number of unprocessed artifacts detected.", 1, unprocessedResultList.size() );
172 // Execute the database task.
173 DatabaseTask dataTask = new DatabaseTask();
175 dataTask.setName( "testDataTask" );
177 taskExecutor.executeTask( dataTask );
179 // Test for artifact existance.
180 artifactList = adao.queryArtifacts( null );
181 assertNotNull( "Artifact list should not be null.", artifactList );
182 assertEquals( "Artifact list size", 1, artifactList.size() );
184 // Test for processed artifacts.
185 List processedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( true ) );
186 assertNotNull( "Processed Results should not be null.", processedResultList );
187 assertEquals( "Incorrect number of processed artifacts detected.", 1, processedResultList.size() );
190 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
192 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
194 repo.setName( name );
195 repo.setLocation( location.getAbsolutePath() );