1 package org.apache.maven.archiva.database.constraints;
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.database.AbstractArchivaDatabaseTestCase;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.ArtifactDAO;
26 import org.apache.maven.archiva.database.SimpleConstraint;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.List;
36 * UniqueGroupIdConstraintTest
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class UniqueGroupIdConstraintTest
42 extends AbstractArchivaDatabaseTestCase
44 private ArtifactDAO artifactDao;
46 public void testConstraintGroupIdParamCommonsLang()
51 assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) );
54 public void testConstraintGroupIdParamNoRepos()
59 List<String> selectedRepos = new ArrayList<String>();
60 new UniqueGroupIdConstraint( selectedRepos, "org" );
61 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
63 catch ( IllegalArgumentException e )
69 public void testConstraintGroupIdParamNullRepos()
74 new UniqueGroupIdConstraint( (List<String>) null, "org" );
75 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
77 catch ( NullPointerException e )
83 public void testConstraintGroupIdParamOrg()
88 assertConstraint( new String[] {
89 "org.apache.maven.test",
90 "org.apache.maven.test.foo",
91 "org.apache.maven.shared",
93 "org.codehaus.modello",
94 "org.codehaus.mojo" }, new UniqueGroupIdConstraint( "org" ) );
97 public void testConstraintGroupIdParamOrgApache()
102 assertConstraint( new String[] {
103 "org.apache.maven.test",
104 "org.apache.maven.test.foo",
105 "org.apache.maven.shared",
106 "org.apache.archiva" }, new UniqueGroupIdConstraint( "org.apache" ) );
109 public void testConstraintGroupIdParamOrgApacheMaven()
114 assertConstraint( new String[] {
115 "org.apache.maven.test",
116 "org.apache.maven.test.foo",
117 "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) );
120 public void testConstraintGroupIdParamOrgApacheSnapshotsOnly()
125 List<String> observableRepositories = new ArrayList<String>();
126 observableRepositories.add( "snapshots" );
128 assertConstraint( new String[] { "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories,
132 public void testConstraintGroupIdParamOrgSnapshotsOnly()
137 List<String> observableRepositories = new ArrayList<String>();
138 observableRepositories.add( "snapshots" );
140 assertConstraint( new String[] { "org.apache.archiva", "org.codehaus.modello", "org.codehaus.mojo" },
141 new UniqueGroupIdConstraint( observableRepositories, "org" ) );
144 public void testConstraintNoGroupIdParam()
149 assertConstraint( new String[] {
151 "org.apache.maven.test",
152 "org.apache.maven.test.foo",
153 "org.apache.maven.shared",
154 "org.codehaus.modello",
156 "org.apache.archiva" }, new UniqueGroupIdConstraint() );
159 public void testConstraintNoGroupIdParamCentralAndSnapshots()
164 List<String> observableRepositories = new ArrayList<String>();
165 observableRepositories.add( "central" );
166 observableRepositories.add( "snapshots" );
168 assertConstraint( new String[] {
170 "org.apache.maven.test",
171 "org.apache.maven.test.foo",
172 "org.apache.maven.shared",
173 "org.codehaus.modello",
175 "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories ) );
178 public void testConstraintNoGroupIdParamCentralOnly()
183 List<String> observableRepositories = new ArrayList<String>();
184 observableRepositories.add( "central" );
186 assertConstraint( new String[] {
188 "org.apache.maven.test",
189 "org.apache.maven.test.foo",
190 "org.apache.maven.shared",
191 "org.codehaus.modello" }, new UniqueGroupIdConstraint( observableRepositories ) );
194 public void testConstraintNoGroupIdParamNoRepos()
199 List<String> selectedRepos = new ArrayList<String>();
200 new UniqueGroupIdConstraint( selectedRepos );
201 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
203 catch ( IllegalArgumentException e )
209 public void testConstraintNoGroupIdParamNullRepos()
214 new UniqueGroupIdConstraint( (List<String>) null );
215 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
217 catch ( NullPointerException e )
223 public void testConstraintNoGroupIdParamSnapshotsOnly()
228 List<String> observableRepositories = new ArrayList<String>();
229 observableRepositories.add( "snapshots" );
231 assertConstraint( new String[] { "org.codehaus.modello", "org.codehaus.mojo", "org.apache.archiva" },
232 new UniqueGroupIdConstraint( observableRepositories ) );
235 private void assertConstraint( String[] expectedGroupIds, SimpleConstraint constraint )
238 String prefix = "Unique Group IDs: ";
240 List<String> results = dao.query( constraint );
241 assertNotNull( prefix + "Not Null", results );
242 assertEquals( prefix + "Results.size", expectedGroupIds.length, results.size() );
244 List<String> groupIdList = Arrays.asList( expectedGroupIds );
246 Iterator<String> it = results.iterator();
247 while ( it.hasNext() )
249 String actualGroupId = (String) it.next();
250 assertTrue( prefix + "groupId result should not be blank.", StringUtils.isNotBlank( actualGroupId ) );
251 assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.", groupIdList
252 .contains( actualGroupId ) );
256 private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
258 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
259 artifact.getModel().setLastModified( new Date() ); // mandatory field.
260 artifact.getModel().setRepositoryId( repoId );
264 private void setupArtifacts()
267 ArchivaArtifact artifact;
269 // Setup artifacts in fresh DB.
270 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
271 artifactDao.saveArtifact( artifact );
273 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
274 artifactDao.saveArtifact( artifact );
276 artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
277 artifactDao.saveArtifact( artifact );
279 artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
280 artifactDao.saveArtifact( artifact );
282 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
283 artifactDao.saveArtifact( artifact );
285 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1" );
286 artifactDao.saveArtifact( artifact );
288 artifact = createArtifact( "central", "org.codehaus.modello", "test-two", "3.0" );
289 artifactDao.saveArtifact( artifact );
291 // Snapshots repository artifacts
292 artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
293 artifactDao.saveArtifact( artifact );
295 artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
296 artifactDao.saveArtifact( artifact );
298 artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
299 artifactDao.saveArtifact( artifact );
302 protected void setUp()
307 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
308 artifactDao = dao.getArtifactDAO();