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.ArchivaDatabaseException;
26 import org.apache.maven.archiva.database.ArtifactDAO;
27 import org.apache.maven.archiva.database.SimpleConstraint;
28 import org.apache.maven.archiva.model.ArchivaArtifact;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.List;
36 * UniqueArtifactIdConstraintTest
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", "testable_repo" );
57 artifact.getModel().setLastModified( new Date() ); // mandatory field.
58 artifact.getModel().setRepositoryId( "testable_repo" );
62 public void testConstraint()
67 assertConstraint( new String[] {}, new UniqueArtifactIdConstraint( "org.apache" ) );
68 assertConstraint( new String[] { "commons-lang" }, new UniqueArtifactIdConstraint( "commons-lang" ) );
69 assertConstraint( new String[] { "test-one" }, new UniqueArtifactIdConstraint( "org.apache.maven.test" ) );
70 assertConstraint( new String[] { "test-two", "test-bar" },
71 new UniqueArtifactIdConstraint( "org.apache.maven.shared" ) );
72 assertConstraint( new String[] { "modellong" }, new UniqueArtifactIdConstraint( "org.codehaus.modello" ) );
75 public void testConstraintDisregardGroupId()
80 assertConstraintWithMultipleResultTypes( new String[] { "commons-lang", "test-one", "test-two", "test-two", "test-bar", "modellong" },
81 new UniqueArtifactIdConstraint( "testable_repo", true ) );
84 private void setUpArtifacts()
85 throws ArchivaDatabaseException
87 ArchivaArtifact artifact;
89 // Setup artifacts in fresh DB.
90 artifact = createArtifact( "commons-lang", "commons-lang", "2.0" );
91 artifactDao.saveArtifact( artifact );
93 artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
94 artifactDao.saveArtifact( artifact );
96 artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
97 artifactDao.saveArtifact( artifact );
99 artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
100 artifactDao.saveArtifact( artifact );
102 artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" );
103 artifactDao.saveArtifact( artifact );
105 artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1" );
106 artifactDao.saveArtifact( artifact );
108 artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" );
109 artifactDao.saveArtifact( artifact );
111 artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" );
112 artifactDao.saveArtifact( artifact );
115 private void assertConstraintWithMultipleResultTypes( String[] artifactIds, SimpleConstraint constraint )
118 String prefix = "Unique Artifact IDs: ";
120 List<Object[]> results = dao.query( constraint );
121 assertNotNull( prefix + "Not Null", results );
122 assertEquals( prefix + "Results.size", artifactIds.length, results.size() );
124 List<String> expectedArtifactIds = Arrays.asList( artifactIds );
126 Iterator<Object[]> it = results.iterator();
127 while ( it.hasNext() )
129 Object[] actualArtifactIds = (Object[]) it.next();
130 String actualArtifactId = ( String ) actualArtifactIds[1];
131 assertTrue( prefix + "artifactId result should not be blank.", StringUtils.isNotBlank( actualArtifactId ) );
132 assertTrue( prefix + " artifactId result <" + actualArtifactId + "> exists in expected artifactIds.",
133 expectedArtifactIds.contains( actualArtifactId ) );
137 private void assertConstraint( String[] artifactIds, SimpleConstraint constraint )
139 String prefix = "Unique Artifact IDs: ";
141 List<String> results = dao.query( constraint );
142 assertNotNull( prefix + "Not Null", results );
143 assertEquals( prefix + "Results.size", artifactIds.length, results.size() );
145 List<String> expectedArtifactIds = Arrays.asList( artifactIds );
147 Iterator<String> it = results.iterator();
148 while ( it.hasNext() )
150 String actualArtifactId = (String) it.next();
151 assertTrue( prefix + "artifactId result should not be blank.", StringUtils.isNotBlank( actualArtifactId ) );
152 assertTrue( prefix + " artifactId result <" + actualArtifactId + "> exists in expected artifactIds.",
153 expectedArtifactIds.contains( actualArtifactId ) );