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.maven.archiva.database.AbstractArchivaDatabaseTestCase;
23 import org.apache.maven.archiva.database.ArchivaDAO;
24 import org.apache.maven.archiva.database.ArtifactDAO;
25 import org.apache.maven.archiva.database.Constraint;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import java.util.Calendar;
29 import java.util.List;
32 * RecentArtifactsByAgeConstraintTest
36 public class RecentArtifactsByAgeConstraintTest
37 extends AbstractArchivaDatabaseTestCase
39 private ArtifactDAO artifactDao;
42 protected void setUp()
47 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
48 artifactDao = dao.getArtifactDAO();
51 public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld )
53 ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version,
54 "", "jar", "testable_repo" );
55 Calendar cal = Calendar.getInstance();
56 cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld );
57 artifact.getModel().setLastModified( cal.getTime() );
58 artifact.getModel().setRepositoryId( "testable_repo" );
62 public void testConstraint()
65 ArchivaArtifact artifact;
67 // Setup artifacts in fresh DB.
68 artifact = createArtifact( "test-one", "1.0", 200 );
69 artifactDao.saveArtifact( artifact );
71 artifact = createArtifact( "test-one", "1.1", 100 );
72 artifactDao.saveArtifact( artifact );
74 artifact = createArtifact( "test-one", "1.2", 50 );
75 artifactDao.saveArtifact( artifact );
77 artifact = createArtifact( "test-two", "1.0", 200 );
78 artifactDao.saveArtifact( artifact );
80 artifact = createArtifact( "test-two", "2.0", 150 );
81 artifactDao.saveArtifact( artifact );
83 artifact = createArtifact( "test-two", "2.1", 100 );
84 artifactDao.saveArtifact( artifact );
86 artifact = createArtifact( "test-two", "3.0", 5 );
87 artifactDao.saveArtifact( artifact );
89 assertConstraint( 0, new RecentArtifactsByAgeConstraint( 2 ) );
90 assertConstraint( 1, new RecentArtifactsByAgeConstraint( 7 ) );
91 assertConstraint( 2, new RecentArtifactsByAgeConstraint( 90 ) );
92 assertConstraint( 4, new RecentArtifactsByAgeConstraint( 100 ) );
93 assertConstraint( 5, new RecentArtifactsByAgeConstraint( 150 ) );
94 assertConstraint( 7, new RecentArtifactsByAgeConstraint( 9000 ) );
97 private void assertConstraint( int expectedHits, Constraint constraint )
100 List results = artifactDao.queryArtifacts( constraint );
101 assertNotNull( "Recent Artifacts By Age: Not Null", results );
102 assertEquals( "Recent Artifacts By Age: Results.size", expectedHits, results.size() );