* More unit testing of the archiva-database.

* Added CompoundKey tag to help JdoAccess process compound keys more intelligently.
* Updates to keys to honor CompoundKey identity.



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@526927 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-04-09 21:28:30 +00:00
parent ede71ae385
commit 84e5e12ca8
11 changed files with 412 additions and 68 deletions

View File

@ -76,7 +76,8 @@ import java.io.Serializable;
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class AbstractArtifactKey implements Serializable
public class AbstractArtifactKey
implements CompoundKey, Serializable
{
/**
* The Group ID. (JPOX Requires this remain public)
@ -96,12 +97,12 @@ public class AbstractArtifactKey implements Serializable
/**
* The Classifier. (JPOX Requires this remain public)
*/
public String classifier;
public String classifier = "";
/**
* The Type. (JPOX Requires this remain public)
*/
public String type;
public String type = "";
/**
* Default Constructor. Required by JPOX.
@ -119,11 +120,11 @@ public class AbstractArtifactKey implements Serializable
public AbstractArtifactKey( String key )
{
String parts[] = StringUtils.splitPreserveAllTokens( key, ":" );
groupId = parts[1];
artifactId = parts[2];
version = parts[3];
classifier = parts[4];
type = parts[5];
groupId = parts[0];
artifactId = parts[1];
version = parts[2];
classifier = parts[3];
type = parts[4];
}
/**
@ -131,7 +132,7 @@ public class AbstractArtifactKey implements Serializable
*/
public String toString()
{
return StringUtils.join( new String[] { groupId, artifactId, version, classifier, type } );
return StringUtils.join( new String[] { groupId, artifactId, version, classifier, type }, ':' );
}
/**
@ -233,4 +234,55 @@ public class AbstractArtifactKey implements Serializable
return true;
}
public void setGroupId( String groupId )
{
if ( StringUtils.isBlank( groupId ) )
{
throw new IllegalArgumentException( "A blank Group ID is not allowed." );
}
this.groupId = groupId;
}
public void setArtifactId( String artifactId )
{
if ( StringUtils.isBlank( artifactId ) )
{
throw new IllegalArgumentException( "A blank Artifact ID is not allowed." );
}
this.artifactId = artifactId;
}
public void setVersion( String version )
{
if ( StringUtils.isBlank( artifactId ) )
{
throw new IllegalArgumentException( "A blank version is not allowed." );
}
this.version = version;
}
public void setClassifier( String classifier )
{
this.classifier = "";
if ( StringUtils.isNotBlank( classifier ) )
{
this.classifier = classifier;
}
}
public void setType( String type )
{
this.type = "";
if ( StringUtils.isNotBlank( type ) )
{
this.type = type;
}
}
}

View File

@ -77,7 +77,7 @@ import java.io.Serializable;
* @version $Id$
*/
public class AbstractProjectKey
implements Serializable
implements CompoundKey, Serializable
{
/**
* The Group ID. (JPOX Requires this remain public)
@ -105,8 +105,8 @@ public class AbstractProjectKey
public AbstractProjectKey( String key )
{
String parts[] = StringUtils.splitPreserveAllTokens( key, ":" );
groupId = parts[1];
artifactId = parts[2];
groupId = parts[0];
artifactId = parts[1];
}
/**

View File

@ -76,7 +76,8 @@ import java.io.Serializable;
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class AbstractVersionedKey implements Serializable
public class AbstractVersionedKey
implements CompoundKey, Serializable
{
/**
* The Group ID. (JPOX Requires this remain public)
@ -109,9 +110,9 @@ public class AbstractVersionedKey implements Serializable
public AbstractVersionedKey( String key )
{
String parts[] = StringUtils.splitPreserveAllTokens( key, ":" );
groupId = parts[1];
artifactId = parts[2];
version = parts[3];
groupId = parts[0];
artifactId = parts[1];
version = parts[2];
}
/**
@ -119,7 +120,7 @@ public class AbstractVersionedKey implements Serializable
*/
public String toString()
{
return StringUtils.join( new String[] { groupId, artifactId, version } );
return StringUtils.join( new String[] { groupId, artifactId, version }, ':' );
}
/**

View File

@ -0,0 +1,31 @@
package org.apache.maven.archiva.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* Tag for identifying a Compound Key
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public interface CompoundKey
{
public String toString();
}

View File

@ -225,7 +225,7 @@
<identifier>true</identifier>
<version>1.0.0+</version>
<type>String</type>
<required>false</required>
<required>true</required>
<description>
The version of the repository content.
</description>
@ -406,7 +406,7 @@
<identifier>true</identifier>
<version>1.0.0+</version>
<type>String</type>
<required>false</required>
<required>true</required>
<description>
The version of the repository content.
</description>
@ -621,7 +621,7 @@
<identifier>true</identifier>
<version>1.0.0+</version>
<type>String</type>
<required>false</required>
<required>true</required>
<description>
The version of the repository content.
</description>

View File

@ -57,7 +57,7 @@ public interface ProjectModelDAO
public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version )
throws ObjectNotFoundException, ArchivaDatabaseException;
public List /*<ArchivaProjectModel>*/queryProjectModel( Constraint constraint )
public List /*<ArchivaProjectModel>*/queryProjectModels( Constraint constraint )
throws ObjectNotFoundException, ArchivaDatabaseException;
public ArchivaProjectModel saveProjectModel( ArchivaProjectModel model )

View File

@ -23,6 +23,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.Constraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.CompoundKey;
import org.codehaus.plexus.jdo.JdoFactory;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
@ -299,7 +300,16 @@ public class JdoAccess
pm.getFetchPlan().addGroup( fetchGroup );
}
Object objectId = pm.newObjectIdInstance( clazz, id );
Object objectId = null;
if ( id instanceof CompoundKey )
{
objectId = pm.newObjectIdInstance( clazz, id.toString() );
}
else
{
objectId = pm.newObjectIdInstance( clazz, id );
}
Object object = pm.getObjectById( objectId );
@ -311,13 +321,15 @@ public class JdoAccess
}
catch ( JDOObjectNotFoundException e )
{
throw new ObjectNotFoundException( "Unable to find Database Object '" + id + "' of type " + clazz.getName()
+ " using fetch-group '" + fetchGroup + "'", e, id );
throw new ObjectNotFoundException( "Unable to find Database Object [" + id + "] of type " + clazz.getName()
+ " using " + ( ( fetchGroup == null ) ? "no fetch-group" : "a fetch-group of [" + fetchGroup + "]" ),
e, id );
}
catch ( JDOException e )
{
throw new ArchivaDatabaseException( "Error in JDO during get of Database object id '" + id + "' of type "
+ clazz.getName() + " using fetch-group '" + fetchGroup + "'", e );
throw new ArchivaDatabaseException( "Error in JDO during get of Database object id [" + id + "] of type "
+ clazz.getName() + " using "
+ ( ( fetchGroup == null ) ? "no fetch-group" : "a fetch-group of [" + fetchGroup + "]" ), e );
}
finally
{

View File

@ -26,6 +26,7 @@ import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaArtifactModel;
import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.util.ArrayList;
import java.util.Iterator;
@ -40,6 +41,7 @@ import java.util.List;
* @plexus.component role-hint="jdo"
*/
public class JdoArtifactDAO
extends AbstractLogEnabled
implements ArtifactDAO
{
/**
@ -60,6 +62,12 @@ public class JdoArtifactDAO
}
catch ( ArchivaDatabaseException e )
{
if ( !( e instanceof ObjectNotFoundException ) )
{
getLogger().warn(
"Unable to get artifact [" + groupId + ":" + artifactId + ":" + version + ":"
+ classifier + ":" + type + "]: " + e.getMessage(), e );
}
artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type );
}
@ -71,11 +79,11 @@ public class JdoArtifactDAO
throws ObjectNotFoundException, ArchivaDatabaseException
{
ArchivaArtifactModelKey key = new ArchivaArtifactModelKey();
key.groupId = groupId;
key.artifactId = artifactId;
key.version = version;
key.classifier = classifier;
key.type = type;
key.setGroupId( groupId );
key.setArtifactId( artifactId );
key.setVersion( version );
key.setClassifier( classifier );
key.setType( type );
ArchivaArtifactModel model = (ArchivaArtifactModel) jdo.getObjectById( ArchivaArtifactModel.class, key, null );

View File

@ -24,6 +24,7 @@ import org.apache.maven.archiva.database.Constraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.model.ArchivaProjectModel;
import org.apache.maven.archiva.model.jpox.ArchivaProjectModelKey;
import java.util.List;
@ -45,36 +46,49 @@ public class JdoProjectModelDAO
public ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
{
ArchivaProjectModel model;
return null;
try
{
model = getProjectModel( groupId, artifactId, version );
}
catch ( ArchivaDatabaseException e )
{
model = new ArchivaProjectModel();
model.setGroupId( groupId );
model.setArtifactId( artifactId );
model.setVersion( version );
}
return model;
}
public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version )
throws ObjectNotFoundException, ArchivaDatabaseException
{
ArchivaProjectModelKey key = new ArchivaProjectModelKey();
key.groupId = groupId;
key.artifactId = artifactId;
key.version = version;
return null;
return (ArchivaProjectModel) jdo.getObjectById( ArchivaProjectModel.class, key, null );
}
public List queryProjectModel( Constraint constraint )
public List queryProjectModels( Constraint constraint )
throws ObjectNotFoundException, ArchivaDatabaseException
{
// TODO Auto-generated method stub
return null;
return jdo.getAllObjects( ArchivaProjectModel.class, constraint );
}
public ArchivaProjectModel saveProjectModel( ArchivaProjectModel model )
throws ArchivaDatabaseException
{
// TODO Auto-generated method stub
return null;
return (ArchivaProjectModel) jdo.saveObject( model );
}
public void deleteProjectModel( ArchivaProjectModel model )
throws ArchivaDatabaseException
{
// TODO Auto-generated method stub
jdo.removeObject( model );
}
}

View File

@ -0,0 +1,115 @@
package org.apache.maven.archiva.database.jdo;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaArtifactModel;
import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey;
import java.util.Date;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.spi.JDOImplHelper;
/**
* JdoArtifactDAOTest
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class JdoArtifactDAOTest
extends AbstractArchivaDatabaseTestCase
{
public void testArtifactKey()
{
Object o = JDOImplHelper.getInstance().newObjectIdInstance( ArchivaArtifactModel.class, "foo:bar:1.0::jar" );
assertNotNull( "Key should not be null.", o );
assertTrue( "Key should be an instance of " + ArchivaArtifactModelKey.class.getName(),
( o instanceof ArchivaArtifactModelKey ) );
ArchivaArtifactModelKey key = (ArchivaArtifactModelKey) o;
assertEquals( "foo", key.groupId );
assertEquals( "bar", key.artifactId );
assertEquals( "1.0", key.version );
assertEquals( "", key.classifier );
assertEquals( "jar", key.type );
}
public void testArtifactCRUD()
throws Exception
{
ArtifactDAO artiDao = dao.getArtifactDAO();
// Create it
ArchivaArtifact artifact = artiDao.createArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0",
"", "jar" );
assertNotNull( artifact );
// Set some mandatory values
artifact.getModel().setLastModified( new Date() );
artifact.getModel().setOrigin( "test" );
// Save it.
ArchivaArtifact savedArtifact = artiDao.saveArtifact( artifact );
assertNotNull( savedArtifact );
String savedKeyId = JDOHelper.getObjectId( savedArtifact.getModel() ).toString();
assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0::jar", savedKeyId );
// Test that something has been saved.
List artifacts = artiDao.queryArtifacts( null );
assertNotNull( artifacts );
assertEquals( 1, artifacts.size() );
// Test that retrieved object is what we expect.
ArchivaArtifact firstArtifact = (ArchivaArtifact) artifacts.get( 0 );
assertNotNull( firstArtifact );
assertEquals( "org.apache.maven.archiva", firstArtifact.getGroupId() );
assertEquals( "archiva-test-module", firstArtifact.getArtifactId() );
assertEquals( "1.0", firstArtifact.getVersion() );
assertEquals( "", firstArtifact.getClassifier() );
assertEquals( "jar", firstArtifact.getType() );
// Change value and save.
savedArtifact.getModel().setOrigin( "changed" );
artiDao.saveArtifact( savedArtifact );
// Test that only 1 object is saved.
assertEquals( 1, artiDao.queryArtifacts( null ).size() );
// Get the specific artifact.
ArchivaArtifact actualArtifact = artiDao.getArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0",
null, "jar" );
assertNotNull( actualArtifact );
// Test expected values.
assertEquals( "archiva-test-module", actualArtifact.getArtifactId() );
assertEquals( "changed", actualArtifact.getModel().getOrigin() );
// Test that only 1 object is saved.
assertEquals( 1, artiDao.queryArtifacts( null ).size() );
// Delete object.
artiDao.deleteArtifact( actualArtifact );
assertEquals( 0, artiDao.queryArtifacts( null ).size() );
}
}

View File

@ -0,0 +1,111 @@
package org.apache.maven.archiva.database.jdo;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.model.ArchivaProjectModel;
import org.apache.maven.archiva.model.jpox.ArchivaProjectModelKey;
import java.util.Date;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.spi.JDOImplHelper;
/**
* JdoProjectModelDAOTest
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class JdoProjectModelDAOTest
extends AbstractArchivaDatabaseTestCase
{
public void testProjectModelKey()
{
Object o = JDOImplHelper.getInstance().newObjectIdInstance( ArchivaProjectModel.class, "foo:bar:1.0" );
assertNotNull( "Key should not be null.", o );
assertTrue( "Key should be an instance of " + ArchivaProjectModelKey.class.getName(),
( o instanceof ArchivaProjectModelKey ) );
ArchivaProjectModelKey key = (ArchivaProjectModelKey) o;
assertEquals( "foo", key.groupId );
assertEquals( "bar", key.artifactId );
assertEquals( "1.0", key.version );
}
public void testProjectModelCRUD()
throws Exception
{
ProjectModelDAO projectDao = dao.getProjectModelDAO();
// Create it
ArchivaProjectModel model = projectDao.createProjectModel( "org.apache.maven.archiva", "archiva-test-module",
"1.0" );
assertNotNull( model );
// Set some mandatory values
model.setPackaging( "pom" );
model.setWhenIndexed( new Date() );
model.setOrigin( "test" );
// Save it.
ArchivaProjectModel savedModel = projectDao.saveProjectModel( model );
assertNotNull( savedModel );
String savedKeyId = JDOHelper.getObjectId( savedModel ).toString();
assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0", savedKeyId );
// Test that something has been saved.
List projects = projectDao.queryProjectModels( null );
assertNotNull( projects );
assertEquals( 1, projects.size() );
// Test that retrieved object is what we expect.
ArchivaProjectModel firstModel = (ArchivaProjectModel) projects.get( 0 );
assertNotNull( firstModel );
assertEquals( "org.apache.maven.archiva", firstModel.getGroupId() );
assertEquals( "archiva-test-module", firstModel.getArtifactId() );
assertEquals( "1.0", firstModel.getVersion() );
// Change value and save.
savedModel.setOrigin( "changed" );
projectDao.saveProjectModel( savedModel );
// Test that only 1 object is saved.
assertEquals( 1, projectDao.queryProjectModels( null ).size() );
// Get the specific artifact.
ArchivaProjectModel actualModel = projectDao.getProjectModel( "org.apache.maven.archiva",
"archiva-test-module", "1.0" );
assertNotNull( actualModel );
// Test expected values.
assertEquals( "archiva-test-module", actualModel.getArtifactId() );
assertEquals( "changed", actualModel.getOrigin() );
// Test that only 1 object is saved.
assertEquals( 1, projectDao.queryProjectModels( null ).size() );
// Delete object.
projectDao.deleteProjectModel( actualModel );
assertEquals( 0, projectDao.queryProjectModels( null ).size() );
}
}