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
40 public class UniqueGroupIdConstraintTest
41 extends AbstractArchivaDatabaseTestCase
43 private ArtifactDAO artifactDao;
45 public void testConstraintGroupIdParamCommonsLang()
50 assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) );
53 public void testConstraintGroupIdParamNoRepos()
58 List<String> selectedRepos = new ArrayList<String>();
59 new UniqueGroupIdConstraint( selectedRepos, "org" );
60 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
62 catch ( IllegalArgumentException e )
68 public void testConstraintGroupIdParamNullRepos()
73 new UniqueGroupIdConstraint( (List<String>) null, "org" );
74 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
76 catch ( NullPointerException e )
82 public void testConstraintGroupIdParamOrg()
87 assertConstraint( new String[] {
88 "org.apache.maven.test",
89 "org.apache.maven.test.foo",
90 "org.apache.maven.shared",
92 "org.codehaus.modello",
93 "org.codehaus.mojo" }, new UniqueGroupIdConstraint( "org" ) );
96 public void testConstraintGroupIdParamOrgApache()
101 assertConstraint( new String[] {
102 "org.apache.maven.test",
103 "org.apache.maven.test.foo",
104 "org.apache.maven.shared",
105 "org.apache.archiva" }, new UniqueGroupIdConstraint( "org.apache" ) );
108 public void testConstraintGroupIdParamOrgApacheMaven()
113 assertConstraint( new String[] {
114 "org.apache.maven.test",
115 "org.apache.maven.test.foo",
116 "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) );
119 public void testConstraintGroupIdParamOrgApacheSnapshotsOnly()
124 List<String> observableRepositories = new ArrayList<String>();
125 observableRepositories.add( "snapshots" );
127 assertConstraint( new String[] { "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories,
131 public void testConstraintGroupIdParamOrgSnapshotsOnly()
136 List<String> observableRepositories = new ArrayList<String>();
137 observableRepositories.add( "snapshots" );
139 assertConstraint( new String[] { "org.apache.archiva", "org.codehaus.modello", "org.codehaus.mojo" },
140 new UniqueGroupIdConstraint( observableRepositories, "org" ) );
143 public void testConstraintNoGroupIdParam()
148 assertConstraint( new String[] {
150 "org.apache.maven.test",
151 "org.apache.maven.test.foo",
152 "org.apache.maven.shared",
153 "org.codehaus.modello",
155 "org.apache.archiva" }, new UniqueGroupIdConstraint() );
158 public void testConstraintNoGroupIdParamCentralAndSnapshots()
163 List<String> observableRepositories = new ArrayList<String>();
164 observableRepositories.add( "central" );
165 observableRepositories.add( "snapshots" );
167 assertConstraint( new String[] {
169 "org.apache.maven.test",
170 "org.apache.maven.test.foo",
171 "org.apache.maven.shared",
172 "org.codehaus.modello",
174 "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories ) );
177 public void testConstraintNoGroupIdParamCentralOnly()
182 List<String> observableRepositories = new ArrayList<String>();
183 observableRepositories.add( "central" );
185 assertConstraint( new String[] {
187 "org.apache.maven.test",
188 "org.apache.maven.test.foo",
189 "org.apache.maven.shared",
190 "org.codehaus.modello" }, new UniqueGroupIdConstraint( observableRepositories ) );
193 public void testConstraintNoGroupIdParamNoRepos()
198 List<String> selectedRepos = new ArrayList<String>();
199 new UniqueGroupIdConstraint( selectedRepos );
200 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
202 catch ( IllegalArgumentException e )
208 public void testConstraintNoGroupIdParamNullRepos()
213 new UniqueGroupIdConstraint( (List<String>) null );
214 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
216 catch ( NullPointerException e )
222 public void testConstraintNoGroupIdParamSnapshotsOnly()
227 List<String> observableRepositories = new ArrayList<String>();
228 observableRepositories.add( "snapshots" );
230 assertConstraint( new String[] { "org.codehaus.modello", "org.codehaus.mojo", "org.apache.archiva" },
231 new UniqueGroupIdConstraint( observableRepositories ) );
234 private void assertConstraint( String[] expectedGroupIds, SimpleConstraint constraint )
237 String prefix = "Unique Group IDs: ";
239 List<String> results = dao.query( constraint );
240 assertNotNull( prefix + "Not Null", results );
241 assertEquals( prefix + "Results.size", expectedGroupIds.length, results.size() );
243 List<String> groupIdList = Arrays.asList( expectedGroupIds );
245 Iterator<String> it = results.iterator();
246 while ( it.hasNext() )
248 String actualGroupId = (String) it.next();
249 assertTrue( prefix + "groupId result should not be blank.", StringUtils.isNotBlank( actualGroupId ) );
250 assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.", groupIdList
251 .contains( actualGroupId ) );
255 private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
257 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "testrepo" );
258 artifact.getModel().setLastModified( new Date() ); // mandatory field.
259 artifact.getModel().setRepositoryId( repoId );
263 private void setupArtifacts()
266 ArchivaArtifact artifact;
268 // Setup artifacts in fresh DB.
269 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
270 artifactDao.saveArtifact( artifact );
272 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
273 artifactDao.saveArtifact( artifact );
275 artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
276 artifactDao.saveArtifact( artifact );
278 artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
279 artifactDao.saveArtifact( artifact );
281 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
282 artifactDao.saveArtifact( artifact );
284 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1" );
285 artifactDao.saveArtifact( artifact );
287 artifact = createArtifact( "central", "org.codehaus.modello", "test-two", "3.0" );
288 artifactDao.saveArtifact( artifact );
290 // Snapshots repository artifacts
291 artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
292 artifactDao.saveArtifact( artifact );
294 artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
295 artifactDao.saveArtifact( artifact );
297 artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
298 artifactDao.saveArtifact( artifact );
301 protected void setUp()
306 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
307 artifactDao = dao.getArtifactDAO();