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 java.util.Calendar;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
27  import org.apache.maven.archiva.database.ArchivaDAO;
28  import org.apache.maven.archiva.database.ArtifactDAO;
29  import org.apache.maven.archiva.model.ArchivaArtifact;
30  
31  /**
32   * ArtifactVersionsConstraintTest
33   * 
34   * @version
35   */
36  public class ArtifactVersionsConstraintTest
37      extends AbstractArchivaDatabaseTestCase
38  {
39      private ArtifactDAO artifactDao; 
40      
41      public static final String TEST_REPO = "test-repo";
42      
43      @Override
44      public void setUp()
45          throws Exception
46      {
47          super.setUp();
48          
49          ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
50          artifactDao = dao.getArtifactDAO();
51      }
52      
53      private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
54      {
55          ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, null, "jar", TEST_REPO );
56          artifact.getModel().setLastModified( new Date() );
57          artifact.getModel().setRepositoryId( TEST_REPO );
58  
59          return artifact;
60      }
61      
62      private void populateDb()
63          throws Exception
64      {
65          Date whenGathered = Calendar.getInstance().getTime();
66          whenGathered.setTime( 123456789 );
67  
68          ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0" );
69          artifact.getModel().setWhenGathered( null );
70          artifactDao.saveArtifact( artifact );
71  
72          artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1" );
73          artifact.getModel().setWhenGathered( whenGathered );
74          artifactDao.saveArtifact( artifact );
75  
76          artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.2" );
77          artifact.getModel().setWhenGathered( whenGathered );
78          artifactDao.saveArtifact( artifact );
79  
80          artifact = createArtifact( "org.apache.archiva", "artifact-one", "2.0" );
81          artifact.getModel().setRepositoryId( "different-repo" );
82          artifact.getModel().setWhenGathered( whenGathered );
83          artifactDao.saveArtifact( artifact );
84      }
85      
86      public void testQueryAllVersionsOfArtifactAcrossRepos() throws Exception
87      {        
88          populateDb();
89          assertConstraint( "Artifacts By Repository", 3, 
90                            new ArtifactVersionsConstraint( null, "org.apache.archiva", "artifact-one", true ) );
91      }    
92      
93      public void testQueryAllVersionsOfArtifactInARepo() throws Exception
94      {
95          populateDb();
96          assertConstraint( "Artifacts By Repository", 2, 
97                            new ArtifactVersionsConstraint( TEST_REPO, "org.apache.archiva", "artifact-one", true ) );
98      }
99      
100     private void assertConstraint( String msg, int count, ArtifactVersionsConstraint constraint )
101         throws Exception
102     {
103         List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
104         assertNotNull( msg + ": Not Null", results );
105         assertEquals( msg + ": Results.size", count, results.size() );
106     }
107 }