From 84e5e12ca820af1557ad20c67bcb8fc4b69184aa Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 9 Apr 2007 21:28:30 +0000 Subject: [PATCH] * 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 --- .../archiva/model/AbstractArtifactKey.java | 98 +++++++++++---- .../archiva/model/AbstractProjectKey.java | 6 +- .../archiva/model/AbstractVersionedKey.java | 31 ++--- .../maven/archiva/model/CompoundKey.java | 31 +++++ .../src/main/mdo/archiva-base.xml | 6 +- .../archiva/database/ProjectModelDAO.java | 2 +- .../maven/archiva/database/jdo/JdoAccess.java | 22 +++- .../archiva/database/jdo/JdoArtifactDAO.java | 20 ++- .../database/jdo/JdoProjectModelDAO.java | 38 ++++-- .../database/jdo/JdoArtifactDAOTest.java | 115 ++++++++++++++++++ .../database/jdo/JdoProjectModelDAOTest.java | 111 +++++++++++++++++ 11 files changed, 412 insertions(+), 68 deletions(-) create mode 100644 archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/CompoundKey.java create mode 100644 archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java create mode 100644 archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java diff --git a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractArtifactKey.java b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractArtifactKey.java index 2c4f987a9..d31be8a92 100644 --- a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractArtifactKey.java +++ b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractArtifactKey.java @@ -76,7 +76,8 @@ import java.io.Serializable; * @author Joakim Erdfelt * @version $Id$ */ -public class AbstractArtifactKey implements Serializable +public class AbstractArtifactKey + implements CompoundKey, Serializable { /** * The Group ID. (JPOX Requires this remain public) @@ -92,17 +93,17 @@ public class AbstractArtifactKey implements Serializable * The Version. (JPOX Requires this remain public) */ public String version = ""; - + /** * 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. */ @@ -110,7 +111,7 @@ public class AbstractArtifactKey implements Serializable { /* do nothing */ } - + /** * Key Based Constructor. Required by JPOX. * @@ -119,19 +120,19 @@ 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]; } - + /** * Get the String representation of this object. - Required by JPOX. */ public String toString() { - return StringUtils.join( new String[] { groupId, artifactId, version, classifier, type } ); + return StringUtils.join( new String[] { groupId, artifactId, version, classifier, type }, ':' ); } /** @@ -158,19 +159,19 @@ public class AbstractArtifactKey implements Serializable { return true; } - + if ( !super.equals( obj ) ) { return false; } - + if ( getClass() != obj.getClass() ) { return false; } - + final AbstractArtifactKey other = (AbstractArtifactKey) obj; - + if ( groupId == null ) { if ( other.groupId != null ) @@ -182,7 +183,7 @@ public class AbstractArtifactKey implements Serializable { return false; } - + if ( artifactId == null ) { if ( other.artifactId != null ) @@ -194,7 +195,7 @@ public class AbstractArtifactKey implements Serializable { return false; } - + if ( version == null ) { if ( other.version != null ) @@ -206,7 +207,7 @@ public class AbstractArtifactKey implements Serializable { return false; } - + if ( classifier == null ) { if ( other.classifier != null ) @@ -218,7 +219,7 @@ public class AbstractArtifactKey implements Serializable { return false; } - + if ( type == null ) { if ( other.type != null ) @@ -230,7 +231,58 @@ public class AbstractArtifactKey implements Serializable { return false; } - + 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; + } + } + } diff --git a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractProjectKey.java b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractProjectKey.java index 23f628cb6..1eb79c1f7 100644 --- a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractProjectKey.java +++ b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractProjectKey.java @@ -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]; } /** diff --git a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractVersionedKey.java b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractVersionedKey.java index c173a3574..845b16f81 100644 --- a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractVersionedKey.java +++ b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/AbstractVersionedKey.java @@ -76,7 +76,8 @@ import java.io.Serializable; * @author Joakim Erdfelt * @version $Id$ */ -public class AbstractVersionedKey implements Serializable +public class AbstractVersionedKey + implements CompoundKey, Serializable { /** * The Group ID. (JPOX Requires this remain public) @@ -92,7 +93,7 @@ public class AbstractVersionedKey implements Serializable * The Version. (JPOX Requires this remain public) */ public String version = ""; - + /** * Default Constructor. Required by JPOX. */ @@ -100,7 +101,7 @@ public class AbstractVersionedKey implements Serializable { /* do nothing */ } - + /** * Key Based Constructor. Required by JPOX. * @@ -109,17 +110,17 @@ 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]; } - + /** * Get the String representation of this object. - Required by JPOX. */ public String toString() { - return StringUtils.join( new String[] { groupId, artifactId, version } ); + return StringUtils.join( new String[] { groupId, artifactId, version }, ':' ); } /** @@ -144,19 +145,19 @@ public class AbstractVersionedKey implements Serializable { return true; } - + if ( !super.equals( obj ) ) { return false; } - + if ( getClass() != obj.getClass() ) { return false; } - + final AbstractVersionedKey other = (AbstractVersionedKey) obj; - + if ( groupId == null ) { if ( other.groupId != null ) @@ -168,7 +169,7 @@ public class AbstractVersionedKey implements Serializable { return false; } - + if ( artifactId == null ) { if ( other.artifactId != null ) @@ -180,7 +181,7 @@ public class AbstractVersionedKey implements Serializable { return false; } - + if ( version == null ) { if ( other.version != null ) @@ -192,7 +193,7 @@ public class AbstractVersionedKey implements Serializable { return false; } - + return true; } } diff --git a/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/CompoundKey.java b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/CompoundKey.java new file mode 100644 index 000000000..b6adde705 --- /dev/null +++ b/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/CompoundKey.java @@ -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 Joakim Erdfelt + * @version $Id$ + */ +public interface CompoundKey +{ + public String toString(); +} diff --git a/archiva-base/archiva-model/src/main/mdo/archiva-base.xml b/archiva-base/archiva-model/src/main/mdo/archiva-base.xml index 0f6587666..b8d17158b 100644 --- a/archiva-base/archiva-model/src/main/mdo/archiva-base.xml +++ b/archiva-base/archiva-model/src/main/mdo/archiva-base.xml @@ -225,7 +225,7 @@ true 1.0.0+ String - false + true The version of the repository content. @@ -406,7 +406,7 @@ true 1.0.0+ String - false + true The version of the repository content. @@ -621,7 +621,7 @@ true 1.0.0+ String - false + true The version of the repository content. diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java index 942a8f087..210c18982 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java @@ -57,7 +57,7 @@ public interface ProjectModelDAO public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version ) throws ObjectNotFoundException, ArchivaDatabaseException; - public List /**/queryProjectModel( Constraint constraint ) + public List /**/queryProjectModels( Constraint constraint ) throws ObjectNotFoundException, ArchivaDatabaseException; public ArchivaProjectModel saveProjectModel( ArchivaProjectModel model ) diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java index bd5bf7af9..73c262cec 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java @@ -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 { diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java index 841494753..0980226c4 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java @@ -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,14 +79,14 @@ 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 ); - + return new ArchivaArtifact( model ); } diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java index 495d1184e..f5d8cbe4f 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java @@ -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 ) { - - return null; + ArchivaProjectModel model; + + 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 { - - return null; + ArchivaProjectModelKey key = new ArchivaProjectModelKey(); + key.groupId = groupId; + key.artifactId = artifactId; + key.version = version; + + 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 ); } - } diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java new file mode 100644 index 000000000..899d77518 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java @@ -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 Joakim Erdfelt + * @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() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java new file mode 100644 index 000000000..edb13129d --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java @@ -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 Joakim Erdfelt + * @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() ); + } +}