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.jdo.DefaultConfigurableJdoFactory;
30 import org.codehaus.plexus.jdo.JdoFactory;
31 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
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
50 public abstract class AbstractArchivaDatabaseTestCase
51 extends PlexusInSpringTestCase
53 private static final String TIMESTAMP = "yyyy/MM/dd HH:mm:ss";
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" );
136 protected TestDatabaseCleanupConsumer lookupTestCleanupConsumer()
139 TestDatabaseCleanupConsumer consumer = (TestDatabaseCleanupConsumer) lookup( DatabaseCleanupConsumer.class,
141 assertNotNull( "Test Database Cleanup Consumer should not be null.", consumer );
145 protected TestDatabaseUnprocessedConsumer lookupTestUnprocessedConsumer()
148 TestDatabaseUnprocessedConsumer consumer = (TestDatabaseUnprocessedConsumer) lookup(
149 DatabaseUnprocessedArtifactConsumer.class,
150 "test-db-unprocessed" );
151 assertNotNull( "Test Database Unprocessed Consumer should not be null.", consumer );
155 protected Date toDate( String txt )
158 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
159 return sdf.parse( txt );
162 protected String fromDate( Date date )
165 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
166 return sdf.format( date );
169 protected VersionedReference toVersionedReference( String id )
171 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
172 assertEquals( "Should have 3 parts [" + id + "]", 3, parts.length );
174 VersionedReference ref = new VersionedReference();
175 ref.setGroupId( parts[0] );
176 ref.setArtifactId( parts[1] );
177 ref.setVersion( parts[2] );
179 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
180 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
181 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
186 protected ArtifactReference toArtifactReference( String id )
188 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
189 assertEquals( "Should have 5 parts [" + id + "]", 5, parts.length );
191 ArtifactReference ref = new ArtifactReference();
192 ref.setGroupId( parts[0] );
193 ref.setArtifactId( parts[1] );
194 ref.setVersion( parts[2] );
195 ref.setClassifier( parts[3] );
196 ref.setType( parts[4] );
198 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
199 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
200 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
201 // Blank string is ok for classifier, NULL is not.
202 assertNotNull( "Classifier should not be null [" + id + "]", ref.getClassifier() );
203 assertTrue( "Type should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getType() ) );