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.List;
35 * UniqueVersionConstraintTest
39 public class UniqueVersionConstraintTest
40 extends AbstractArchivaDatabaseTestCase
42 private ArtifactDAO artifactDao;
44 public void testConstraintGroupIdArtifactIdCommonsLang()
49 assertConstraint( new String[] { "2.0", "2.1" }, new UniqueVersionConstraint( "commons-lang", "commons-lang" ) );
52 public void testConstraintGroupIdArtifactIdInvalid()
57 assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache", "invalid" ) );
58 assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache.test", "invalid" ) );
59 assertConstraint( new String[] {}, new UniqueVersionConstraint( "invalid", "test-two" ) );
62 public void testConstraintGroupIdArtifactIdMavenSharedTestTwo()
67 assertConstraint( new String[] { "2.0", "2.1-SNAPSHOT", "2.1.1", "2.1-alpha-1" },
68 new UniqueVersionConstraint( "org.apache.maven.shared", "test-two" ) );
71 public void testConstraintGroupIdArtifactIdMavenSharedTestTwoCentralOnly()
76 List<String> observableRepositories = new ArrayList<String>();
77 observableRepositories.add( "central" );
79 assertConstraint( new String[] { "2.0", "2.1.1", "2.1-alpha-1" },
80 new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
83 public void testConstraintGroupIdArtifactIdMavenSharedTestTwoSnapshotsOnly()
88 List<String> observableRepositories = new ArrayList<String>();
89 observableRepositories.add( "snapshots" );
91 assertConstraint( new String[] { "2.1-SNAPSHOT" },
92 new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
95 public void testConstraintGroupIdArtifactIdMavenTestOne()
100 assertConstraint( new String[] { "1.2" }, new UniqueVersionConstraint( "org.apache.maven.test", "test-one" ) );
103 public void testConstraintGroupIdArtifactIdModelloLong()
108 assertConstraint( new String[] { "3.0" }, new UniqueVersionConstraint( "org.codehaus.modello", "modellong" ) );
111 private void assertConstraint( String[] versions, SimpleConstraint constraint )
113 String prefix = "Unique Versions: ";
115 List<String> results = dao.query( constraint );
116 assertNotNull( prefix + "Not Null", results );
117 assertEquals( prefix + "Results.size", versions.length, results.size() );
119 List<String> expectedVersions = Arrays.asList( versions );
121 for ( String actualVersion : results )
123 assertTrue( prefix + "version result should not be blank.", StringUtils.isNotBlank( actualVersion ) );
124 assertTrue( prefix + "version result <" + actualVersion + "> exists in expected versions.",
125 expectedVersions.contains( actualVersion ) );
129 private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
131 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "testrepo" );
132 artifact.getModel().setLastModified( new Date() ); // mandatory field.
133 artifact.getModel().setRepositoryId( repoId );
137 private void setupArtifacts()
140 ArchivaArtifact artifact;
142 // Setup artifacts in fresh DB.
143 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
144 artifactDao.saveArtifact( artifact );
146 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
147 artifactDao.saveArtifact( artifact );
149 artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
150 artifactDao.saveArtifact( artifact );
152 artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
153 artifactDao.saveArtifact( artifact );
155 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
156 artifactDao.saveArtifact( artifact );
158 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1.1" );
159 artifactDao.saveArtifact( artifact );
161 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
162 artifactDao.saveArtifact( artifact );
164 artifact = createArtifact( "central", "org.apache.maven.shared", "test-bar", "2.1" );
165 artifactDao.saveArtifact( artifact );
167 artifact = createArtifact( "central", "org.codehaus.modello", "modellong", "3.0" );
168 artifactDao.saveArtifact( artifact );
170 // Snapshots repository artifacts
171 artifact = createArtifact( "snapshots", "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
172 artifactDao.saveArtifact( artifact );
174 artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
175 artifactDao.saveArtifact( artifact );
177 artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
178 artifactDao.saveArtifact( artifact );
180 artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
181 artifactDao.saveArtifact( artifact );
184 protected void setUp()
189 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
190 artifactDao = dao.getArtifactDAO();