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.Arrays;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
35 * UniqueArtifactIdConstraintTest
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
40 public class UniqueArtifactIdConstraintTest
41 extends AbstractArchivaDatabaseTestCase
43 private ArtifactDAO artifactDao;
45 protected void setUp()
50 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
51 artifactDao = dao.getArtifactDAO();
54 public ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
56 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
57 artifact.getModel().setLastModified( new Date() ); // mandatory field.
58 artifact.getModel().setRepositoryId( "testable_repo" );
62 public void testConstraint()
65 ArchivaArtifact artifact;
67 // Setup artifacts in fresh DB.
68 artifact = createArtifact( "commons-lang", "commons-lang", "2.0" );
69 artifactDao.saveArtifact( artifact );
71 artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
72 artifactDao.saveArtifact( artifact );
74 artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
75 artifactDao.saveArtifact( artifact );
77 artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
78 artifactDao.saveArtifact( artifact );
80 artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" );
81 artifactDao.saveArtifact( artifact );
83 artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1" );
84 artifactDao.saveArtifact( artifact );
86 artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" );
87 artifactDao.saveArtifact( artifact );
89 artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" );
90 artifactDao.saveArtifact( artifact );
92 assertConstraint( new String[] {}, new UniqueArtifactIdConstraint( "org.apache" ) );
93 assertConstraint( new String[] { "commons-lang" }, new UniqueArtifactIdConstraint( "commons-lang" ) );
94 assertConstraint( new String[] { "test-one" }, new UniqueArtifactIdConstraint( "org.apache.maven.test" ) );
95 assertConstraint( new String[] { "test-two", "test-bar" },
96 new UniqueArtifactIdConstraint( "org.apache.maven.shared" ) );
97 assertConstraint( new String[] { "modellong" }, new UniqueArtifactIdConstraint( "org.codehaus.modello" ) );
100 private void assertConstraint( String[] artifactIds, SimpleConstraint constraint )
102 String prefix = "Unique Artifact IDs: ";
104 List results = dao.query( constraint );
105 assertNotNull( prefix + "Not Null", results );
106 assertEquals( prefix + "Results.size", artifactIds.length, results.size() );
108 List expectedArtifactIds = Arrays.asList( artifactIds );
110 Iterator it = results.iterator();
111 while ( it.hasNext() )
113 String actualArtifactId = (String) it.next();
114 assertTrue( prefix + "artifactId result should not be blank.", StringUtils.isNotBlank( actualArtifactId ) );
115 assertTrue( prefix + " artifactId result <" + actualArtifactId + "> exists in expected artifactIds.",
116 expectedArtifactIds.contains( actualArtifactId ) );