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.commons.beanutils.PropertyUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
25 import org.apache.maven.archiva.database.ProjectModelDAO;
26 import org.apache.maven.archiva.model.ArchivaProjectModel;
27 import org.apache.maven.archiva.repository.project.ProjectModelReader;
28 import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.Iterator;
34 import java.util.List;
36 import javax.jdo.JDOHelper;
39 * JdoProjectModelDAOTest
43 public class JdoProjectModelDAOTest
44 extends AbstractArchivaDatabaseTestCase
46 public void testProjectModelCRUD()
49 ProjectModelDAO projectDao = dao.getProjectModelDAO();
52 ArchivaProjectModel model = projectDao.createProjectModel( "org.apache.maven.archiva", "archiva-test-module",
54 assertNotNull( model );
56 // Set some mandatory values
57 model.setPackaging( "pom" );
58 model.setWhenIndexed( new Date() );
59 model.setOrigin( "test" );
62 ArchivaProjectModel savedModel = projectDao.saveProjectModel( model );
63 assertNotNull( savedModel );
64 String savedKeyId = JDOHelper.getObjectId( savedModel ).toString();
65 assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0", savedKeyId );
67 // Test that something has been saved.
68 List projects = projectDao.queryProjectModels( null );
69 assertNotNull( projects );
70 assertEquals( 1, projects.size() );
72 // Test that retrieved object is what we expect.
73 ArchivaProjectModel firstModel = (ArchivaProjectModel) projects.get( 0 );
74 assertNotNull( firstModel );
75 assertEquals( "org.apache.maven.archiva", firstModel.getGroupId() );
76 assertEquals( "archiva-test-module", firstModel.getArtifactId() );
77 assertEquals( "1.0", firstModel.getVersion() );
79 // Change value and save.
80 savedModel.setOrigin( "changed" );
81 projectDao.saveProjectModel( savedModel );
83 // Test that only 1 object is saved.
84 assertEquals( 1, projectDao.queryProjectModels( null ).size() );
86 // Get the specific artifact.
87 ArchivaProjectModel actualModel = projectDao.getProjectModel( "org.apache.maven.archiva",
88 "archiva-test-module", "1.0" );
89 assertNotNull( actualModel );
91 // Test expected values.
92 assertEquals( "archiva-test-module", actualModel.getArtifactId() );
93 assertEquals( "changed", actualModel.getOrigin() );
95 // Test that only 1 object is saved.
96 assertEquals( 1, projectDao.queryProjectModels( null ).size() );
99 projectDao.deleteProjectModel( actualModel );
100 assertEquals( 0, projectDao.queryProjectModels( null ).size() );
103 public void testSaveGetRealProjectModel()
106 String groupId = "org.apache.maven.shared";
107 String artifactId = "maven-shared-jar";
108 String version = "1.0-SNAPSHOT";
110 ProjectModelDAO projectDao = dao.getProjectModelDAO();
112 ProjectModelReader modelReader = new ProjectModel400Reader();
114 File pomFile = getTestFile( "src/test/resources/projects/maven-shared-jar-1.0-SNAPSHOT.pom" );
116 assertTrue( "pom file should exist: " + pomFile.getAbsolutePath(), pomFile.exists() && pomFile.isFile() );
118 ArchivaProjectModel model = modelReader.read( pomFile );
119 assertNotNull( "Model should not be null.", model );
121 /* NOTE: We are intentionally using a basic project model in this unit test.
122 * The expansion of expressions, resolving of dependencies, and merging
123 * of parent poms is *NOT* performed to keep this unit test simple.
126 // Fill in mandatory/missing fields
127 model.setGroupId( groupId );
128 model.setOrigin( "testcase" );
130 projectDao.saveProjectModel( model );
132 ArchivaProjectModel savedModel = projectDao.getProjectModel( groupId, artifactId, version );
133 assertNotNull( "Project model should not be null.", savedModel );
135 // Test proper detachment of sub-objects.
136 List exprs = new ArrayList();
137 exprs.add( "parentProject.groupId" );
138 exprs.add( "organization.name" );
139 exprs.add( "issueManagement.system" );
140 exprs.add( "ciManagement.system" );
141 exprs.add( "scm.url" );
142 exprs.add( "individuals[0].name" );
143 exprs.add( "dependencies[0].groupId" );
144 exprs.add( "dependencyManagement[0].artifactId" );
145 exprs.add( "repositories[0].id" );
146 exprs.add( "plugins[0].artifactId" );
147 exprs.add( "reports[0].artifactId" );
148 exprs.add( "buildExtensions[0].artifactId" );
149 exprs.add( "licenses[0].url" );
150 exprs.add( "mailingLists[0].name" );
152 Iterator it = exprs.iterator();
153 while ( it.hasNext() )
155 String expr = (String) it.next();
158 Object obj = PropertyUtils.getProperty( model, expr );
159 assertNotNull( "Expr \"" + expr + "\" != null", obj );
160 assertTrue( "Expr \"" + expr + "\" should be a String.", ( obj instanceof String ) );
161 String value = (String) obj;
162 assertTrue( "Expr \"" + expr + "\" value should not be blank.", StringUtils.isNotBlank( value ) );
164 catch ( IndexOutOfBoundsException e )
166 fail( "Expr \"" + expr + "\" unable to get indexed property: " + e.getClass().getName() + ": "