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 java.util.Calendar;
23 import java.util.Date;
24 import java.util.List;
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;
32 * ArtifactsByRepositoryConstraintTest
36 public class ArtifactsByRepositoryConstraintTest
37 extends AbstractArchivaDatabaseTestCase
39 private ArtifactDAO artifactDao;
46 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
47 artifactDao = dao.getArtifactDAO();
50 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type )
52 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, null, type, "test-repo" );
53 artifact.getModel().setLastModified( new Date() );
54 artifact.getModel().setRepositoryId( "test-repo" );
59 public void testQueryAllArtifactsInRepo()
62 Date whenGathered = Calendar.getInstance().getTime();
63 whenGathered.setTime( 123456789 );
65 ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
66 artifact.getModel().setWhenGathered( whenGathered );
67 artifactDao.saveArtifact( artifact );
69 artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
70 artifact.getModel().setWhenGathered( whenGathered );
71 artifactDao.saveArtifact( artifact );
73 artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.0.2", "jar" );
74 artifact.getModel().setWhenGathered( whenGathered );
75 artifactDao.saveArtifact( artifact );
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 );
82 assertConstraint( "Artifacts By Repository", 3, new ArtifactsByRepositoryConstraint( "test-repo" ) );
85 public void testQueryArtifactsInRepoWithWhenGathered()
88 Date whenGathered = Calendar.getInstance().getTime();
90 ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
91 artifact.getModel().setWhenGathered( whenGathered );
92 artifactDao.saveArtifact( artifact );
94 artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
95 artifact.getModel().setWhenGathered( whenGathered );
96 artifactDao.saveArtifact( artifact );
98 artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.2", "jar" );
99 artifact.getModel().setWhenGathered( whenGathered );
100 artifactDao.saveArtifact( artifact );
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 );
107 Date olderWhenGathered = Calendar.getInstance().getTime();
108 olderWhenGathered.setTime( 123456789 );
110 artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.1-SNAPSHOT", "jar" );
111 artifact.getModel().setWhenGathered( olderWhenGathered );
112 artifactDao.saveArtifact( artifact );
114 artifact = createArtifact( "org.apache.archiva", "artifact-three", "2.0-beta-1", "jar" );
115 artifact.getModel().setWhenGathered( whenGathered );
116 artifactDao.saveArtifact( artifact );
118 assertConstraint( "Artifacts By Repository and When Gathered", 4,
119 new ArtifactsByRepositoryConstraint( "test-repo", whenGathered, "repositoryId", false ) );
122 public void testQueryArtifactsInRepoByType()
125 Calendar cal = Calendar.getInstance();
126 cal.set( 2008, 9, 1 );
127 Date whenGathered = cal.getTime();
129 ArchivaArtifact artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0", "jar" );
130 artifact.getModel().setWhenGathered( whenGathered );
131 artifactDao.saveArtifact( artifact );
133 artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.1", "jar" );
134 artifact.getModel().setWhenGathered( whenGathered );
135 artifactDao.saveArtifact( artifact );
137 artifact = createArtifact( "org.apache.archiva", "artifact-one", "1.0.2", "jar" );
138 artifact.getModel().setWhenGathered( whenGathered );
139 artifactDao.saveArtifact( artifact );
141 artifact = createArtifact( "org.apache.archiva", "artifact-two", "1.1-SNAPSHOT", "war" );
142 artifact.getModel().setWhenGathered( whenGathered );
143 artifactDao.saveArtifact( artifact );
145 artifact = createArtifact( "org.apache.archiva", "artifact-three", "2.0-beta-1", "war" );
146 artifact.getModel().setWhenGathered( whenGathered );
147 artifactDao.saveArtifact( artifact );
149 Calendar cal2 = Calendar.getInstance();
150 cal2.set( 2008, 12, 12 );
151 Date diffWhenGathered = cal2.getTime();
153 artifact = createArtifact( "org.apache.archiva", "artifact-one", "2.0", "jar" );
154 artifact.getModel().setWhenGathered( diffWhenGathered );
155 artifactDao.saveArtifact( artifact );
157 cal2 = Calendar.getInstance();
158 cal2.set( 2008, 10, 30 );
159 Date before = cal2.getTime();
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" ) );
167 private void assertConstraint( String msg, int count, ArtifactsByRepositoryConstraint constraint )
170 List results = artifactDao.queryArtifacts( constraint );
171 assertNotNull( msg + ": Not Null", results );
172 assertEquals( msg + ": Results.size", count, results.size() );