]> source.dussan.org Git - archiva.git/blob
88b7f69f5376bc7ef77cffcfa7e31c0729aa72d9
[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 org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.ArtifactDAO;
26 import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint;
27 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
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;
33
34 import java.io.File;
35 import java.net.URL;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Properties;
40
41 import javax.jdo.PersistenceManager;
42 import javax.jdo.PersistenceManagerFactory;
43
44 /**
45  * ArchivaRepositoryScanningTaskExecutorTest 
46  *
47  * @version $Id$
48  */
49 public class ArchivaRepositoryScanningTaskExecutorTest
50     extends PlexusInSpringTestCase
51 {
52     private TaskExecutor taskExecutor;
53
54     protected ArchivaDAO dao;
55     
56     protected void setUp()
57         throws Exception
58     {
59         super.setUp();
60         
61         DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
62         assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
63
64         jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" ); 
65
66         /* derby version
67         File derbyDbDir = new File( "target/plexus-home/testdb" );
68         if ( derbyDbDir.exists() )
69         {
70             FileUtils.deleteDirectory( derbyDbDir );
71         }
72
73         jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.apache.derby.jdbc.EmbeddedDriver" ) );   
74         jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:derby:" + derbyDbDir.getAbsolutePath() + ";create=true" ) );
75          */   
76
77         jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );   
78         jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
79         
80         jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) ); 
81
82         jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) ); 
83
84         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );  
85
86         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );  
87
88         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );  
89
90         jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
91
92         jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
93
94         // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
95
96         jdoFactory.setProperty( "org.jpox.validateTables", "true" );
97
98         jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
99
100         jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
101
102         Properties properties = jdoFactory.getProperties();
103
104         for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
105         {
106             Map.Entry entry = (Map.Entry) it.next();
107
108             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
109         }
110
111         URL jdoFileUrls[] = new URL[] { getClass()
112             .getResource( "/org/apache/maven/archiva/model/package.jdo" ) }; 
113
114         if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
115         {
116             fail( "Unable to process test " + getName() + " - missing package.jdo." );
117         }
118
119         File propsFile = null; // intentional
120         boolean verbose = true;
121
122         SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
123         SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
124
125         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
126
127         assertNotNull( pmf );
128
129         PersistenceManager pm = pmf.getPersistenceManager();
130
131         pm.close();
132
133         this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
134
135         taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
136     }
137
138     public void testExecutor() throws Exception
139     {
140         File repoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
141
142         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
143
144         ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
145         assertNotNull( archivaConfig );
146         
147         // Create it
148         ManagedRepositoryConfiguration repo = createRepository( "testRepo", "Test Repository", repoDir );
149         assertNotNull( repo );
150         archivaConfig.getConfiguration().getManagedRepositories().clear();
151         archivaConfig.getConfiguration().addManagedRepository( repo );
152
153         RepositoryTask repoTask = new RepositoryTask();
154         
155         repoTask.setName( "testRepoTask" );
156         repoTask.setRepositoryId( "testRepo" );
157         
158         taskExecutor.executeTask( repoTask );
159
160         ArtifactDAO adao = dao.getArtifactDAO();
161         List unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
162         
163         assertNotNull( unprocessedResultList );
164         assertEquals("Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
165     }
166     
167     protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
168     {
169         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
170         repo.setId( id );
171         repo.setName( name );
172         repo.setLocation( location.getAbsolutePath() );
173         return repo;
174     }
175 }