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 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;
27  
28  import java.util.Calendar;
29  import java.util.List;
30  
31  /**
32   * OlderArtifactsByAgeConstraintTest 
33   *
34   * @version $Id: OlderArtifactsByAgeConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
35   */
36  public class OlderArtifactsByAgeConstraintTest
37      extends AbstractArchivaDatabaseTestCase
38  {
39      private ArtifactDAO artifactDao;
40  
41      protected 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      public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld )
51      {
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" );
58          return artifact;
59      }
60  
61      public void testConstraint()
62          throws Exception
63      {
64          ArchivaArtifact artifact;
65  
66          // Setup artifacts in fresh DB.
67          artifact = createArtifact( "test-one", "1.0", 200 );
68          artifactDao.saveArtifact( artifact );
69  
70          artifact = createArtifact( "test-one", "1.1", 100 );
71          artifactDao.saveArtifact( artifact );
72  
73          artifact = createArtifact( "test-one", "1.2", 50 );
74          artifactDao.saveArtifact( artifact );
75  
76          artifact = createArtifact( "test-two", "1.0", 200 );
77          artifactDao.saveArtifact( artifact );
78  
79          artifact = createArtifact( "test-two", "2.0", 150 );
80          artifactDao.saveArtifact( artifact );
81  
82          artifact = createArtifact( "test-two", "2.1", 100 );
83          artifactDao.saveArtifact( artifact );
84  
85          artifact = createArtifact( "test-two", "3.0", 5 );
86          artifactDao.saveArtifact( artifact );
87  
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 ) );
93      }
94  
95      private void assertConstraint( int expectedHits, Constraint constraint )
96          throws Exception
97      {
98          List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
99          assertNotNull( "Older Artifacts By Age: Not Null", results );
100         assertEquals( "Older Artifacts By Age: Results.size", expectedHits, results.size() );
101     }
102 }