View Javadoc

1   package org.apache.maven.archiva.database.constraints;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
28  
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Date;
32  import java.util.List;
33  
34  /**
35   * UniqueVersionConstraintTest 
36   *
37   * @version $Id: UniqueVersionConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
38   */
39  public class UniqueVersionConstraintTest
40      extends AbstractArchivaDatabaseTestCase
41  {
42      private ArtifactDAO artifactDao;
43  
44      public void testConstraintGroupIdArtifactIdCommonsLang()
45          throws Exception
46      {
47          setupArtifacts();
48  
49          assertConstraint( new String[] { "2.0", "2.1" }, new UniqueVersionConstraint( "commons-lang", "commons-lang" ) );
50      }
51  
52      public void testConstraintGroupIdArtifactIdInvalid()
53          throws Exception
54      {
55          setupArtifacts();
56  
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" ) );
60      }
61  
62      public void testConstraintGroupIdArtifactIdMavenSharedTestTwo()
63          throws Exception
64      {
65          setupArtifacts();
66  
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" ) );
69      }
70  
71      public void testConstraintGroupIdArtifactIdMavenSharedTestTwoCentralOnly()
72          throws Exception
73      {
74          setupArtifacts();
75  
76          List<String> observableRepositories = new ArrayList<String>();
77          observableRepositories.add( "central" );
78  
79          assertConstraint( new String[] { "2.0", "2.1.1", "2.1-alpha-1" },
80                            new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
81      }
82  
83      public void testConstraintGroupIdArtifactIdMavenSharedTestTwoSnapshotsOnly()
84          throws Exception
85      {
86          setupArtifacts();
87  
88          List<String> observableRepositories = new ArrayList<String>();
89          observableRepositories.add( "snapshots" );
90  
91          assertConstraint( new String[] { "2.1-SNAPSHOT" }, 
92                            new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
93      }
94  
95      public void testConstraintGroupIdArtifactIdMavenTestOne()
96          throws Exception
97      {
98          setupArtifacts();
99  
100         assertConstraint( new String[] { "1.2" }, new UniqueVersionConstraint( "org.apache.maven.test", "test-one" ) );
101     }
102 
103     public void testConstraintGroupIdArtifactIdModelloLong()
104         throws Exception
105     {
106         setupArtifacts();
107 
108         assertConstraint( new String[] { "3.0" }, new UniqueVersionConstraint( "org.codehaus.modello", "modellong" ) );
109     }
110 
111     @SuppressWarnings("unchecked")
112     private void assertConstraint( String[] versions, SimpleConstraint constraint )
113     {
114         String prefix = "Unique Versions: ";
115 
116         List<String> results = (List<String>) dao.query( constraint );
117         assertNotNull( prefix + "Not Null", results );
118         assertEquals( prefix + "Results.size", versions.length, results.size() );
119 
120         List<String> expectedVersions = Arrays.asList( versions );
121 
122         for ( String actualVersion : results )
123         {
124             assertTrue( prefix + "version result should not be blank.", StringUtils.isNotBlank( actualVersion ) );
125             assertTrue( prefix + "version result <" + actualVersion + "> exists in expected versions.",
126                         expectedVersions.contains( actualVersion ) );
127         }
128     }
129 
130     private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
131     {
132         ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "testrepo" );
133         artifact.getModel().setLastModified( new Date() ); // mandatory field.
134         artifact.getModel().setRepositoryId( repoId );
135         return artifact;
136     }
137 
138     private void setupArtifacts()
139         throws Exception
140     {
141         ArchivaArtifact artifact;
142 
143         // Setup artifacts in fresh DB.
144         artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
145         artifactDao.saveArtifact( artifact );
146 
147         artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
148         artifactDao.saveArtifact( artifact );
149 
150         artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
151         artifactDao.saveArtifact( artifact );
152 
153         artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
154         artifactDao.saveArtifact( artifact );
155 
156         artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
157         artifactDao.saveArtifact( artifact );
158 
159         artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1.1" );
160         artifactDao.saveArtifact( artifact );
161 
162         artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
163         artifactDao.saveArtifact( artifact );
164 
165         artifact = createArtifact( "central", "org.apache.maven.shared", "test-bar", "2.1" );
166         artifactDao.saveArtifact( artifact );
167 
168         artifact = createArtifact( "central", "org.codehaus.modello", "modellong", "3.0" );
169         artifactDao.saveArtifact( artifact );
170 
171         // Snapshots repository artifacts
172         artifact = createArtifact( "snapshots", "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
173         artifactDao.saveArtifact( artifact );
174 
175         artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
176         artifactDao.saveArtifact( artifact );
177 
178         artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
179         artifactDao.saveArtifact( artifact );
180 
181         artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
182         artifactDao.saveArtifact( artifact );
183     }
184 
185     protected void setUp()
186         throws Exception
187     {
188         super.setUp();
189 
190         ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
191         artifactDao = dao.getArtifactDAO();
192     }
193 }