1 package org.apache.maven.archiva.database.jdo;
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.maven.archiva.database.AbstractArchivaDatabaseTestCase;
23 import org.apache.maven.archiva.database.ArtifactDAO;
24 import org.apache.maven.archiva.model.ArchivaArtifact;
25 import org.apache.maven.archiva.model.ArchivaArtifactModel;
26 import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey;
28 import java.util.Date;
29 import java.util.List;
31 import javax.jdo.JDOHelper;
32 import javax.jdo.spi.JDOImplHelper;
39 public class JdoArtifactDAOTest
40 extends AbstractArchivaDatabaseTestCase
42 public void testArtifactKey()
44 Object o = JDOImplHelper.getInstance().newObjectIdInstance( ArchivaArtifactModel.class, "foo:bar:1.0::jar:testrepo" );
45 assertNotNull( "Key should not be null.", o );
46 assertTrue( "Key should be an instance of " + ArchivaArtifactModelKey.class.getName(),
47 ( o instanceof ArchivaArtifactModelKey ) );
49 ArchivaArtifactModelKey key = (ArchivaArtifactModelKey) o;
50 assertEquals( "foo", key.groupId );
51 assertEquals( "bar", key.artifactId );
52 assertEquals( "1.0", key.version );
53 assertEquals( "", key.classifier );
54 assertEquals( "jar", key.type );
55 assertEquals("testrepo", key.repositoryId);
58 public void testArtifactCRUD()
61 ArtifactDAO artiDao = dao.getArtifactDAO();
64 ArchivaArtifact artifact = artiDao.createArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0",
65 "", "jar", "testrepo" );
66 assertNotNull( artifact );
68 // Set some mandatory values
69 artifact.getModel().setLastModified( new Date() );
70 artifact.getModel().setOrigin( "test" );
73 ArchivaArtifact savedArtifact = artiDao.saveArtifact( artifact );
74 assertNotNull( savedArtifact );
75 String savedKeyId = JDOHelper.getObjectId( savedArtifact.getModel() ).toString();
76 assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0::jar:testrepo", savedKeyId );
78 // Test that something has been saved.
79 List artifacts = artiDao.queryArtifacts( null );
80 assertNotNull( artifacts );
81 assertEquals( 1, artifacts.size() );
83 // Test that retrieved object is what we expect.
84 ArchivaArtifact firstArtifact = (ArchivaArtifact) artifacts.get( 0 );
85 assertNotNull( firstArtifact );
86 assertEquals( "org.apache.maven.archiva", firstArtifact.getGroupId() );
87 assertEquals( "archiva-test-module", firstArtifact.getArtifactId() );
88 assertEquals( "1.0", firstArtifact.getVersion() );
89 assertEquals( "", firstArtifact.getClassifier() );
90 assertEquals( "jar", firstArtifact.getType() );
92 // Change value and save.
93 savedArtifact.getModel().setOrigin( "changed" );
94 artiDao.saveArtifact( savedArtifact );
96 // Test that only 1 object is saved.
97 assertEquals( 1, artiDao.queryArtifacts( null ).size() );
99 // Get the specific artifact.
100 ArchivaArtifact actualArtifact = artiDao.getArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0",
101 null, "jar", "testrepo" );
102 assertNotNull( actualArtifact );
104 // Test expected values.
105 assertEquals( "archiva-test-module", actualArtifact.getArtifactId() );
106 assertEquals( "changed", actualArtifact.getModel().getOrigin() );
108 // Test that only 1 object is saved.
109 assertEquals( 1, artiDao.queryArtifacts( null ).size() );
112 artiDao.deleteArtifact( actualArtifact );
113 assertEquals( 0, artiDao.queryArtifacts( null ).size() );