1 package org.apache.maven.archiva.database;
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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.consumers.DatabaseCleanupConsumer;
24 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
25 import org.apache.maven.archiva.database.updater.TestDatabaseCleanupConsumer;
26 import org.apache.maven.archiva.database.updater.TestDatabaseUnprocessedConsumer;
27 import org.apache.maven.archiva.model.ArtifactReference;
28 import org.apache.maven.archiva.model.VersionedReference;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
31 import org.codehaus.plexus.jdo.JdoFactory;
32 import org.jpox.SchemaTool;
36 import java.text.SimpleDateFormat;
37 import java.util.Date;
38 import java.util.Iterator;
40 import java.util.Properties;
42 import javax.jdo.PersistenceManager;
43 import javax.jdo.PersistenceManagerFactory;
46 * AbstractArchivaDatabaseTestCase
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 public class AbstractArchivaDatabaseTestCase
52 extends PlexusTestCase
54 private static final String TIMESTAMP = "yyyy/MM/dd HH:mm:ss";
56 protected ArchivaDAO dao;
58 protected void setUp()
63 DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
64 assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
66 jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
69 File derbyDbDir = new File( "target/plexus-home/testdb" );
70 if ( derbyDbDir.exists() )
72 FileUtils.deleteDirectory( derbyDbDir );
75 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.apache.derby.jdbc.EmbeddedDriver" ) );
76 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:derby:" + derbyDbDir.getAbsolutePath() + ";create=true" ) );
79 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
80 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
82 jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
84 jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
86 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
88 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
90 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
92 jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
94 jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
96 // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
98 jdoFactory.setProperty( "org.jpox.validateTables", "true" );
100 jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
102 jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
104 Properties properties = jdoFactory.getProperties();
106 for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
108 Map.Entry entry = (Map.Entry) it.next();
110 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
113 URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
115 if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
117 fail( "Unable to process test " + getName() + " - missing package.jdo." );
120 File propsFile = null; // intentional
121 boolean verbose = true;
123 SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
124 SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
126 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
128 assertNotNull( pmf );
130 PersistenceManager pm = pmf.getPersistenceManager();
134 this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
137 protected TestDatabaseCleanupConsumer lookupTestCleanupConsumer()
140 TestDatabaseCleanupConsumer consumer = (TestDatabaseCleanupConsumer) lookup( DatabaseCleanupConsumer.class,
142 assertNotNull( "Test Database Cleanup Consumer should not be null.", consumer );
146 protected TestDatabaseUnprocessedConsumer lookupTestUnprocessedConsumer()
149 TestDatabaseUnprocessedConsumer consumer = (TestDatabaseUnprocessedConsumer) lookup(
150 DatabaseUnprocessedArtifactConsumer.class,
151 "test-db-unprocessed" );
152 assertNotNull( "Test Database Unprocessed Consumer should not be null.", consumer );
156 protected Date toDate( String txt )
159 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
160 return sdf.parse( txt );
163 protected String fromDate( Date date )
166 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
167 return sdf.format( date );
170 protected VersionedReference toVersionedReference( String id )
172 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
173 assertEquals( "Should have 3 parts [" + id + "]", 3, parts.length );
175 VersionedReference ref = new VersionedReference();
176 ref.setGroupId( parts[0] );
177 ref.setArtifactId( parts[1] );
178 ref.setVersion( parts[2] );
180 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
181 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
182 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
187 protected ArtifactReference toArtifactReference( String id )
189 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
190 assertEquals( "Should have 5 parts [" + id + "]", 5, parts.length );
192 ArtifactReference ref = new ArtifactReference();
193 ref.setGroupId( parts[0] );
194 ref.setArtifactId( parts[1] );
195 ref.setVersion( parts[2] );
196 ref.setClassifier( parts[3] );
197 ref.setType( parts[4] );
199 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
200 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
201 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
202 // Blank string is ok for classifier, NULL is not.
203 assertNotNull( "Classifier should not be null [" + id + "]", ref.getClassifier() );
204 assertTrue( "Type should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getType() ) );