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 * OlderArtifactsByAgeConstraintTest
36 public class OlderArtifactsByAgeConstraintTest
37 extends AbstractArchivaDatabaseTestCase
39 private ArtifactDAO artifactDao;
41 protected void setUp()
46 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
47 artifactDao = dao.getArtifactDAO();
50 public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld )
52 ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version,
53 "", "jar", "testable_repo" );
54 Calendar cal = Calendar.getInstance();
55 cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld );
56 artifact.getModel().setLastModified( cal.getTime() );
57 artifact.getModel().setRepositoryId( "testable_repo" );
61 public void testConstraint()
64 ArchivaArtifact artifact;
66 // Setup artifacts in fresh DB.
67 artifact = createArtifact( "test-one", "1.0", 200 );
68 artifactDao.saveArtifact( artifact );
70 artifact = createArtifact( "test-one", "1.1", 100 );
71 artifactDao.saveArtifact( artifact );
73 artifact = createArtifact( "test-one", "1.2", 50 );
74 artifactDao.saveArtifact( artifact );
76 artifact = createArtifact( "test-two", "1.0", 200 );
77 artifactDao.saveArtifact( artifact );
79 artifact = createArtifact( "test-two", "2.0", 150 );
80 artifactDao.saveArtifact( artifact );
82 artifact = createArtifact( "test-two", "2.1", 100 );
83 artifactDao.saveArtifact( artifact );
85 artifact = createArtifact( "test-two", "3.0", 5 );
86 artifactDao.saveArtifact( artifact );
88 assertConstraint( 6, new OlderArtifactsByAgeConstraint( 7 ) );
89 assertConstraint( 5, new OlderArtifactsByAgeConstraint( 90 ) );
90 assertConstraint( 5, new OlderArtifactsByAgeConstraint( 100 ) );
91 assertConstraint( 3, new OlderArtifactsByAgeConstraint( 150 ) );
92 assertConstraint( 0, new OlderArtifactsByAgeConstraint( 9000 ) );
95 private void assertConstraint( int expectedHits, Constraint constraint )
98 List results = artifactDao.queryArtifacts( constraint );
99 assertNotNull( "Older Artifacts By Age: Not Null", results );
100 assertEquals( "Older Artifacts By Age: Results.size", expectedHits, results.size() );