View Javadoc

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.Date;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import javax.jdo.PersistenceManager;
30  import javax.jdo.PersistenceManagerFactory;
31  
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.model.ArchivaArtifact;
37  import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
38  import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
39  import org.codehaus.plexus.jdo.JdoFactory;
40  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41  import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
42  import org.jpox.SchemaTool;
43  
44  /**
45   * ArchivaDatabaseUpdateTaskExecutorTest
46   *
47   * @version $Id:$
48   */
49  public class ArchivaDatabaseUpdateTaskExecutorTest
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 ( Map.Entry<Object, Object> entry : properties.entrySet() )
105         {
106             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
107         }
108 
109         URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
110 
111         if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
112         {
113             fail( "Unable to process test " + getName() + " - missing package.jdo." );
114         }
115 
116         File propsFile = null; // intentional
117         boolean verbose = true;
118 
119         SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
120         SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
121 
122         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
123 
124         assertNotNull( pmf );
125 
126         PersistenceManager pm = pmf.getPersistenceManager();
127 
128         pm.close();
129 
130         this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
131 
132         taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-database-update" );
133     }
134 
135     public void testExecutor()
136         throws Exception
137     {
138         File repoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
139 
140         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
141 
142         ManagedRepositoryConfiguration repo = createRepository( "testRepo", "Test Repository", repoDir );
143         assertNotNull( repo );
144 
145         ArtifactDAO adao = dao.getArtifactDAO();
146 
147         ArchivaArtifact sqlArtifact = adao.createArtifact( "javax.sql", "jdbc", "2.0", "", "jar", repo.getId() );
148         sqlArtifact.getModel().setLastModified( new Date() );
149         sqlArtifact.getModel().setSize( 1234 );
150         sqlArtifact.getModel().setOrigin( "testcase" );
151         sqlArtifact.getModel().setWhenProcessed( null );
152 
153         adao.saveArtifact( sqlArtifact );
154 
155         ArchivaArtifact artifact = adao.getArtifact( "javax.sql", "jdbc", "2.0", null, "jar", repo.getId() );
156 
157         assertNotNull( artifact );
158 
159         // Test for artifact existance.
160         List<ArchivaArtifact> artifactList = adao.queryArtifacts( null );
161         assertNotNull( "Artifact list should not be null.", artifactList );
162         assertEquals( "Artifact list size", 1, artifactList.size() );
163         
164         // Test for unprocessed artifacts.
165         List<ArchivaArtifact> unprocessedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( false ) );
166         assertNotNull( "Unprocessed Results should not be null.", unprocessedResultList );
167         assertEquals( "Incorrect number of unprocessed artifacts detected.", 1, unprocessedResultList.size() );
168 
169         // Execute the database task.
170         DatabaseTask dataTask = new DatabaseTask();
171         taskExecutor.executeTask( dataTask );
172         
173         // Test for artifact existance.
174         artifactList = adao.queryArtifacts( null );
175         assertNotNull( "Artifact list should not be null.", artifactList );
176         assertEquals( "Artifact list size", 1, artifactList.size() );
177         
178         // Test for processed artifacts.
179         List<ArchivaArtifact> processedResultList = adao.queryArtifacts( new ArtifactsProcessedConstraint( true ) );
180         assertNotNull( "Processed Results should not be null.", processedResultList );
181         assertEquals( "Incorrect number of processed artifacts detected.", 1, processedResultList.size() );
182     }
183     
184     protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
185     {
186         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
187         repo.setId( id );
188         repo.setName( name );
189         repo.setLocation( location.getAbsolutePath() );
190         return repo;
191     }
192 }