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   * ArtifactsByRepositoryConstraintTest
33   * 
34   * @version
35   */
36  public class ArtifactsByRepositoryConstraintTest
37      extends AbstractArchivaDatabaseTestCase
38  {
39      private ArtifactDAO artifactDao; 
40  
41      public void setUp()
42          throws Exception
43      {
44          super.setUp(); 
45  
46          ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
47          artifactDao = dao.getArtifactDAO();
48      }
49  
50      private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type )
51      {
52          ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, null, type, "test-repo" );
53          artifact.getModel().setLastModified( new Date() );
54          artifact.getModel().setRepositoryId( "test-repo" );
55  
56          return artifact;
57      }
58  
59      public void testQueryAllArtifactsInRepo()
60          throws Exception
61      {
62          Date whenGathered = Calendar.getInstance().getTime();
63          whenGathered.setTime( 123456789 );
64  
65          ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
66          artifact.getModel().setWhenGathered( whenGathered );
67          artifactDao.saveArtifact( artifact );
68  
69          artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
70          artifact.getModel().setWhenGathered( whenGathered );
71          artifactDao.saveArtifact( artifact );
72  
73          artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.0.2", "jar" );
74          artifact.getModel().setWhenGathered( whenGathered );
75          artifactDao.saveArtifact( artifact );
76  
77          artifact = createArtifact( "org.apache.archiva", "artifact-one", "2.0", "jar" );
78          artifact.getModel().setRepositoryId( "different-repo" );
79          artifact.getModel().setWhenGathered( whenGathered );
80          artifactDao.saveArtifact( artifact );
81  
82          assertConstraint( "Artifacts By Repository", 3, new ArtifactsByRepositoryConstraint( "test-repo" ) );
83      }
84  
85      public void testQueryArtifactsInRepoWithWhenGathered()
86          throws Exception
87      {
88          Date whenGathered = Calendar.getInstance().getTime();
89  
90          ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
91          artifact.getModel().setWhenGathered( whenGathered );
92          artifactDao.saveArtifact( artifact );
93  
94          artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
95          artifact.getModel().setWhenGathered( whenGathered );
96          artifactDao.saveArtifact( artifact );
97  
98          artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.2", "jar" );
99          artifact.getModel().setWhenGathered( whenGathered );
100         artifactDao.saveArtifact( artifact );
101 
102         artifact = createArtifact( "org.apache.archiva", "artifact-one", "2.0", "jar" );
103         artifact.getModel().setRepositoryId( "different-repo" );
104         artifact.getModel().setWhenGathered( whenGathered );
105         artifactDao.saveArtifact( artifact );
106 
107         Date olderWhenGathered = Calendar.getInstance().getTime();
108         olderWhenGathered.setTime( 123456789 );
109         
110         artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.1-SNAPSHOT", "jar" );
111         artifact.getModel().setWhenGathered( olderWhenGathered );
112         artifactDao.saveArtifact( artifact );
113 
114         artifact = createArtifact( "org.apache.archiva", "artifact-three", "2.0-beta-1", "jar" );
115         artifact.getModel().setWhenGathered( whenGathered );
116         artifactDao.saveArtifact( artifact );
117 
118         assertConstraint( "Artifacts By Repository and When Gathered", 4,
119                           new ArtifactsByRepositoryConstraint( "test-repo", whenGathered, "repositoryId", false ) );
120     }
121     
122     public void testQueryArtifactsInRepoByType()
123         throws Exception
124     {
125         Calendar cal = Calendar.getInstance();
126         cal.set( 2008, 9, 1 );
127         Date whenGathered = cal.getTime();
128 
129         ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
130         artifact.getModel().setWhenGathered( whenGathered );
131         artifactDao.saveArtifact( artifact );
132 
133         artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
134         artifact.getModel().setWhenGathered( whenGathered );
135         artifactDao.saveArtifact( artifact );
136 
137         artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.2", "jar" );
138         artifact.getModel().setWhenGathered( whenGathered );
139         artifactDao.saveArtifact( artifact );
140         
141         artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.1-SNAPSHOT", "war" );
142         artifact.getModel().setWhenGathered( whenGathered );
143         artifactDao.saveArtifact( artifact );
144 
145         artifact = createArtifact( "org.apache.archiva", "artifact-three", "2.0-beta-1", "war" );
146         artifact.getModel().setWhenGathered( whenGathered );
147         artifactDao.saveArtifact( artifact );
148 
149         Calendar cal2 = Calendar.getInstance();
150         cal2.set( 2008, 12, 12 );
151         Date diffWhenGathered = cal2.getTime();
152         
153         artifact = createArtifact( "org.apache.archiva", "artifact-one", "2.0", "jar" );
154         artifact.getModel().setWhenGathered( diffWhenGathered );
155         artifactDao.saveArtifact( artifact );
156         
157         cal2 = Calendar.getInstance();
158         cal2.set( 2008, 10, 30 );
159         Date before = cal2.getTime();
160         
161         assertConstraint( "Artifacts of Type 'jar' By Repository and When Gathered", 3,
162                           new ArtifactsByRepositoryConstraint( "test-repo", "jar", before, "whenGathered" ) );
163         assertConstraint( "Artifacts of Type 'war' By Repository and When Gathered", 2,
164                           new ArtifactsByRepositoryConstraint( "test-repo", "war", before, "whenGathered" ) );
165     }
166     
167     private void assertConstraint( String msg, int count, ArtifactsByRepositoryConstraint constraint )
168         throws Exception
169     {
170         List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
171         assertNotNull( msg + ": Not Null", results );
172         assertEquals( msg + ": Results.size", count, results.size() );
173     }
174 }